with emp as(
select '张丰' 教师,'教授' 职称,55 年龄 from dual union all
select '王郅' 教师,'副教授' 职称,44 年龄 from dual union all
select '李娜' 教师,'讲师'职称,42 年龄 from dual union all
select '张贴' 教师,'助教' 职称,38 年龄 from dual union all
select '李自成' 教师,'讲师' 职称,33 年龄 from dual union allselect '吴宇川' 教师,'教授' 职称,55 年龄 from dual union all
select '高希希' 教师,'副教授' 职称,34 年龄 from dual union all
select '邓菲' 教师,'讲师' 职称,42 年龄 from dual union all
select '方舟子' 教师,'助教' 职称,38 年龄 from dual union all
select '王一定' 教师,'讲师' 职称,33 年龄 from dual )要如下统计结果
不知道怎么处理 help~~ 51-60 41-50 31-40 21-30
教授
副教授
讲师
助教
小计

解决方案 »

  1.   

    select 教师 , 
           sum(case when 年龄 between 51 and 60 then 1 else 0 end) '51-60',
           sum(case when 年龄 between 41 and 50 then 1 else 0 end) '41-50',
           sum(case when 年龄 between 31 and 40 then 1 else 0 end) '31-40',
           sum(case when 年龄 between 21 and 20 then 1 else 0 end) '21-30'
    from emp
    group by 教师
    union all
    select '小计' 教师 , 
           sum(case when 年龄 between 51 and 60 then 1 else 0 end) '51-60',
           sum(case when 年龄 between 41 and 50 then 1 else 0 end) '41-50',
           sum(case when 年龄 between 31 and 40 then 1 else 0 end) '31-40',
           sum(case when 年龄 between 21 and 20 then 1 else 0 end) '21-30'
    from emp
      

  2.   

    select 职称
           ,count(case when 年龄 between  51 and 60 then 教师 end) "51-60"
           ,count(case when 年龄 between  41 and 50 then 教师 end) "41-50"
           ,count(case when 年龄 between  31 and 40 then 教师 end) "31-40"
           ,count(case when 年龄 between  21 and 30 then 教师 end) "21-30"
    from tmp
    group by 职称;
      

  3.   

    小计
    加个cubeselect 职称
           ,count(case when 年龄 between  51 and 60 then 教师 end) "51-60"
           ,count(case when 年龄 between  41 and 50 then 教师 end) "41-50"
           ,count(case when 年龄 between  31 and 40 then 教师 end) "31-40"
           ,count(case when 年龄 between  21 and 30 then 教师 end) "21-30"
    from tmp
    group by cube(职称);
      

  4.   

    不知道sum里面还可以用条件 书都白看了 呵呵
      

  5.   

      1  with emp as(
      2  select '张丰' 教师,'教授' 职称,55 年龄 from dual union all
      3  select '王郅' 教师,'副教授' 职称,44 年龄 from dual union all
      4  select '李娜' 教师,'讲师'职称,42 年龄 from dual union all
      5  select '张贴' 教师,'助教' 职称,38 年龄 from dual union all
      6  select '李自成' 教师,'讲师' 职称,33 年龄 from dual union all
      7  select '吴宇川' 教师,'教授' 职称,55 年龄 from dual union all
      8  select '高希希' 教师,'副教授' 职称,34 年龄 from dual union all
      9  select '邓菲' 教师,'讲师' 职称,42 年龄 from dual union all
     10  select '方舟子' 教师,'助教' 职称,38 年龄 from dual union all
     11  select '王一定' 教师,'讲师' 职称,33 年龄 from dual )
     12  select decode(grouping(职称),1,'小计',职称) "职称",
     13  count(case when 年龄 between 51 and 60 then 教师 end) "51-60",
     14  count(case when 年龄 between 41 and 50 then 教师 end) "41-50",
     15  count(case when 年龄 between 31 and 40 then 教师 end) "31-40",
     16  count(case when 年龄 between 21 and 30 then 教师 end) "21-30"
     17  from emp
     18* group by rollup(职称)
    SQL> /职称        51-60      41-50      31-40      21-30
    ------ ---------- ---------- ---------- ----------
    副教授          0          1          1          0
    讲师            0          2          2          0
    教授            2          0          0          0
    助教            0          0          2          0
    小计            2          3          5          0
      

  6.   

    结完贴不能加分了,抱歉canhui87