当前位置: 代码迷 >> 报表 >> oracle 数据库排序加统计
  详细解决方案

oracle 数据库排序加统计

热度:273   发布时间:2016-05-05 08:06:43.0
求助 oracle 数据库排序加统计
各位高手:
 
现在遇到要统计各个年龄段性别统计情况。想通过一个sql语句全部得到 如下写法 好像不对 请各位赐教, 谢谢
select sex,count(*) 
from tb_report_staff
where (age>='20'and age<='30') or (age>'30'and age<='40') or (age>'40'and age<='50')
group by sex

表结构
create table TB_REPORT_STAFF
(STAFFSTATISTICS_ID NUMBER not null,
  SEX VARCHAR2(5),
  AGE VARCHAR2(5)
)

------解决方案--------------------
SQL code
--方法1Select sex,Count(1)  num1,'20~30' desc1 from TB_REPORT_STAFFWhere age>='20'and age<='30'Group By sexUnion allSelect sex,Count(1)  num1,'30~40' desc1 from TB_REPORT_STAFFWhere age>'30'and age<='40'Group By sexunion allSelect sex,Count(1)  num1,'40~50' desc1 from TB_REPORT_STAFFWhere age>'40'and age<='50'Group By sex;Selectx.Sex,x.AGE1,Count(1) as Num1From(SelectSTAFFSTATISTICS_ID,Sex,CaseWhen age>='20'and age<='30' Then'20~30'When age>'30'and age<='40' Then'30~40'WHEN age>'40'and age<='50' THEN'40~50'ELSE'其他'EndAGE1From TB_REPORT_STAFF) xGroup Byx.Sex,x.Age1
------解决方案--------------------
1是指查询中的第一个字段
跟Count(某字段)类似

  相关解决方案