Bigquery - '拥有'没有按预期工作

时间:2016-01-20 11:01:07

标签: google-bigquery having

我有查询:

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'没找到。

同时输入where avg(cnt) over (partition by App) > 5也不起作用。

我做错了什么?

我不想使用双SELECT,因为每个查询都需要花钱,而且它们应该每小时运行一次。

由于

1 个答案:

答案 0 :(得分:1)

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

成本方面应该是一样的。