SQL server 语句根据出生日期每5岁一个段,查询出相应的人数,如年龄段内无数据,则不显示.仅用查询语句完成
的有关信息介绍如下:select ageRange, countfrom( select ageRange = case when birth between dateadd(yyyy,-5,getdate()) and getdate() then '0-5' when birth between dateadd(yyyy,-10,getdate()) and dateadd(yyyy,-5,getdate()) then '5-10' when birth between dateadd(yyyy,-15,getdate()) and dateadd(yyyy,-10,getdate()) then '10-15' when birth between dateadd(yyyy,-20,getdate()) and dateadd(yyyy,-15,getdate()) then '15-20' when birth between dateadd(yyyy,-25,getdate()) and dateadd(yyyy,-20,getdate()) then '20-25' when birth between dateadd(yyyy,-30,getdate()) and dateadd(yyyy,-25,getdate()) then '25-30' when birth between dateadd(yyyy,-35,getdate()) and dateadd(yyyy,-30,getdate()) then '30-35' when birth between dateadd(yyyy,-40,getdate()) and dateadd(yyyy,-35,getdate()) then '35-40' when birth between dateadd(yyyy,-45,getdate()) and dateadd(yyyy,-40,getdate()) then '40-45' when birth between dateadd(yyyy,-50,getdate()) and dateadd(yyyy,-45,getdate()) then '45-50' when birth between dateadd(yyyy,-55,getdate()) and dateadd(yyyy,-50,getdate()) then '50-55' when birth between dateadd(yyyy,-60,getdate()) and dateadd(yyyy,-55,getdate()) then '55-60' when birth between dateadd(yyyy,-65,getdate()) and dateadd(yyyy,-60,getdate()) then '60-65' when birth between dateadd(yyyy,-70,getdate()) and dateadd(yyyy,-65,getdate()) then '65-70' when birth between dateadd(yyyy,-75,getdate()) and dateadd(yyyy,-70,getdate()) then '70-75' when birth between dateadd(yyyy,-80,getdate()) and dateadd(yyyy,-75,getdate()) then '75-80' end, count(ID) as countRange from person) a where countRange > 0group by ageRange