查找在 2 个以上项目中一起工作的员工数
Find the number of employees who worked together on more than 2 projects
现在我们有 3 个 table,分别是 employees、workson、project。
对于员工 table(带有示例数据)
employeeid
name
gender
100
John
M
101
Jim
M
102
Sam
F
103
Quinn
F
400
Jane
F
401
Mary
F
对于 workson table 我们有
employeeid
projectid
101
4554
102
4554
103
4554
104
4554
101
4555
102
4555
401
4555
101
4556
102
4556
401
4556
对于项目 table
projectid
projectName
4556
Zombies
4555
Umbrella Corp
4554
Evil
根据数据集,应该清楚的是,在 2 个以上的项目中一起工作的员工只有 Jim 和 Sam。因此,这应该是预期的结果,即 2.
然而,我自己的代码似乎 return 每个员工从事过的项目数量,并检索了 3000+++ 行(每个员工)。当输出应该只是一个简单的整数时。
SELECT COUNT(e.employeeid)
FROM employees e
JOIN workson w ON e.employeeid = w.employeeid
GROUP BY e.employeeid
HAVING COUNT(w.projectid) > 9 ;
您需要加入 table workson
自身:
with u as
(select w1.employeeid e1, w2.employeeid e2, count(*) ct
from workson w1 inner join workson w2
on w1.projectid = w2.projectid
and w1.employeeid < w2.employeeid
group by w1.employeeid, w2.employeeid
having count(*) > 2),
v as
(select distinct e1 from u union select distinct e2 from u)
select count(*) from v;
您需要像这样加入 workson
自身:
select e1.employeeid, e2.employeeid, count(e1.projectid) as worked_together
from workson as e1
join workson as e2 on e1.projectid = e2.projectid and e1.employeeid < e2.employeeid
group by e1.employeeid, e2.employeeid
order by worked_together desc
<
条件确保不包括 (Jim, Jim, Evil)
等行和 (Jim, Sam, Evil) <-> (Sam, Jim, Evil)
等相反的行。
结果包含成对的员工以及他们一起工作的项目数。添加 having
子句是微不足道的。
employeeid
employeeid
worked_together
101
102
3
101
401
2
102
401
2
101
103
1
102
103
1
101
104
1
102
104
1
103
104
1
现在我们有 3 个 table,分别是 employees、workson、project。
对于员工 table(带有示例数据)
employeeid | name | gender |
---|---|---|
100 | John | M |
101 | Jim | M |
102 | Sam | F |
103 | Quinn | F |
400 | Jane | F |
401 | Mary | F |
对于 workson table 我们有
employeeid | projectid |
---|---|
101 | 4554 |
102 | 4554 |
103 | 4554 |
104 | 4554 |
101 | 4555 |
102 | 4555 |
401 | 4555 |
101 | 4556 |
102 | 4556 |
401 | 4556 |
对于项目 table
projectid | projectName |
---|---|
4556 | Zombies |
4555 | Umbrella Corp |
4554 | Evil |
根据数据集,应该清楚的是,在 2 个以上的项目中一起工作的员工只有 Jim 和 Sam。因此,这应该是预期的结果,即 2.
然而,我自己的代码似乎 return 每个员工从事过的项目数量,并检索了 3000+++ 行(每个员工)。当输出应该只是一个简单的整数时。
SELECT COUNT(e.employeeid)
FROM employees e
JOIN workson w ON e.employeeid = w.employeeid
GROUP BY e.employeeid
HAVING COUNT(w.projectid) > 9 ;
您需要加入 table workson
自身:
with u as
(select w1.employeeid e1, w2.employeeid e2, count(*) ct
from workson w1 inner join workson w2
on w1.projectid = w2.projectid
and w1.employeeid < w2.employeeid
group by w1.employeeid, w2.employeeid
having count(*) > 2),
v as
(select distinct e1 from u union select distinct e2 from u)
select count(*) from v;
您需要像这样加入 workson
自身:
select e1.employeeid, e2.employeeid, count(e1.projectid) as worked_together
from workson as e1
join workson as e2 on e1.projectid = e2.projectid and e1.employeeid < e2.employeeid
group by e1.employeeid, e2.employeeid
order by worked_together desc
<
条件确保不包括 (Jim, Jim, Evil)
等行和 (Jim, Sam, Evil) <-> (Sam, Jim, Evil)
等相反的行。
结果包含成对的员工以及他们一起工作的项目数。添加 having
子句是微不足道的。
employeeid | employeeid | worked_together |
---|---|---|
101 | 102 | 3 |
101 | 401 | 2 |
102 | 401 | 2 |
101 | 103 | 1 |
102 | 103 | 1 |
101 | 104 | 1 |
102 | 104 | 1 |
103 | 104 | 1 |