我真的很难解决这个问题。我试图找到每个部门病假最多的员工。如果“仍然是当前员工”,则他们没有结束日期。我尝试过各种方式调整这个但我不断得到多个结果。我只想要每个部门中工作时间最多的员工。
Select H.DepartmentID, max(E.SickLeaveHours) as MaxSick, E.EmployeeID
From HR.Department as D,
HR.EmployeeDepartmentHistory as H,
HR.Employee as E
Where E.EmployeeID = H.EmployeeID
and D.DepartmentID = H.DepartmentID
and H.EndDate is null
group by H.DepartmentID, E.EmployeeID
DepartmentID MaxHours EmployeeID
7 61 8
1 22 9
7 64 10
1 23 11
1 20 12
答案 0 :(得分:0)
select temp.DepartmentID, temp.SickLeaveHours as MaxSick, EH.EmployeeID from
(Select H.DepartmentID DepartmentID, max(E.SickLeaveHours) SickLeaveHours
From HR.Department D,
HR.EmployeeDepartmentHistory H,
HR.Employee E
Where E.EmployeeID = H.EmployeeID
and D.DepartmentID = H.DepartmentID
and H.EndDate is null
group by H.DepartmentID) temp, HR.EmployeeDepartmentHistory EH, Employee emp
Where EH.DepartmentID = temp.DepartmentID
and emp.SickLeaveHours = temp.SickLeaveHours
and emp.employeeId = EH.employeeId
解决方案的第一步是找到每个部门的最大病假。这可以通过上面的内部查询来回答。
下一步是找到该部门的员工匹配最大小时数,这可以通过外部查询中的联接来回答。
如果多个员工的最长病假时间相同,则查询将返回该部门的所有此类员工。
不知道这是否与此相关,但在我看来,病假的工作时间在某种程度上是累计工作时间,不论他/她在哪个部门工作,因为病假时间是"属性" "员工"而不是" EmployeeDepartmentHistory"。因此,病假时间可能在之前的部门中消耗/获得,但会出现在此查询的当前部门(其中enddate在EmployeeDepartmentHistory中为null)。