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

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


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的值区分该条记录是属于哪个年龄段

你可能感兴趣的:(sqlserver,sqlserver,sqlserver,sqlserver)