将数据分组到存储桶中-SQL

时间:2019-02-28 10:22:00

标签: sql sqlite bucket

我正在尝试让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

3 个答案:

答案 0 :(得分:0)

我相信这就是您要寻找的

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

答案 1 :(得分:0)

尝试如下

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

答案 2 :(得分:0)

由于只与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