Sql查询查找在我的经理下工作的员工总数 [英] Sql query to find total number of employees working under my manager

查看:193
本文介绍了Sql查询查找在我的经理下工作的员工总数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要传递员工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.

推荐答案

所以,你想要我们给你一个查询来处理我们看不到的数据,并且不知道它是如何组织的?



这可能不容易,但是...... 。

假设您的数据类似于:

So, you want us to give you a query to work with data that we can't see, and have no idea how it is organised?

That might not be easy, but...
Assuming your data is similar to this:
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查询查找在我的经理下工作的员工总数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆