我正在研究Elmasri Employee DB。有关其详细信息,请参阅此文档document。 我已经获得了两个或更多项目及其各自位置的员工:
select w."Essn", p."Plocation"
from "Works_On" w, "Project" p, "Employee" e
where p."Pnumber" = w."Pno" and w."Essn" = e."Ssn"
and
w."Essn" in
(select w1."Essn"
from "Works_On" w1
group by w1."Essn"
having count(*) >= 2
)
我需要确定在两个或更多不同位置工作的员工。因此,987987987
和999887777
不符合条件,因为他们正在处理两个不同的项目,但位于同一位置。我提出的解决方案是根据员工Essn
和Plocation
属性对员工进行分组,并确定那些等于一的员工。更新的代码将是:
select base."Essn", base."Plocation"
from
(select w."Essn", p."Plocation"
from "Works_On" w, "Project" p, "Employee" e
where p."Pnumber" = w."Pno" and w."Essn" = e."Ssn"
and
w."Essn" in
(select w1."Essn"
from "Works_On" w1
group by w1."Essn"
having count(*) >= 2
)
) as base
group by base."Essn", base."Plocation"
having count(*) =1
然而,这显然不是真的,因为它会消除33344555 - Stafford
。
我的问题是:是否可以从给定的base
关系中确定那些在x
个不同位置工作的员工?
答案 0 :(得分:1)
为了找到在两个不同位置处理两个不同项目的员工,您可能只需要这个查询
select w."Essn"
from "Works_On" w
join "Project" p on p."Pnumber" = w."Pno"
group by w."Essn"
having count(distinct w."Pno") > 1 and count(distinct p."Plocation") > 1
并且像这样的查询也应该足够了
select w."Essn"
from "Works_On" w
join "Project" p on p."Pnumber" = w."Pno"
group by w."Essn"
having count(distinct p."Plocation") > 1