按经理ID分组

时间:2012-07-09 21:02:39

标签: sql-server database sql-server-2008 tsql

我有一张如下表格

Emp_id,Emp_user_id,Emp_Name,Emp_mgr_id

Emp_mgr_id将是经理的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和输出我需要按直接报告分组的所有下属。

由于

3 个答案:

答案 0 :(得分:2)

您可以使用recursive common-table expression。事实上,它是它们最常见的用途之一(经理 - >员工类型层次结构)

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

答案 1 :(得分:1)

您好在下面找到sproc以获取订单中的名称。

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

答案 2 :(得分:1)

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

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

INSERT INTO emptbl 值(1, 'D021', '乔',6), (2, 'D024', '凯西',6), (3, 'D054', '肖恩',1), (4, 'D058', '蒂珀',2), (5, 'D059', '保罗',4), (6, 'D070', '大卫',9), (7, 'D075', '杰克',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