项目数据统计功能中有一个需求,需要把人员年龄分成各个年龄段后再统计数量。数据源格式:
需要统计出来的格式为:
实现sql为:
select case
when s.age between 10 and 20 then
'10--20'
when s.age between 20 and 30 then
'20-30'
end as "年龄",
count(*) as "人次"
from student s
group by case
when s.age between 10 and 20 then
'10--20'
when s.age between 20 and 30 then
'20-30'
end

这篇博客介绍了一个SQL查询需求,该查询用于将人员年龄数据按10-20岁和20-30岁两个年龄段进行分组,并统计每个年龄段的人次。查询使用了CASE语句进行条件判断,然后通过GROUP BY进行分组,COUNT(*)计算人次。
1万+

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



