Bigquery错误代码:Window ORDER BY表达式引用的列start_date既未分组也未聚集在

时间:2019-07-18 17:24:46

标签: sql google-bigquery

我正在使用BigQuery for SQL,但我不知道为什么会有这样的错误消息:

Window ORDER BY expression references column start_date which is neither grouped nor aggregated at [4:73]

这是我的代码:

SELECT EXTRACT(WEEK FROM start_date) as week, count(start_date) as count,
RANK() OVER (PARTITION BY start_station_name ORDER BY EXTRACT(WEEK FROM start_date))
from `bigquery-public-data.london_bicycles.cycle_hire`
GROUP BY EXTRACT(WEEK FROM start_date), start_station_name)

我以为我将下面的一周分组了,如最后一行所示。那么,什么会导致此错误消息不断弹出?

3 个答案:

答案 0 :(得分:1)

请尝试使用cte,如下所示

  with cte as
    (
    SELECT *, EXTRACT(WEEK FROM start_date) as week

    from `bigquery-public-data.london_bicycles.cycle_hire`

    ) select week,count(start_date) as count,
     RANK() OVER (PARTITION BY start_station_name ORDER BY week)
     from cte group by week,start_station_name

答案 1 :(得分:1)

这是BigQuery中的解析错误,您可以使用聚合函数来解决。您的查询还有另一个问题,即start_station_name

SELECT EXTRACT(WEEK FROM start_date) as week, start_station_name, count(start_date) as count,
       RANK() OVER (PARTITION BY start_station_name ORDER BY MIN(EXTRACT(WEEK FROM start_date)))
from `bigquery-public-data.london_bicycles.cycle_hire`
GROUP BY 1, 2;

MIN()除了给BigQuery解析查询字母之外,实际上没有其他用途。由于该表达式是GROUP BY的一部分,因此MIN()仅考虑一个值。

这是BigQuery解析中的错误,因为它无法识别该表达式与GROUP BY中的表达式相同。幸运的是,它很容易解决。

答案 2 :(得分:0)

在查询中,您需要确保仅将ORDER BY放在要选择的那些值上。

对于您的查询,问题在于您正在执行ORDER BY EXTRACT(从start_date开始的WEEK)。而不是这样做,您应该写ORDER BY week,因为您已经选择了周