0

我有 3 张桌子Employee和. 一个员工可以分配到多个项目,一个项目可以有多个员工。表保持员工和项目之间的多对多关系。我想要一个 sql server 中的 sql 查询,它返回一个曾经同时从事过多个项目的员工列表。ProjectAssignmentAssignment

表的星形

表员工

EmpId,
EmpName

表项目

ProjId,ProjName,ProjStartDate,ProjEndDate

任务

AssignmentId,ProjId,EmpId,AssStartDate,AssEndDate
4

2 回答 2

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
于 2012-12-21T07:31:35.173 回答
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 
于 2012-12-21T07:36:21.797 回答