Chinaunix首页 | 论坛 | 博客
  • 博客访问: 393109
  • 博文数量: 85
  • 博客积分: 1416
  • 博客等级: 上尉
  • 技术积分: 916
  • 用 户 组: 普通用户
  • 注册时间: 2007-10-03 20:35
文章分类
文章存档

2014年(5)

2012年(2)

2010年(3)

2009年(67)

2008年(8)

分类: Oracle

2009-07-23 16:20:24

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


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));
阅读(2703) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~