查询拥有最多的员工的部门的基本信息

2801阅读 0评论2009-07-23 Ophelia_cu
分类:Oracle

查询拥有最多的员工的部门的基本信息(要求只取出一个部门的信息),
如果有多个部门人数一样,那么取出部门编号最小的那个部门的基本信息。


select * from dept_table c,
(
select a.dept_id,a.num
from
(select count(*) num,dept_id from emp_table group by dept_id)
a
where rownum=1
order by a.num desc,a.dept_id
) b
where b.dept_id=c.dept_id




第二种做法

create view vw_maxemp(did,empno)

as select dept_id,count(*)

from emp_table

group by dept_id;




             
select * from dept_table a,

(
select did from vw_maxemp where rownum=1 order by empno desc,did      
) b

where a.dept_id=b.did

第三种做法
建立视图

create view vw_maxemp(did,empno)

as select did,count(*)

from work

group by did;



select * from dept

where did in(select min(did)

              from vw_maxemp

              where empno=(select max(empno) from vw_maxemp));
上一篇:使用SQL Server数据库嵌套子查询
下一篇:Oracle中INSTR,SUBSTR方法