无法限制一段数据出现多次的数据集

时间:2015-05-08 22:35:47

标签: sql join count subquery having

SELECT FirstName, LastName, [description], ProjectName, StartOnDate, FinishOnDate
FROM Assignments AS A
JOIN Projects AS P ON A.ProjectID = P.ProjectID
JOIN Positions AS PO ON A.PositionCode = Po.PositionCode
JOIN Employees AS E ON E.EmployeeID = A.EmployeeID

这些是我需要的列,但我只需要显示两次的行。具体来说,我只需要有多个任务的员工。

下一个代码给出了重复的结果,但我似乎无法加入其余的表cloumns我正在寻找的结果集。

SELECT A.EmployeeID
FROM Assignments AS A
GROUP BY A.EmployeeID 
HAVING COUNT(A.EmployeeID) > 1

2 个答案:

答案 0 :(得分:0)

代码将返回由where语句的子查询标识的每个员工的项目和位置。

SELECT FirstName, LastName, [description], ProjectName, StartOnDate, FinishOnDate
FROM Assignments AS A
JOIN Projects AS P ON A.ProjectID = P.ProjectID
JOIN Positions AS PO ON A.PositionCode = Po.PositionCode
JOIN Employees AS E ON E.EmployeeID = A.EmployeeID
WHERE EmployeeID in (SELECT A.EmployeeID
                     FROM Assignments AS A
                     GROUP BY A.EmployeeID 
                     HAVING COUNT(A.EmployeeID) > 1)

答案 1 :(得分:0)

如果您没有庞大的数据集,那么您可以通过艰难的方式实现目标:

Select EmployeeID, FirstName, LastName, [description], ProjectName, StartOnDate,         
 FinishOnDate from (
   SELECT EmployeeID, FirstName, LastName, [description], ProjectName, StartOnDate,      FinishOnDate
   FROM Assignments AS A
   JOIN Projects AS P ON A.ProjectID = P.ProjectID
JOIN Positions AS PO ON A.PositionCode = Po.PositionCode
JOIN Employees AS E ON E.EmployeeID = A.EmployeeID) B
group by GROUP B.EmployeeID 
HAVING COUNT(B.EmployeeID) > 1