0

我有一张如下表

Emp_id、Emp_user_id、Emp_Name、Emp_mgr_id

Emp_mgr_id 将是 Manager 的 emp_id。

所以例如我有如下数据

Emp_id Emp_user_id Emp_Name Emp_mgr_id
1      D021         Joe      6
2      D024         Kathy    6
3      D054         Shaun    1
4      D058         Tippu    2
5      D059         Paul     4
6      D070         David    9
7      D075         Jack     9

现在我需要创建一个表单,当 David 使用他的 id D070 登录时,我需要将所有员工报告拉给 David。

D021 Joe
 -->D054 Shaun
D024  Kathy
  -->D058   Tippu
  -->D059   Paul

我想按他的直接下属对行进行分组。

有人知道如何编写查询来提取这样的数据吗?输入将是 Emp_User_id,输出我需要按直接下属分组的所有下属。

谢谢

4

3 回答 3

2

您可以为此使用递归公用表表达式。事实上,这是它们最常见的用途之一(经理 -> 员工类型层次结构)

WITH Emp_CTE AS (
SELECT Emp_ID, Emp_User_ID, Emp_Name, Emp_Mgr_ID
FROM EmpTable
WHERE Emp_Mgr_ID IS NULL -- This assumes the top-level managers have no manager.
UNION ALL
SELECT Emp_ID, Emp_User_ID, Emp_Name, Emp_Mgr_ID
FROM EmpTable AS e
    INNER JOIN Emp_CTE AS cte ON cte.Emp_ID = e.Emp_Mgr_ID
)
SELECT *
FROM Emp_CTE
于 2012-07-09T21:12:46.633 回答
1

嗨找到下面的存储过程以按顺序获取名称。

CREATE TABLE emptbl(Emp_id int, Emp_user_id varchar(10), Emp_Name varchar(100), Emp_mgr_id int)

INSERT INTO emptbl
VALUES(1,'D021','Joe',6),
(2,'D024','Kathy',6),
(3,'D054','Shaun',1),
(4,'D058','Tippu',2),
(5,'D059','Paul',4),
(6,'D070','David',9),
(7,'D075','Jack',9)

SELECT * FROM emptbl



CREATE Proc sp_getRepportees
@emp_id varchar(20)
As
--sp_getRepportees 'D070'
DECLARE @min int=1
DECLARE @max int

DECLARE @empout TABLE(Emp_id int, Emp_user_id varchar(10), Emp_Name varchar(100), Emp_mgr_id int)
;WITH CTEEmp(Emp_id,Emp_user_id,Emp_Name,Emp_mgr_id) As
(
SELECT Emp_id,Emp_user_id,Emp_Name,Emp_mgr_id from emptbl where Emp_user_id=@emp_id
UNION ALL
SELECT et.Emp_id,et.Emp_user_id,et.Emp_Name,et.Emp_mgr_id from emptbl et inner join CTEEmp e on et.Emp_mgr_id = e.emp_id)

,CTE2(rno,Emp_id,Emp_user_id,Emp_Name,Emp_mgr_id) as(
SELECT ROW_NUMBER() over (order by emp_id) as rno,* FROM CTEEmp where Emp_user_id<>@emp_id)

select * into #tempe from CTE2
SELECT @max=COUNT(*) from #tempe

select * from #tempe
While(@min<=@max)
begin

IF EXISTS(select Emp_id,Emp_user_id,Emp_Name,Emp_mgr_id from #tempe where Emp_mgr_id in (select Emp_id from #tempe where rno=@min))
begin
INSERT INTO @empout
select Emp_id,Emp_user_id,Emp_Name,Emp_mgr_id from #tempe where rno=@min
union  
select Emp_id,Emp_user_id,Emp_Name,Emp_mgr_id from #tempe where Emp_mgr_id in (select Emp_id from #tempe where rno=@min)
end

delete from #tempe where rno=@min
SET @min+=1
end

select Emp_user_id,Emp_Name from @empout
于 2012-07-10T09:48:57.350 回答
1

这是 CTE 的输出。但是在这里你需要做一点改变才能以所需的格式显示结果。

创建表emptbl(Emp_id int,Emp_user_id varchar(10),Emp_Name varchar(100),Emp_mgr_id int)

插入空值(1,'D021','Joe',6),(2,'D024','Kathy',6),(3,'D054','Shaun',1),(4,' D058','Tippu',2), (5,'D059','Paul',4), (6,'D070','David',9), (7,'D075','Jack',9 )

SELECT * FROM emptbl

;WITH CTEEmp(Emp_id,Emp_user_id,Emp_Name,Emp_mgr_id) As
(
SELECT Emp_id,Emp_user_id,Emp_Name,Emp_mgr_id from emptbl where Emp_user_id='D070'
UNION ALL
SELECT  et.Emp_id,et.Emp_user_id,et.Emp_Name,et.Emp_mgr_id from emptbl et inner join CTEEmp e on et.Emp_mgr_id = e.emp_id)

,CTEEmp1(Emp_id,Emp_user_id,Emp_Name,Emp_mgr_id)As
(SELECT Emp_id,Emp_user_id,Emp_Name,Emp_mgr_id from CTEEmp where Emp_user_id<>'D070')

SELECT * FROM CTEEmp1 
于 2012-07-10T09:54:16.113 回答