如何得出每个部门的人数????

billlyh 2012-08-02 09:09:04
如何得出每个部门的人数????
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
...全文
293 18 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
billlyh 2012-08-13
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
SQL code

--重復帖?
select chr_cl1,count(distinct(chr_cl2)) 人数
from zd_report_temp
group by chr_cl1;
[/Quote]
正解
billlyh 2012-08-13
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
SQL code

--重復帖?
select chr_cl1,count(distinct(chr_cl2)) 人数
from zd_report_temp
group by chr_cl1;
[/Quote]
正解
billlyh 2012-08-13
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
SQL code

--重復帖?
select chr_cl1,count(distinct(chr_cl2)) 人数
from zd_report_temp
group by chr_cl1;
[/Quote]
正解
huangdh12 2012-08-07
  • 打赏
  • 举报
回复
简单的group by用法。
sohome 2012-08-07
  • 打赏
  • 举报
回复
group by一下即可
Qiao0328 2012-08-07
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
SQL code

--重復帖?
select chr_cl1,count(distinct(chr_cl2)) 人数
from zd_report_temp
group by chr_cl1;
[/Quote]++
billlyh 2012-08-06
  • 打赏
  • 举报
回复
正解,谢谢
billlyh 2012-08-06
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]
SQL code

--创建部门表
create table department
(
chr_cl1 varchar2(100),
chr_cl2 varchar2(100)
)
--添加测试数据
insert into department
select 'A1供应部','罗杰泷' from dual union all
select 'A1供应部','罗杰泷' fro……
[/Quote]
正解,谢谢
billlyh 2012-08-06
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
SQL code

--重復帖?
select chr_cl1,count(distinct(chr_cl2)) 人数
from zd_report_temp
group by chr_cl1;
[/Quote]
正解,谢谢
jinfreaks 2012-08-04
  • 打赏
  • 举报
回复
对不起,多了一个括号
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
jinfreaks 2012-08-04
  • 打赏
  • 举报
回复
我觉得以下代码可能符合你的要求:


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

pengpeng409 2012-08-04
  • 打赏
  • 举报
回复
select chr_cl1,count(distinct chr_cl2)
from test
group by chr_cl1
  • 打赏
  • 举报
回复

--创建部门表
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
fangxiaoni893436 2012-08-03
  • 打赏
  • 举报
回复
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;
fangxiaoni893436 2012-08-03
  • 打赏
  • 举报
回复
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
人生无悔 2012-08-02
  • 打赏
  • 举报
回复

--重復帖?
select chr_cl1,count(distinct(chr_cl2)) 人数
from zd_report_temp
group by chr_cl1;
睿智天成 2012-08-02
  • 打赏
  • 举报
回复
以上就能够实现LZ所需的结果

17,382

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧