-- 如果你的性别与工资级别的统计不重叠的话,试试这样写
select dept,count(*),
count(decode(sex,'male',1,0)) male,
count(decode(sex,'female',1,0)) female,
count(decode(floor(salary/1000),1,1,0)) above1000,
count(decode(floor(salary/1000),2,1,0)) above2000,
count(decode(floor(salary/1000),3,1,0)) above3000,
count(decode(floor(salary/4000),0,0,1)  above4000
from your_table
group by dept;

解决方案 »

  1.   

    首先界定:
    above1000为:1000-1999;
    above2000为:2000-2999;
    above3000为:3000-3999;
    above4000为:4000->
    用单句sql可以这样写:select dept, count(emp) total, 
    sum(decode(sex,'male',1,0)) male, 
    sum(decode(sex,'female',1,0)) female, 
    sum(decode(sign(salary-1000),-1,0,1)*decode(sign(salary-2000),-1,1,0)) above1000, 
    sum(decode(sign(salary-2000),-1,0,1)*decode(sign(salary-3000),-1,1,0)) above2000, 
    sum(decode(sign(salary-3000),-1,0,1)*decode(sign(salary-4000),-1,1,0)) above3000, 
    sum(decode(sign(salary-4000),-1,0,1) above4000
    from table group by dept随手写的,你看看能不能用。。
      

  2.   

    orakiv(殷商人)兄的比较简洁,不过有点错。。
    male, female, above1000, above2000, above3000, above4000四个字段的聚组不能用count,要用sum。。
      

  3.   

    select dept,count(*) as total,
       sum(case when sex = 'male' then 1 else 0 end) as male,
       sum(case when sex='female' then 1 else 0 end) as female,
       sum(case when salary >= 1000 and salary < 2000 then 1 else 0 end) as above1000,
       sum(case when salary >= 2000 and salary < 3000 then 1 else 0 end) as above2000,
       sum(case when salary >= 3000 and salary < 4000 then 1 else 0 end) as above3000,
       sum(case when salary >= 4000 then 1 else 0 end) as above4000
    from yourtable
    group by dept;
      

  4.   

    case 的写法必须要815以上的版本才支持!
    decode没有这个限制
      

  5.   

    decode应当还是比case好的,但是不支持>,<
    有必要的时候用case也无妨,对速度几乎不会有影响