You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

BigQuery仅按email分组查询报错,咨询实现方法

BigQuery分组查询问题解决

BigQuery严格遵循ANSI SQL标准,不支持仅按单个字段分组,同时在SELECT子句中引用未被聚合、也未加入GROUP BY的字段——这就是你遇到报错的核心原因。DBeaver的宽松模式允许非标准写法,但BigQuery会严格校验分组逻辑的合法性。

针对你的需求,分两种场景给出解决方案:

场景1:每个email对应唯一的typology值

如果同一个email的所有记录中typology值完全一致,直接将typology加入GROUP BY子句即可,修改后的SQL如下:

select 
  email, 
  format_date('%m %Y', min(booking_date)),              
  CASE when typology = 1 then 1 else 0 end as first_purchase_1,                 
  CASE when typology = 0 then 1 else 0 end as first_purchase_0        
from `bookings`                                                    
group by email, typology

场景2:一个email可能对应多个typology值

如果需要统计每个email是否存在typology=1或typology=0的记录,要对CASE表达式使用聚合函数(比如MAX),确保分组逻辑符合SQL标准:

select 
  email, 
  format_date('%m %Y', min(booking_date)),              
  MAX(CASE when typology = 1 then 1 else 0 end) as first_purchase_1,                 
  MAX(CASE when typology = 0 then 1 else 0 end) as first_purchase_0        
from `bookings`                                                    
group by email

这里MAX的作用是:只要该email下有至少一条typology=1的记录,first_purchase_1就返回1,否则返回0;first_purchase_0逻辑同理。

内容的提问来源于stack exchange,提问作者Marina Pérez Sáiz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 16:40:21