考虑下表,我如何将这些分数分组为三个桶(不多于):小于等于150,在150到350之间,超过350。
id | score
----+-------
1 | 5
2 | 5
3 | 5
4 | 4
5 | 5
6 | 4
7 | 4
8 | 4
9 | 2
10 | 2
11 | 6
12 | 205
13 | 250
13 | 400
14 | 105
15 | 900
16 | 1300
我试过这个方法:
select (score/100)*100 || '-' || (score/100)*100 + 100 as scorerange,count(*)
from scores group by score/100 order by score/100;
这就是结果:
scorerange | count
------------+-------
0-100 | 11
100-200 | 1
200-300 | 2
400-500 | 1
900-1000 | 1
1300-1400 | 1
(6 rows)
它将分数分组,但不在我需要的三个分组中。
答案 0 :(得分:1)
这将为您提供所需的范围:
SELECT
CASE WHEN min(score) <= 150 THEN '*-150'
WHEN min(score) <= 350 THEN '151-350'
ELSE '350-*'
END AS scorerange, #A comma is needed here right after scorerange
count(*)
FROM scores
GROUP BY score <= 150, score <= 350;
答案 1 :(得分:0)
此查询将为您提供预期的输出。
SELECT CASE WHEN Score <= 150 THEN '0-150'
WHEN Score >= 350 THEN '150-350'
ELSE '350+' END AS SCORE_RANGE,
COUNT(*) AS COUNT
FROM SCORES
GROUP BY SCORE_RANGE