一个group by例子
select
case
when (datediff(yy,csrq,getdate()) > 18) then '18岁及以上'
when (datediff(yy,csrq,getdate()) = 11) then '11岁'
when (datediff(yy,csrq,getdate()) = 12) then '12岁'
when (datediff(yy,csrq,getdate()) = 13) then '13岁'
when (datediff(yy,csrq,getdate()) = 14) then '14岁'
when (datediff(yy,csrq,getdate()) = 15) then '15岁'
when (datediff(yy,csrq,getdate()) = 16) then '16岁'
when (datediff(yy,csrq,getdate()) = 17) then '17岁'
else '10岁及以下'
end 'AgeStage',
count(*) as 'Num', xbm as Sex,
XSLBM as 'Class',MZM as 'Nationality',ZJXY as Boarder
from stu_studentinfo
where Dept_ID
in (select id from sys_departments where p_id
in (select id from sys_departments where p_id in (252,258,277,279)
and gradeclass = '初一年级'))
group by
case
when (datediff(yy,csrq,getdate()) > 18) then '18岁及以上'
when (datediff(yy,csrq,getdate()) = 11) then '11岁'
when (datediff(yy,csrq,getdate()) = 12) then '12岁'
when (datediff(yy,csrq,getdate()) = 13) then '13岁'
when (datediff(yy,csrq,getdate()) = 14) then '14岁'
when (datediff(yy,csrq,getdate()) = 15) then '15岁'
when (datediff(yy,csrq,getdate()) = 16) then '16岁'
when (datediff(yy,csrq,getdate()) = 17) then '17岁'
else '10岁及以下'
end, xbm,XSLBM,MZM,ZJXY