查找在 2 个以上项目中一起工作的员工人数

发布于 2025-01-11 19:57:58 字数 1672 浏览 0 评论 0原文

现在我们有3张表,分别是employees、workson、project。

对于 employees 表(包含示例数据)

employeeidnamesex
100JohnM
101JimM
102SamF
103QuinnF
400JaneF
401MaryF

对于 workson 表,我们有

员工ID 项目 ID
1014554
1024554
1034554
1044554
1014555
1024555
4014555
1014556
1024556
4014556

对于projects

projectidprojectName
4556Zombies
4555Umbrella Corp
4554Evil

根据数据集,应该清楚的是,只有在超过两个项目是吉姆和萨姆。因此,这应该是预期的结果,即 2。

然而,我自己的代码似乎返回每个员工参与过的项目数,并检索了 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 ;

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

For the employees table (with sample data)

employeeidnamegender
100JohnM
101JimM
102SamF
103QuinnF
400JaneF
401MaryF

For the workson table we have

employeeidprojectid
1014554
1024554
1034554
1044554
1014555
1024555
4014555
1014556
1024556
4014556

For the projects table

projectidprojectName
4556Zombies
4555Umbrella Corp
4554Evil

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 ;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

未央 2025-01-18 19:57:58

您需要将 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) <-> (山姆、吉姆、邪恶) 不包括在内。

结果包含成对的员工以及他们一起工作的项目数。添加 having 子句很简单。

员工 ID员工 ID一起工作
1011023
1014012
1024012
1011031
1021031
1011041
1021041
1031041

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.

employeeidemployeeidworked_together
1011023
1014012
1024012
1011031
1021031
1011041
1021041
1031041
清晰传感 2025-01-18 19:57:58

您需要将表 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;

Fiddle< /a>

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文