我有一个疑问:
SELECT date(date) as day, App, count(*) as cnt,
avg(cnt) over (partition by App) as avg_per_day
FROM [DATABASE]
group by date,two_hour, App
现在我基本上只想只使用平均每天下载量超过 5 次的应用程序。但是,当我添加having avg_per_day>5
时,我得到
Field 'avg_per_day' not found.
另外输入 where avg(cnt) over (partition by App) > 5
不起作用。
我做错了什么?
我不想使用双SELECT
,因为每个查询都要花钱,而且它们应该每小时运行一次。
谢谢
最佳答案
OVER() 函数在 HAVING 步骤之后运行 - 因此在 HAVING 阶段,avg_per_day
列仍然不存在。
而不是:
SELECT date(date) as day, App, count(*) as cnt,
avg(cnt) over (partition by App) as avg_per_day
FROM [DATABASE]
group by date,two_hour, App
having avg_per_day>5
尝试:
SELECT day, App, cnt, avg_per_day
FROM (
SELECT date(date) as day, App, count(*) as cnt,
avg(cnt) over (partition by App) as avg_per_day
FROM [DATABASE]
group by date,two_hour, App
)
WHERE avg_per_day>5
成本方面应该是相同的。
关于google-bigquery - Bigquery - 'Having' 无法按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34898206/