BigQuery Over和Analytics功能

时间:2018-01-19 17:21:45

标签: sql google-bigquery

我正在尝试编写一个利用BigQuery的“Over”功能的SQL。

我基本上试图做一些复合和更顺畅的回归,但我有点像一个车辙。我的查询如下:

WITH daily_extract AS (select date_adjusted,
                              sum(some_number) as 
                              daily_sum_of_some_number,
                              entity_id
                       from `some table`
                       where 1=1 -- no filter  
                       and   1=1 -- no filter  
                       group by date_adjusted, entity_id
                       order by date_adjusted
                       )
      select
      date_adjusted as date_adjusted,
      entity_id as entity_id,
      sum(log(1 + daily_sum_of_some_number)) 
          over (PARTITION by entity_id 
                ORDER     by date_adjusted 
                rows unbounded preceding) as smoothed_number
      from daily_extract
      group by 1,2

但我得到的是一个错误,例如:

无法检索数据 - SELECT列表表达式引用列daily_sum_return_selected_nav既未在[16:19]分组也未汇总。

我试图在底部用1,2,3说组,但后来我得到了错误:

无法检索数据 - 第3列包含分析函数,在[18:20]的GROUP BY中不允许使用

我正在阅读下面的链接,但如果有人能帮助我,那将会非常有用!

https://cloud.google.com/bigquery/docs/reference/standard-sql/functions-and-operators#analytic-functions

1 个答案:

答案 0 :(得分:2)

我很容易看到至少三个问题。您的CTE是通过错误的列聚合,窗口框子句是不必要的,并且您在外部查询中缺少聚合。

您可能还有其他问题,但这可能有所帮助:

with daily_extract as (
      select date_adjusted, sum(some_number) as daily_sum_of_some_number,
             entity_id
      from `some table`
      group by date_adjusted, entity_id
------------------------------^
     )
select date_adjusted, entity_id,
      sum(log(1 + sum(daily_sum_of_some_number))) over
          (partition by entity_id 
           order by date_adjusted 
          ) as smoothed_number
from daily_extract
group by 1, 2;