我正在尝试按它们所在的范围对表中的值进行存储,例如,如果我的表如下:
course_name | current enrollment
course_1 | 10
course_2 | 200
course_3 | 500
我得到以下结果:
enrollment_range | courses
10 | 1
100 | 1
500 | 1
到目前为止,我有以下内容:
SELECT
CASE
WHEN courses.current_enrollment >= 500 THEN 500
WHEN courses.current_enrollment >= 250 THEN 250
WHEN courses.current_enrollment >= 100 THEN 100
WHEN courses.current_enrollment >= 50 THEN 50
WHEN courses.current_enrollment >= 30 THEN 30
WHEN courses.current_enrollment >= 10 THEN 10
END enrollment_range, count() AS total
FROM courses
GROUP BY enrollment_range
ORDER BY enrollment_range ASC
但我最终得到了一个额外的结果,即我所拥有的课程总数,所以我得到如下内容:
enrollment_range | courses
10 | 1
100 | 1
500 | 1
| 3