【发布时间】:2021-07-12 23:02:19
【问题描述】:
我有用户在 BigQuery 中的几款游戏中花费的数据:
CREATE TABLE if not EXISTS user_values (
user_id int,
value float,
game char
);
INSERT INTO user_values VALUES
(1, 10, 'A'),
(1, 10, 'A'),
(1, 2, 'A'),
(1, 4, 'B'),
(1, 5, 'B'),
(2, 0, 'A'),
(2, 10, 'B'),
(2, 6, 'B');
我想检查每个用户在游戏 A 中的花费是否超过 20,在游戏 B 中是否超过 15。在这种情况下,输出表应该是:
user_id,game,spent_more_than_cutoff
1,A,TRUE
1,B,FALSE
2,A,FALSE
2,B,TRUE
我想为任意数量的用户和 5-10 个游戏执行此操作。我试过这个:
select
game,
user_id,
case
when sum(value) > 20 and game = 'A' then TRUE
when sum(value) > 15 and game = 'B' then TRUE
else FALSE
end as spent_more_than_cutoff,
from user_values
group by 1, 2
但我得到以下错误:
第 3 列包含一个聚合函数,该函数在 [19:20] 的 GROUP BY 中是不允许的
在 BigQuery 中无需针对不同游戏执行不同查询的最简单方法是什么?
有没有all 函数可以帮助做这样的事情?
select
game,
user_id,
case
when sum(value) > 20 and all(game) = 'A' then TRUE
when sum(value) > 15 and all(game) = 'B' then TRUE
else FALSE
end as spent_more_than_cutoff,
from user_values
group by 1, 2
【问题讨论】:
标签: sql google-bigquery