1

したがって、このステートメントに適用できる表は 3 つあります。部門名と部門 ID を格納する部門テーブル、プロジェクトとプロジェクトに関連する従業員 ID を格納する workon テーブル、および従業員 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
4

1 に答える 1

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

これを実現する方法は 3 つあります。ネストされた選択を使用するか、Max(count) を変数に保存するか、データを降順で並べ替えて 1 に制限します。

方法 1:

-- ネストされた選択を使用

--最終的な回答がどのように導き出されるかを OP に説明するサブクエリ1

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

-- 順序付けされた DES テーブルで制限 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

-- 降順と制限 1 を使用した結果:

DEP     NAME              NUM_EMP
3       human resource    7

方法 3 を使用する場合、プロジェクトで働く従業員の数が同じである 2 つの部門が存在する場合があることは、問題になる場合と問題にならない場合があります。その場合、ネストされたメソッドまたは変数メソッドのいずれかを使用できます。

* PS 私は StackOverFlow にフルタイムで参加する特権を持っていません。

于 2012-11-29T04:11:15.160 に答える