列出拥有最多员工的项目的部门名称

时间:2012-11-29 03:10:09

标签: sql oracle11g

因此,本声明中有三个表适用。分区表,其中包含部门名称和部门ID,包含项目的工作表和与项目相关的员工ID,以及包含员工ID,部门ID和名称的员工表。我正在努力找到拥有最多员工的部门。

这是我的代码:

select distinct 
    (dname) as "Division Name"
from 
    employee e, division d
where 
    d.did = e.did and 
    d.did in (
        select did from employee where empid in (
            select empid from workon having count(pid) >= all(pid)
        )
    )

我应该得到“人力资源”的答案,但无论我使用什么代码,我似乎无法得到答案。

Workon table

PID EMPID   HOURS
3   1   30
2   3   40
5   4   30
6   6   60
4   3   70
2   4   45
5   3   90
3   3   100
6   8   30
4   4   30
5   8   30
6   7   30
6   9   40
5   9   50
4   6   45
2   7   30
2   8   30
2   9   30
1   9   30
1   8   30
1   7   30
1   5   30
1   6   30
2   6   30

Employee Table

EMPID   NAME    SALARY  DID
1   kevin   32000   2
2   joan    42000   1
3   brian   37000   3
4   larry   82000   5
5   harry   92000   4
6   peter   45000   2
7   peter   68000   3
8   smith   39000   4
9   chen    71000   1
10  kim 46000   5
11  smith   46000   1

Division
DID DNAME   MANAGERID
1   engineering 2
2   marketing   1
3   human resource  3
4   Research and development    5
5   accounting  4

1 个答案:

答案 0 :(得分:1)

请查看此参考资料。

SQLFIDDLE

select d.id, d.name, p.maxcounts
from dept d, 
(select we.dep, max(we.counts) as maxcounts 
 from (select w.eid, count(w.pid) as counts, 
 e.dep as dep from employee e, workon w
where e.id = w.eid
group by e.dep) as we) as p
where d.id = p.dep
;

结果:

ID      NAME                MAXCOUNTS
111     human resoruces     5

以下是基于您自己的数据的编辑:

参考:SQLFIDDLE_Based_ON_OP_Data

有三种方法可以实现这一目标。使用嵌套选择,将Max(count)保存到变量中或通过desc命令数据并将其限制为1.

方法1:

- 使用嵌套选择

- 子查询1 向OP解释如何推导出最终答案

select e.dep, count(w.eid) as num_emp
from employee e, workon w
where e.id = w.eid
group by e.dep
order by e.dep
;
-- **results of sub query 1:**

DEP     NUM_EMP
1           4
2           5
3           7
4           5
5           3

- 最终嵌套选择查询

select ee.dep, dd.name, count(ww.eid)
from employee ee, dept dd, workon ww
where ee.id = ww.eid
and ee.dep = dd.id
group by ee.dep, dd.name
having count(ww.eid) = 
(select distinct max(t.num_emp)
from (select e.dep, count(w.eid) as num_emp
from employee e, workon w
where e.id = w.eid
group by e.dep
order by e.dep)as t)
;

- 使用嵌套选择的结果

DEP     NAME            COUNT(WW.EID)
3       human resource  7

- 使用变量查询

select max(x.num_emp) into @myvar from 
(select e.dep, count(w.eid) as num_emp
from employee e, workon w
where e.id = w.eid
group by e.dep) as x
;


select x.dep, x.name, x.num_emp as num_emp from 
(select e.dep, d.name, count(w.pid) as num_emp
from employee e, workon w, dept d
where e.id = w.eid
and e.dep = d.id
group by e.dep) as x
where x.num_emp = @myvar
;

- 使用变量的结果

DEP     NAME              NUM_EMP
3       human resource    7

- 使用带有排序的desc表的限制1进行查询

select e.dep, d.name, count(w.eid) as num_emp
from employee e, workon w, dept d
where e.id = w.eid
and e.dep = d.id
group by e.dep
order by num_emp desc 
limit 1

- 使用desc和限制1的订单结果:

DEP     NAME              NUM_EMP
3       human resource    7

现在,在使用方法3时,您可能会或者可能无关紧要,有时会有两个部门在项目中工作的员工人数相同。因此,在这种情况下,您可以使用嵌套或可变方法。

* PS我没有权利在StackOverFlow上全职工作,因此很抱歉回到你身边:) *