我正试图在一个月内使用一个promo_code
字段进行促销分组,因为单个customer_ID
可能有多个交易,并且可能有两个不同的促销代码>
SELECT customer_id AS buyer,
CASE
WHEN COUNT(DISTINCT flag_promo) = 2 THEN 'Mixed'
WHEN COUNT(DISTINCT flag_promo) = 1 AND flag_promo = 1 THEN 'Promo'
WHEN COUNT(DISTINCT flag_promo) = 1 AND flag_promo = 0 THEN 'Organic'
END AS promo_group
FROM TABLE
WHERE DATE BETWEEN '2019-04-01' AND '2019-04-30'
GROUP BY 1
ORDER BY 2
它给了我一条错误消息:
SELECT list expression references column flag_promo which is neither grouped nor aggregated at [4:41]
答案 0 :(得分:2)
以下是用于BigQuery标准SQL
result
答案 1 :(得分:0)
这是我想您要执行的查询:
09:23:30.610 [pool-2-thread-1] INFO m.controller.MemberController - member.email is foo@gmail.com
09:23:30.619 [pool-2-thread-1] INFO m.controller.MemberController - check point 1001: finish JsonBuilder
09:23:31.069 [pool-2-thread-1] ERROR i.m.h.s.netty.RoutingInBoundHandler - Unexpected error occurred: null
java.lang.StackOverflowError: null
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:101)
at groovy.lang.MetaBeanProperty.getProperty(MetaBeanProperty.java:59)
at groovy.lang.PropertyValue.getValue(PropertyValue.java:42)
at org.codehaus.groovy.runtime.DefaultGroovyMethods.getProperties(DefaultGroovyMethods.java:557)
at groovy.json.DefaultJsonGenerator.getObjectProperties(DefaultJsonGenerator.java:242)
at groovy.json.DefaultJsonGenerator.writeObject(DefaultJsonGenerator.java:236)
at groovy.json.DefaultJsonGenerator.writeMapEntry(DefaultJsonGenerator.java:387)
at groovy.json.DefaultJsonGenerator.writeMap(DefaultJsonGenerator.java:375)
at groovy.json.DefaultJsonGenerator.writeObject(DefaultJsonGenerator.java:237)
at groovy.json.DefaultJsonGenerator.writeObject(DefaultJsonGenerator.java:164)
at groovy.json.DefaultJsonGenerator.writeIterator(DefaultJsonGenerator.java:402)
at groovy.json.DefaultJsonGenerator.writeArray(DefaultJsonGenerator.java:285)
at groovy.json.DefaultJsonGenerator.writeObject(DefaultJsonGenerator.java:221)
at groovy.json.DefaultJsonGenerator.writeMapEntry(DefaultJsonGenerator.java:387)
at groovy.json.DefaultJsonGenerator.writeMap(DefaultJsonGenerator.java:375)
at groovy.json.DefaultJsonGenerator.writeObject(DefaultJsonGenerator.java:237)
at groovy.json.DefaultJsonGenerator.writeObject(DefaultJsonGenerator.java:164)
at groovy.json.DefaultJsonGenerator.writeIterator(DefaultJsonGenerator.java:402)
at groovy.json.DefaultJsonGenerator.writeArray(DefaultJsonGenerator.java:285)
at groovy.json.DefaultJsonGenerator.writeObject(DefaultJsonGenerator.java:221)
at groovy.json.DefaultJsonGenerator.writeMapEntry(DefaultJsonGenerator.java:387)
at groovy.json.DefaultJsonGenerator.writeMap(DefaultJsonGenerator.java:375)
at groovy.json.DefaultJsonGenerator.writeObject(DefaultJsonGenerator.java:237)
at groovy.json.DefaultJsonGenerator.writeObject(DefaultJsonGenerator.java:164)
at groovy.json.DefaultJsonGenerator.writeIterator(DefaultJsonGenerator.java:402)
at groovy.json.DefaultJsonGenerator.writeArray(DefaultJsonGenerator.java:285)
at groovy.json.DefaultJsonGenerator.writeObject(DefaultJsonGenerator.java:221)
at groovy.json.DefaultJsonGenerator.writeMapEntry(DefaultJsonGenerator.java:387)
at groovy.json.DefaultJsonGenerator.writeMap(DefaultJsonGenerator.java:375)
at groovy.json.DefaultJsonGenerator.writeObject(DefaultJsonGenerator.java:237)
at groovy.json.DefaultJsonGenerator.writeObject(DefaultJsonGenerator.java:164)
at groovy.json.DefaultJsonGenerator.writeIterator(DefaultJsonGenerator.java:402)
at groovy.json.DefaultJsonGenerator.writeArray(DefaultJsonGenerator.java:285)
at groovy.json.DefaultJsonGenerator.writeObject(DefaultJsonGenerator.java:221)
at groovy.json.DefaultJsonGenerator.writeMapEntry(DefaultJsonGenerator.java:387)
at groovy.json.DefaultJsonGenerator.writeMap(DefaultJsonGenerator.java:375)
at groovy.json.DefaultJsonGenerator.writeObject(DefaultJsonGenerator.java:237)
at groovy.json.DefaultJsonGenerator.writeObject(DefaultJsonGenerator.java:164)
at groovy.json.DefaultJsonGenerator.writeIterator(DefaultJsonGenerator.java:402)
at groovy.json.DefaultJsonGenerator.writeArray(DefaultJsonGenerator.java:285)
at groovy.json.DefaultJsonGenerator.writeObject(DefaultJsonGenerator.java:221)
at groovy.json.DefaultJsonGenerator.writeMapEntry(DefaultJsonGenerator.java:387)
at groovy.json.DefaultJsonGenerator.writeMap(DefaultJsonGenerator.java:375)
at groovy.json.DefaultJsonGenerator.writeObject(DefaultJsonGenerator.java:237)
at groovy.json.DefaultJsonGenerator.writeObject(DefaultJsonGenerator.java:164)
at groovy.json.DefaultJsonGenerator.writeIterator(DefaultJsonGenerator.java:402)
at groovy.json.DefaultJsonGenerator.writeArray(DefaultJsonGenerator.java:285)
.....
.....
at groovy.json.DefaultJsonGenerator.writeObject(DefaultJsonGenerator.java:221)
at groovy.json.DefaultJsonGenerator.writeMapEntry(DefaultJsonGenerator.java:387)
at groovy.json.DefaultJsonGenerator.writeMap(DefaultJsonGenerator.java:375)
at groovy.json.DefaultJsonGenerator.writeObject(DefaultJsonGenerator.java:237)
at groovy.json.DefaultJsonGenerator.writeObject(DefaultJsonGenerator.java:164)
at groovy.json.DefaultJsonGenerator.writeIterator(DefaultJsonGenerator.java:402)
at groovy.json.DefaultJsonGenerator.writeArray(DefaultJsonGenerator.java:285)
这假设SELECT
customer_id AS buyer,
CASE WHEN COUNT(DISTINCT flag_promo) = 2 THEN 'Mixed'
WHEN COUNT(DISTINCT flag_promo) = 1 AND MIN(flag_promo) = 1 THEN 'Promo'
WHEN COUNT(DISTINCT flag_promo) = 1 AND MIN(flag_promo) = 2 THEN 'Organic'
END AS promo_group
FROM TABLE
WHERE
DATE BETWEEN '2019-04-01' AND '2019-04-30'
GROUP BY 1
ORDER BY 2;
的值为1表示flag_promo
,而Promo
的值为2
。如果没有,那么我们可以轻松地编辑以上查询。