bigQuery - 计算值操作

bigQuery - operation with calculated values

我有一个包含两个分区依据子句的查询。 我想知道每个应用在一天内平均被下载了多少次。

SELECT date(timestamp) as date, ID, count(*) as cnt_downloads,
sum(cnt_downloads) over (partition by ID) as total_downl, 
count(*) over (partition by ID) as total_appear,
(sum(cnt_downloads) over (partition by ID)) / (count(*) over (partition by ID)) as avg_dowl

FROM [default.download_activity]
where date(timestamp) >= '2016-01-04' and date(timestamp) <= '2016-01-20' 
group by date,two_hour, ID

因此查询在没有 FROM 之前的最后一行的情况下仍然有效。而且我知道我可以通过简单地包含 (avg (cnt_downloads) over (partition by ID)) as avg_dowl.

来获得价值

但是我的问题是如何包含 partition over 子句的两个子结果的操作。 我尝试了使用和不使用 () 以及 []。总是报这个字段不是原始数据库的一部分的错误。

谢谢

不要害怕使用子查询。

SELECT date, ID,
             cnt_downloads,
             total_down1,
             total_appear,
             total_down1/total_appear AS avg_dowl
FROM
  (SELECT date(TIMESTAMP) AS date,
          ID,
          count(*) AS cnt_downloads,
          sum(cnt_downloads) over (partition BY ID) AS total_downl,
          count(*) over (partition BY ID) AS total_appear,
   FROM [default.download_activity]
   WHERE date(TIMESTAMP) >= '2016-01-04'
     AND date(TIMESTAMP) <= '2016-01-20'
   GROUP BY 1,
            2
   )