按分组字段的大查询顺序

时间:2018-06-04 13:59:43

标签: group-by google-cloud-platform google-bigquery sql-order-by

我有一个按日期分组的查询工作正常。

SELECT EXTRACT(date FROM DATETIME(timestamp, 'US/Eastern')) date, SUM(users) total_users FROM `mydataset.mytable` 
GROUP BY EXTRACT(date FROM DATETIME(timestamp, 'US/Eastern'))

但是当我尝试按日期订购时:

SELECT EXTRACT(date FROM DATETIME(timestamp, 'US/Eastern')) date, SUM(users) total_users FROM `mydataset.mytable` 
GROUP BY EXTRACT(date FROM DATETIME(timestamp, 'US/Eastern'))
ORDER BY EXTRACT(date FROM DATETIME(timestamp, 'US/Eastern'));

我收到以下错误:

SELECT list expression references column timestamp which is neither grouped nor aggregated at [1:35]

时间戳栏显然是小组的一部分,甚至更奇怪的是,它的工作没有ORDER BY条款......这里发生了什么?

2 个答案:

答案 0 :(得分:2)

#standardSQL
SELECT 
  EXTRACT(DATE FROM DATETIME(timestamp, 'US/Eastern')) date, 
  SUM(users) total_users 
FROM `mydataset.mytable` 
GROUP BY 1
ORDER BY 1 

答案 1 :(得分:1)

你可以尝试subselect:

#standardSQL
SELECT
  date,
  total_users
FROM (
  SELECT
    EXTRACT(date FROM DATETIME(timestamp,'US/Eastern')) date,
    SUM(users) total_users
  FROM
   `mydataset.mytable`
  GROUP BY EXTRACT(date FROM DATETIME(timestamp, 'US/Eastern'))
  )
ORDER BY
   date