如果我有一个表Employee
在哪里
EmployeeID
,
EmployeeName
,
FirstName
,
LastName
,
ManagerID
,
ManagerName
此处ManagerID
也有EmployeeID
。
经理下的员工工作。
如何从表Employee
中找到
FirstName
,
EmployeeName
,
ManagerName
一位经理不超过6名员工。
答案 0 :(得分:1)
SELECT FirstName,EmployeeName, ManagerName
FROM Employee e
WHERE e.ManagerID IN
(
SELECT ManagerID
FROM Employee
GROUP BY ManagerID
HAVING COUNT(*) <= 6
)
答案 1 :(得分:0)
/ *考虑到可能有顶级员工,其中经理ID为空。 MSSQL中的代码* /
SELECT FirstName, EmployeeName, ManagerName
FROM Employee e INNER JOIN
(
SELECT ManagerId
FROM Employee
WHERE ManagerId IS NOT NULL
GROUP BY ManagerId
HAVING COUNT(*) <= 6
) t
ON (e.ManagerId = t.ManagerId OR e.ManagerId IS NULL);
/ *如果我们不关心经理ID是否为空,那么* /
SELECT FirstName, EmployeeName, ManagerName
FROM Employee e INNER JOIN
(
SELECT ManagerId
FROM Employee
GROUP BY ManagerId
HAVING COUNT(*) <= 6
) t
ON (e.ManagerId = t.ManagerId);