sql 区分各个年龄段 人数 并且区分男女

[color=green][size=xx-large]将多条数据 以 横向 年龄段,竖向 性别 查询出各个年龄段的人数 报表[/size][/color]

要求结果如下格式:

性别 20岁以下 20岁~25岁 26岁~30岁 31岁~35岁 36岁~40岁 41岁~45岁



sql语句如下:

select (CASE GEND_CD WHEN 'MALE' THEN '男' WHEN 'FEML' THEN '女' ELSE '其它' END)"性别",count(CASE WHEN substr(BIRT_YMD,0,4) BETWEEN '1996' and '2015' THEN '20岁以下' ELSE null END)"20岁以下",
count(CASE WHEN substr(BIRT_YMD,0,4) BETWEEN '1991' and '1995' THEN '21岁~25岁' ELSE null END)"20岁~25岁",
count(CASE WHEN substr(BIRT_YMD,0,4) BETWEEN '1986' and '1990' THEN '26岁~30岁' ELSE null END)"26岁~30岁",
count(CASE WHEN substr(BIRT_YMD,0,4) BETWEEN '1981' and '1985' THEN '31岁~35岁' ELSE null END)"31岁~35岁",
count(CASE WHEN substr(BIRT_YMD,0,4) BETWEEN '1976' and '1980' THEN '36岁~40岁' ELSE null END)"36岁~40岁",
count(CASE WHEN substr(BIRT_YMD,0,4) BETWEEN '1971' and '1975' THEN '41岁~45岁' ELSE null END)"41岁~45岁"
from table_name where GEND_CD in('MALE','FEML')GROUP BY GEND_CD;

你可能感兴趣的:(oracle数据库,Oracle,sql,SQL,Server)