空白表结果

时间:2017-02-13 14:20:46

标签: sql sql-server tsql

我的数据库出了问题。我使用冒险作品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 

我删除了“我得到返回结果”(整个表格)。所以我想我知道问题在哪里,不是它是什么/如何解决它。

3 个答案:

答案 0 :(得分: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 :(得分: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

答案 2 :(得分: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