我需要一个主管下的所有员工 [英] I need all the employee under a supervisor

查看:28
本文介绍了我需要一个主管下的所有员工的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下描述的情况

EmpID      Name        SupervisorID
1          A           9
2          B           8
3          C           1
4          D           3

我需要主管 ID 1 下的所有员工

I need all the employees under supervisor ID 1

这里 EmployeeID 3 小于 1 我需要 4 也是小于 3

Here EmployeeID 3 is under 1 i need 4 is is also under 3

需要以下输出.

EmpID   Name    SupervisorID
3       C       1
4       D       3

推荐答案

您需要为此使用递归 CTE.试试这个,

You need to Use Recursive CTE for this.Try this,

With CTE as
(
select EmpID,Name,SupervisorID from Emp
where SupervisorID  =1
Union All 

select a.EmpID,a.Name,a.SupervisorID from Emp as a
    inner join CTE b on a.SupervisorID= b.EmpID

)

select * from CTE

此处为小提琴演示

也请看看这个问题,它和你的问题一样.Sql server CTE 和递归示例

Please take a look at this question also, it is same like your question. Sql server CTE and recursion example

这篇关于我需要一个主管下的所有员工的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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