如何将GROUP BY与AGGREGATED列值一起使用

问题描述 投票:-1回答:1

我正在学习SQL,所以我不确定我的问题。我正在运行以下查询:

SELECT count(key), FORMAT_DATE("%Y-%m", DATE(min(created_date))) as First_Month
FROM `xxxxxxxx.xxxxxxx_xxxxx.xxxxxxxx` as table 
GROUP BY First_Month
ORDER BY First_Month

大查询返回给我这个错误:

Error: Column First_Month contains an aggregation function, which is not allowed in GROUP BY at [3:10]

基本上我有一张表格,上面有帐户和付款清单。我想要做的是计算第一个月的帐户数量。我认为我们称之为队列分析...但我不确定。

我觉得除了我的问题还有更复杂的东西,但我无法表达......

sql google-bigquery
1个回答
2
投票

使用子查询。大概你打算这样的事情:

SELECT count(*), FORMAT_DATE("%Y-%m", DATE(min_created_date)) as First_Month
FROM (SELECT t.key, min(created_date) as min_created_date
      FROM `xxxxxxxx.xxxxxxx_xxxxx.xxxxxxxx` t
      GROUP BY t.key
     ) t 
GROUP BY First_Month
ORDER BY First_Month
LIMIT 1000;

如果你期待超过1000个月的历史,你有很多历史。

© www.soinside.com 2019 - 2024. All rights reserved.