T-Sql 根据出生日期按年龄段统计

 select nnd as age ,count(*) as num from
(
 select
 case
  when to_char(sysdate,'yyyy')-to_char(REC_USER_BIRTHDAY,'yyyy') >= '0' and to_char(sysdate,'yyyy')-to_char(REC_USER_BIRTHDAY,'yyyy') <= '10' then '10岁以下'
  when to_char(sysdate,'yyyy')-to_char(REC_USER_BIRTHDAY,'yyyy') >= '11' and to_char(sysdate,'yyyy')-to_char(REC_USER_BIRTHDAY,'yyyy')<='20' then '11-20'
  when to_char(sysdate,'yyyy')-to_char(REC_USER_BIRTHDAY,'yyyy') >= '21' and to_char(sysdate,'yyyy')-to_char(REC_USER_BIRTHDAY,'yyyy')<='30' then '21-30'
  when to_char(sysdate,'yyyy')-to_char(REC_USER_BIRTHDAY,'yyyy') >= '31' and to_char(sysdate,'yyyy')-to_char(REC_USER_BIRTHDAY,'yyyy')<='40' then '31-40'
  when to_char(sysdate,'yyyy')-to_char(REC_USER_BIRTHDAY,'yyyy') >= '41' and to_char(sysdate,'yyyy')-to_char(REC_USER_BIRTHDAY,'yyyy')<='50' then '41-50'
  when to_char(sysdate,'yyyy')-to_char(REC_USER_BIRTHDAY,'yyyy') >= '51' and to_char(sysdate,'yyyy')-to_char(REC_USER_BIRTHDAY,'yyyy')<='60' then '51-60'
  when to_char(sysdate,'yyyy')-to_char(REC_USER_BIRTHDAY,'yyyy') >'60'  then '60以上'
 end
 as nnd,REC_USER_NAME from QEFOA_RECORD
)
a
group by nnd

你可能感兴趣的:(T-Sql 根据出生日期按年龄段统计)