简体   繁体   中英

Find common employees between departments

I'm using MySQL. I have a table called works(EID,DID,PCT_TIME) and PCT_TIME is not used here.

The question I have is: Find the two departments ( represented by DID) who have the highest number of employees ( he's represented by EID) in common.

I've tried that:

select count(*) as max_NBR_COMMUN from (select EID from WORKS group by EID having count(EID) >= 2) as temp

to get the number of employees common to two departments but it doesn't work. EDIT: I've seen your answer and I thank you all, I'm a business student so not familiar with computer science and therefore sql. So my final request is

select count(distinct wa.eid) as num_eids_in_common, wa.did, wb.did 
from works wa 
join works wb on wa.eid = wb.eid and wa.did < wb.did 
group by wa.did, wb.did 
order by num_eids_in_common desc 
limit 1

Let's start with this.

create table works ( eid int not null, did int not null );
insert into works values (1,1), (1,1), (1,2), (1,3), (2,1), (2,2), (3,1), (4,4);
select * from works order by eid;
eid         did       
----------  ----------
1           1         
1           1         
1           2         
1           3         
2           1         
2           2         
3           1         
4           4   

Note that employee 1 has multiple entries in department 1, a curve ball.

To find out which employees are in more than one department we need to join works with itself, a self-join . This is like any other join, but table aliases are mandatory.

select wa.eid, wa.did, wb.did
from works wa
join works wb on wa.eid = wb.eid
 and wa.did != wb.did;

This joins with any row that has the same employee ID but different department ID, because we don't want to see employees in our own departments.

eid         did         did       
----------  ----------  ----------
1           1           2         
1           1           3         
1           1           2         
1           1           3         
1           2           1         
1           2           1         
1           2           3         
1           3           1         
1           3           1         
1           3           2         
2           1           2      
2           2           1            

Employee 1 shows up multiples times because it has multiple entries in works , remember the curve ball? We can remove these duplicates by selecting only the distinct rows .

select distinct wa.eid, wa.did, wb.did
from works wa
join works wb on wa.eid = wb.eid
 and wa.did != wb.did;

eid         did         did       
----------  ----------  ----------
1           1           2         
1           1           3         
1           2           1         
1           2           3         
1           3           1         
1           3           2         
2           1           2         
2           2           1         

We can also avoid counting each department pair twice by joining with wa.did < wb.did instead of wa.did != wb.did .

select distinct wa.eid, wa.did, wb.did
from works wa
join works wb on wa.eid = wb.eid
 and wa.did < wb.did;

eid         did         did       
----------  ----------  ----------
1           1           2         
1           1           3         
1           2           3         
2           1           2         

Now we have a list of all the employees who are in more than one department, and their departments.

We get the number of employees in common by grouping by both department IDs and counting the employees.

select count(distinct wa.eid) as num_eids_in_common,
  wa.did, wb.did
from works wa
join works wb on wa.eid = wb.eid
 and wa.did < wb.did
group by wa.did, wb.did;

num_eids_in_common  did         did       
------------------  ----------  ----------
1                   1           3         
1                   2           3         
2                   1           2         

Finally we get the one with the most in common by sorting num_eids_in_common in descending order and limiting it to 1 row.

select count(distinct wa.eid) as num_eids_in_common,
  wa.did, wb.did
from works wa
join works wb on wa.eid = wb.eid
 and wa.did < wb.did
group by wa.did, wb.did
order by num_eids_in_common desc
limit 1;

num_eids_in_common  did         did       
------------------  ----------  ----------
2                   1           2         

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM