我有3个表Employee
,Project
和Assignment
。可以将员工分配到多个项目,一个项目可以有多个员工。 Assignment
表保持员工与项目之间的多对多关系。我想在sql server中进行sql查询,返回同时处理过多个项目的员工列表。
表格的结构
表员工
EmpId,
EmpName
表项目
ProjId,ProjName,ProjStartDate,ProjEndDate
分配
AssignmentId,ProjId,EmpId,AssStartDate,AssEndDate
答案 0 :(得分:1)
Select EmpName,zt.* from
(
Select a.EmpId,Count(*) as cnt
from Assignment a
Join Assignment b on a.EmpId=b.EmpId and a.ProjId<>b.ProjId
and
(
(a.AssStartDate>=b.AssStartDate and a.AssStartDate<=b.AssEndDate)
or
(a.AssEndDate>=b.AssStartDate and a.AssEndDate<=b.AssEndDate)
or
(b.AssStartDate>=a.AssStartDate and b.AssStartDate<=a.AssEndDate)
or
(b.AssEndDate>=a.AssStartDate and b.AssEndDate<=a.AssEndDate)
)
Group by a.EmpId
Having Count(*)>1
) zt
join Employee e on e.EmpId=zt.EmpId
答案 1 :(得分:0)
Select a.EmpId from Assignment a join Assignment b on
a.EmpId = b.EmpId
and
a.AssStartDate < b.AssEndDate
and
a.AssEndDate > b.AssStartDate