2017-02-13 146 views
0

我遇到了数据库问题。我已经使用2014年冒险作品复制了它。空白表结果

我想显示BusinessEntityID多次显示的所有结果。因此,如果用户是两个部门的成员,他们的ID将显示两次

但是,这是我与下面的查询得到的。 enter image description here

SELECT Person.FirstName, 
     Person.LastName, 
     HumanResources.Department.Name AS CurrentDepartment, 
     StartDate, 
     EndDate 
FROM AdventureWorks2014.Person.Person 
JOIN HumanResources.EmployeeDepartmentHistory 
    ON HumanResources.EmployeeDepartmentHistory.BusinessEntityID = Person.BusinessEntityID 
JOIN HumanResources.Department 
    ON EmployeeDepartmentHistory.DepartmentID = HumanResources.Department.DepartmentID 
GROUP BY Person.BusinessEntityID, 
     HumanResources.Department.DepartmentID, 
     Person.FirstName, 
     Person.LastName, 
     HumanResources.Department.Name, 
     StartDate, 
     EndDate 
HAVING COUNT(Person.BusinessEntityID) > 1 
ORDER BY Person.LastName, StartDate 

我删除了有我得到返回的结果(整个表)。所以我想我知道问题不在于它是什么/如何解决它。

+2

,请复制粘贴您的样本输入数据。它会帮助每个人轻松理解这个问题。 – Tajinder

+0

因此,基于该分组和那些数据,您期望查询返回什么结果? – HoneyBadger

+0

我期望它返回第一个屏幕截图,但我得到第二个 – Phil3992

回答

0

你需要做的单独分组,我想你真正想要的是人与EmployeeDepartmentHistory多条记录,例如

SELECT BusinessEntityID 
FROM HumanResources.EmployeeDepartmentHistory 
GROUP BY BusinessEntityID 
HAVING COUNT(*) > 1 

我觉得这个集成到您当前查询的最有效的方法是使用EXISTS

SELECT p.FirstName, 
     p.LastName, 
     d.Name AS CurrentDepartment, 
     edh.StartDate, 
     edh.EndDate 
FROM Person.Person AS p 
     JOIN HumanResources.EmployeeDepartmentHistory AS edh 
      ON edh.BusinessEntityID = p.BusinessEntityID 
     JOIN HumanResources.Department AS d 
      ON d.DepartmentID = edh.DepartmentID 
WHERE EXISTS 
     ( SELECT 1 
      FROM HumanResources.EmployeeDepartmentHistory AS edh2 
      WHERE edh2.BusinessEntityID = p.BusinessEntityID 
      HAVING COUNT(*) > 1 
     ) 
ORDER BY p.LastName, StartDate 
+0

这工作完美。投票其他答案的帮助 – Phil3992

1

进出口假设你的查询工作正常,如果你不包括组将通过将带来所有的员工。因此,你需要加入与员工的名单与+1部门

JOIN (SELECT P.BusinessEntityID --, COUNT(EDH.DepartmentID) for debug 
     FROM AdventureWorks2014.Person.Person P 
     JOIN HumanResources.EmployeeDepartmentHistory EDH 
     ON P.BusinessEntityID = EDH.BusinessEntityID 
     GROUP BY P.BusinessEntityID 
     HAVING COUNT(EDH.DepartmentID) > 1 
    ) as list_of_employees_with_two_or_more 
ON AdventureWorks2014.Person.Person.BusinessEntityID = 
    list_of_employees_with_two_or_more.BusinessEntityID 
1
WITH cte AS (
    SELECT Person.FirstName AS FirstName, 
      Person.LastName AS LastName, 
      Person.BusinessEntityID AS BusinessEntityID 
    FROM AdventureWorks2014.Person.Person 
    INNER JOIN HumanResources.EmployeeDepartmentHistory 
     ON HumanResources.EmployeeDepartmentHistory.BusinessEntityID = Person.BusinessEntityID 
    INNER JOIN HumanResources.Department 
     ON EmployeeDepartmentHistory.DepartmentID = HumanResources.Department.DepartmentID 
    GROUP BY Person.FirstName, 
      Person.LastName, 
      Person.BusinessEntityID 
    HAVING COUNT(*) > 1 
) 

SELECT Person.FirstName, 
     Person.LastName, 
     HumanResources.Department.Name AS CurrentDepartment, 
     StartDate, 
     EndDate 
FROM AdventureWorks2014.Person.Person 
INNER JOIN HumanResources.EmployeeDepartmentHistory 
    ON HumanResources.EmployeeDepartmentHistory.BusinessEntityID = Person.BusinessEntityID 
INNER JOIN HumanResources.Department 
    ON EmployeeDepartmentHistory.DepartmentID = HumanResources.Department.DepartmentID 
INNER JOIN cte t 
    ON Person.FirstName = t.FirstName AND 
     Person.LastName = t.LastName AND 
     Person.BusinessEntityID = t.BusinessEntityID 
+0

使用'BusinessEntityID'可以让多个员工具有相同的'FirstName,LastName'。为什么不计算cte中的GROUP BY - HAVING? –

+0

@JuanCarlosOropeza这是一个查询的野兽:-( –

+0

为什么你认为我只写了额外的'JOIN':P。但是如果你在cte中使用我的查询,最终的结果是更清洁 –