sqlserver: 范围分组查询(group by, case)

来源:互联网 发布:业余开淘宝店能赚钱吗 编辑:程序博客网 时间:2024/06/05 22:46

需求:查询在指定年龄段的人数


select case when age>1 and age <26  then 1
 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的值区分该条记录是属于哪个年龄段

原创粉丝点击