需求:查询在指定年龄段的人数
when age>25 and age <27 then 2
else 0 end as stage ,count(*) as population from student
group by case when age>1 and age <26 then 1
when age>25 and age <27 then 2
else 0 end
结果
stage population
1 1 2
2 2 5
可通过stage的值区分该条记录是属于哪个年龄段
本文展示了如何使用SQL查询语句来统计指定年龄段内的学生数量,通过CASE WHEN结构进行年龄分段,最后通过GROUP BY和COUNT(*)函数汇总结果。
129

被折叠的 条评论
为什么被折叠?



