如何得出每个部门的人数????
select语句得到结果如下:
chr_cl1                 chr_cl2
A1供应部                罗杰泷  
A1供应部                罗杰泷
A1供应部                罗杰泷
A1供应部                张国
C1系统支持              谢燕山  
C1系统支持              谢燕山 
上面的结果是A1供应部有两个人,C1系统支持有一个人
但是我想按人名来统计每个部门的人数,希望得到的结果是
chr_cl1                 人数
A1供应部                2  
C1系统支持              1  
试了好几次,都试不出来,哪位兄弟帮帮忙啊?????????????
sql语句如下:
select  t.chr_cl1,t.chr_cl2
  from zd_report_temp t
 where t.program_name in('INVGSTOT')
 order by t.chr_cl1

解决方案 »

  1.   


    --重復帖?
    select chr_cl1,count(distinct(chr_cl2)) 人数
    from zd_report_temp 
    group by chr_cl1;
      

  2.   

    select t.chr_cl1,count(distinct t.chr_cl2)
     from zd_report_temp t
      where t.program_name in('INVGSTOT')
       group by t.chr_cl1
      

  3.   

    select t.chr_cl1,count(distinct t.chr_cl2)
     from zd_report_temp t
      where t.program_name in('INVGSTOT')
       group by t.chr_cl1;
      

  4.   


    --创建部门表
    create table department
    (
    chr_cl1 varchar2(100),
    chr_cl2 varchar2(100)
    )
    --添加测试数据
    insert into department
    select 'A1供应部','罗杰泷' from dual  union all
    select 'A1供应部','罗杰泷' from dual  union all
    select 'A1供应部','罗杰泷' from dual  union all
    select 'A1供应部','张国' from dual  union all
    select 'C1系统支持','谢燕山' from dual  union all   
    select 'C1系统支持','谢燕山'  from dual
    commit;
    --部门人数统计
    select chr_cl1 as 部门,count(distinct chr_cl2) as 人数 from department group by chr_cl1--查询结果
    A1供应部   2
    C1系统支持 1
      

  5.   

    select chr_cl1,count(distinct chr_cl2) 
    from test
    group by chr_cl1
      

  6.   

    我觉得以下代码可能符合你的要求:
    select t.chr_cl1,count(t.chr_cl2) from 
      (select distinct chr_cl1,chr_cl2) from zd_report_temp
         where program_name in('INVGSTOT')
    ) t
    group by chr_cl1
    order by chr_cl1
      

  7.   

    对不起,多了一个括号
    select t.chr_cl1,count(t.chr_cl2) from 
      (select distinct chr_cl1,chr_cl2 from zd_report_temp
         where program_name in('INVGSTOT')
    ) t
    group by chr_cl1
    order by chr_cl1