Home > Net >  Find the number of employees who worked together on more than 2 projects
Find the number of employees who worked together on more than 2 projects

Time:03-08

Now we have 3 tables which are employees, workson, project.

For the employees table (with sample data)

employeeid name gender
100 John M
101 Jim M
102 Sam F
103 Quinn F
400 Jane F
401 Mary F

For the workson table we have

employeeid projectid
101 4554
102 4554
103 4554
104 4554
101 4555
102 4555
401 4555
101 4556
102 4556
401 4556

For the projects table

projectid projectName
4556 Zombies
4555 Umbrella Corp
4554 Evil

Based on the dataset, it should be clear that the only employees who worked together on more than 2 projects are Jim and Sam. Hence that should be the expected outcome which is 2.

My own code however seems to return the number of projects that each employee had worked in and retrieved rows of 3000 (every single employee). When the output should only be a simple integer.

SELECT COUNT(e.employeeid)
FROM employees e
JOIN workson w ON e.employeeid = w.employeeid
GROUP BY e.employeeid
HAVING COUNT(w.projectid) > 9 ;

CodePudding user response:

You need to join the workson with itself like so:

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

The < condition ensures that rows such as (Jim, Jim, Evil) and the opposite pairs such as (Jim, Sam, Evil) <-> (Sam, Jim, Evil) are not included.

The result contains pairs of employees and the count of projects where they worked together. It is trivial to add a having clause.

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

CodePudding user response:

You need to join the table workson with itself:

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;

Fiddle

  • Related