如果员工有经理(有些员工不是前任首席执行官等),我正在努力找到一名员工经理的姓名。目前,它返回经理的员工姓名,如果没有经过联系的经理ID,则不会返回任何值
以下是我试图访问的表格的一般表格结构:
员工
EmpID
EmployeeNumber
StartDate
isManager
Status ( full time / part time )
ContactID
联系
ContactID
Fname
Lname
ManagerID
系
DeptID
Name
DeptHistory
DeptHistID
DeptID
EmpID
PosTitle
StartDate
EndDate
ModifiedDate
以下是我一直在操纵的查询:
SELECT
dh.StartDate, dh.PositionTitle, d.Name,
e.EmployeeNumber, e.Classification, e.Status,
c1.FirstName, c1.LastName, c1.SIN,
c1.DateOfBirth, c1.PhoneNumber, c1.EmailAddress, c1.AddressLine1,
c1.AddressLine2, c1.PostalCode, c1.City, c1.Province,
(c2.FirstName+ ' ' + c2.LastName) AS Manager
FROM
Person.Contact c1
JOIN
HumanResources.Employee e ON c1.ContactID = e.ContactID
JOIN
HumanResources.EmployeeDepartmentHistory dh ON e.EmpID = dh.EmpID
JOIN
HumanResources.Department d ON dh.DepartmentID = d.DepartmentID
JOIN
Person.Contact c2 ON c2.ManagerID = e.EmpID
WHERE
e.EmpID = @empID
答案 0 :(得分:0)
使用LEFT OUTER JOIN
代替JOIN
,因此查询将如下所示:
LEFT OUTER JOIN Person.Contact c2 ON c2.ManagerID = e.EmpID
OUTER
字是可选的,因此您只能使用LEFT JOIN
。
请参阅Microsoft TechNet上的Using Left Outer Joins。