问题描述
需要传递员工ID并获得在SQL中传递员工ID的经理下工作的员工总数。
Out应该是这样的: -
EmplD NoofEmployees
1234 10
2562 25
其中10& 25实际上是在EmplD 1234和2562的经理下工作的员工总数。
我尝试了什么:
我尝试过CTE和自我加入,但我需要一个性能更好的解决方案。
Need to Pass employee Id and get total number of employees working under the manager whose employee id is passed in SQL.
Out should be like this : -
EmplD NoofEmployees
1234 10
2562 25
Where 10 & 25 is actually the total number of employees working under the manager of EmplD 1234 and 2562 resp.
What I have tried:
I tried CTE and Self Join but i need a solution with better performance.
推荐答案
Employees: ID Name ManagerID
然后是一个简单的GROUP BY会这样做:
Then a simple GROUP BY will do it:
SELECT ManagerID, COUNT(ID) FROM Employees GROUP BY ManagerID
如果不是在我们开始回答之前,我们需要更多信息。
表格是这样的:
在GROUP中使用JOIN:
If it isn't, we'd need a load more info before we could begin to answer.
"The Table is something like this :"
Use a JOIN with the GROUP by:
SELECT e.ID, e.Name, m.MngCount FROM EMPLOYEE e
JOIN (SELECT ManagerID, COUNT(ID) as MngCount FROM Employees GROUP BY ManagerID) m
ON e.ManagerID = m.ManagerID
这篇关于Sql查询查找在我的经理下工作的员工总数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!