2012-07-09 56 views
0

我有一個像下面集團的經理ID

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 

現在我需要創建一個表單和大衛登錄用他的身份證D070,我需要把所有的員工報告大衛數據。

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

我想通過他的直接報告對行進行分組。

任何人有任何想法如何編寫查詢來拉這樣的數據?輸入將是Emp_User_id,輸出我需要通過直接報告分組的所有下屬。

感謝

+0

如果您與我們分享定義你的表的'CREATE TABLE'語句,一個d給我們展示了一個預期的結果集,我們會更容易幫助你。 – 2012-07-09 21:16:50

回答

1

這裏是放出來與CTE.But這裏你需要做一點改變讓結果顯示在所需的格式。

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', '喬',6), (2,'D024','Kathy',6), (3,'D054','Shaun',1), (4,'D058','Tippu',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 
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

喜找到下面的存儲過程,以獲得訂單的名稱。

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 [email protected]_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<[email protected]) 
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 [email protected])) 
begin 
INSERT INTO @empout 
select Emp_id,Emp_user_id,Emp_Name,Emp_mgr_id from #tempe where [email protected] 
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 [email protected]) 
end 

delete from #tempe where [email protected] 
SET @min+=1 
end 

select Emp_user_id,Emp_Name from @empout