寻找部门之间的普通员工

时间:2018-11-03 19:46:57

标签: mysql sql

我正在使用MySQL。我有一个名为works(EID,DID,PCT_TIME)的表,这里不使用PCT_TIME。

我的问题是:找到两个共有最多员工的部门(以DID代表)。

我尝试过:

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

获取两个部门共有的雇员人数,但这不起作用。 编辑:我已经看到了你的答案,谢谢大家,我是一名商科学生,所以不熟悉计算机科学,因此不熟悉sql。 所以我的最终要求是

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

1 个答案:

答案 0 :(得分:0)

让我们从这里开始。

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   

请注意,员工1在部门1(曲线球)中有多个条目。

要找出哪些员工属于多个部门,我们需要将works与自己的self-join一起加入。就像任何其他联接一样,但是表别名是必需的。

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

这与具有相同员工ID但具有不同部门ID的任何行合并,因为我们不想看到自己部门中的员工。

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            

员工1出现多次,因为它在works中有多个条目,还记得曲线球吗?我们可以通过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         

我们还可以通过与wa.did < wb.did而非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         

现在我们有了一个以上部门中所有雇员及其部门的列表。

我们通过grouping by两个部门ID并计算员工人数来获得共同的员工人数。

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         

最后,我们通过将num_eids_in_common降序排序并将其限制为1行来得到最共有的一个。

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