我试图让SQL将一些连续数据分组到两个桶中,但它引发了我“GROUP BY子句中不允许聚合函数”错误。我怎么能绕过那个?
SELECT
(CASE WHEN AVG(amount) BETWEEN 0 AND 100000 THEN "Group 1"
WHEN AVG(amount) > 100000 THEN "Group 2" END) AS Bucket
, AVG(amount)
FROM DB
GROUP BY bucket
我相信这就是你要找的东西
SELECT
(CASE amount BETWEEN 0 AND 100000 THEN "Group 1"
WHEN amount > 100000 THEN "Group 2" END) AS Bucket
, AVG(amount)
FROM DB
GROUP BY bucket
由于您只处理AVG,因此您不需要分组。
SELECT
(CASE WHEN AVG(amount) BETWEEN 0 AND 100000 THEN "Group 1"
WHEN AVG(amount) > 100000 THEN "Group 2" END) AS Bucket
, AVG(amount)
FROM DB
尝试如下
SELECT
CASE when amount BETWEEN 0 AND 100000 THEN 'Group 1'
WHEN amount > 100000 THEN 'Group 2' END AS Bucket
, AVG(amount)
FROM DB
GROUP BY CASE when amount BETWEEN 0 AND 100000 THEN 'Group 1'
WHEN amount > 100000 THEN 'Group 2' END