编写查询以在SQL中获取分层数据

时间:2017-05-09 07:29:48

标签: sql sql-server hierarchical-data recursive-query

我正在使用MS SQL 2008 R2,我想根据员工检索数据 按所需输出部分中给出的最旧到最新数据的层次结构顺序

我的表数据如下:

SELECT * FROM 
(
    SELECT Code = 1  ,ParentCode = NULL, EmployeeName  = 'Hemant'
    UNION ALL
    SELECT Code = 2  ,ParentCode = 1, EmployeeName  = 'Sachin'
    UNION ALL
    SELECT Code = 3  ,ParentCode = 2, EmployeeName  = 'Shanti'
    UNION ALL
    SELECT Code = 4  ,ParentCode = 1, EmployeeName  = 'Sandesh'
    UNION ALL
    SELECT Code =  5  ,ParentCode = 3, EmployeeName  = 'Sandeep'
    UNION ALL
    SELECT Code = 6  ,ParentCode = NULL, EmployeeName  = 'Smily'
    UNION ALL
    SELECT Code = 7  ,ParentCode = 6, EmployeeName  = 'Snehi'
    UNION ALL
    SELECT Code =  8  ,ParentCode = 6, EmployeeName  = 'Kiran'
    UNION ALL
    SELECT Code =  9  ,ParentCode = 7, EmployeeName  = 'Shital'
    UNION ALL
    SELECT Code =  10  ,ParentCode = 9, EmployeeName  = 'Simran'
) X

必需的OutPut:

Code    Name        ParentCode   Parent
1      Hemant       NULL         NULL
2      Sachin       1            Hemant
3      Shanti       2            Sachin
5      Sandeep      3            shanti
4      Sandesh      1            Hemant
6      Smily        NULL         NULL
7      Snehi        6            Smily
9      Shital       7            Snehi
10     Simran       9            Shital
8      Kiran        6            Smily

4 个答案:

答案 0 :(得分:1)

您可以使用LEFT JOIN

来完成
SELECT t1.Code,
       t1.EmployeeName Name,
       t1.ParentCode,
       t2.EmployeeName Parent
FROM yourTable t1
LEFT JOIN yourTable t2 ON t1.ParentCode = t2.Code

答案 1 :(得分:0)

我使用临时表首先存储你的" select union"数据,然后使用左连接查询数据:

SELECT * 
INTO #temp_tbl_test 
FROM (SELECT Code = 1  ,ParentCode = NULL, EmployeeName  = 'Hemant'
    UNION ALL
    SELECT Code = 2  ,ParentCode = 1, EmployeeName  = 'Sachin'
    UNION ALL
    SELECT Code = 3  ,ParentCode = 2, EmployeeName  = 'Shanti'
    UNION ALL
    SELECT Code = 4  ,ParentCode = 1, EmployeeName  = 'Sandesh'
    UNION ALL
    SELECT Code =  5  ,ParentCode = 3, EmployeeName  = 'Sandeep'
    UNION ALL
    SELECT Code = 6  ,ParentCode = NULL, EmployeeName  = 'Smily'
    UNION ALL
    SELECT Code = 7  ,ParentCode = 6, EmployeeName  = 'Snehi'
    UNION ALL
    SELECT Code =  8  ,ParentCode = 6, EmployeeName  = 'Kiran'
    UNION ALL
    SELECT Code =  9  ,ParentCode = 7, EmployeeName  = 'Shital'
    UNION ALL
    SELECT Code =  10  ,ParentCode = 9, EmployeeName  = 'Simran') x

SELECT base.Code, base.EmployeeName, parent.Code AS ParentCode, parent.EmployeeName AS ParentName
FROM #temp_tbl_test base
LEFT JOIN #temp_tbl_test parent 
ON parent.Code = base.ParentCode

答案 2 :(得分:0)

您也可以使用UNION ALL和INNER JOIN实现相同的输出,如下所示。 我已将您的表命名为SampleData

SELECT * FROM 
(SELECT code,EmployeeName as Name,Parentcode,NULL AS EmployeeName
FROM SampleData WHERE Parentcode IS NULL
UNION ALL
SELECT s1.code,s1.EmployeeName as Name,s1.ParentCode,s2.EmployeeName
FROM SampleData s1
JOIN SampleData s2 ON s1.parentcode = s2.Code) X
ORDER BY X.code

答案 3 :(得分:0)

Use Recusrion for this purpose
SELECT * INTO EmployeeDetails FROM 
(
    SELECT Code = 1  ,ParentCode = NULL, EmployeeName  = 'Hemant'
    UNION ALL
    SELECT Code = 2  ,ParentCode = 1, EmployeeName  = 'Sachin'
    UNION ALL
    SELECT Code = 3  ,ParentCode = 2, EmployeeName  = 'Shanti'
    UNION ALL
    SELECT Code = 4  ,ParentCode = 1, EmployeeName  = 'Sandesh'
    UNION ALL
    SELECT Code =  5  ,ParentCode = 3, EmployeeName  = 'Sandeep'
    UNION ALL
    SELECT Code = 6  ,ParentCode = NULL, EmployeeName  = 'Smily'
    UNION ALL
    SELECT Code = 7  ,ParentCode = 6, EmployeeName  = 'Snehi'
    UNION ALL
    SELECT Code =  8  ,ParentCode = 6, EmployeeName  = 'Kiran'
    UNION ALL
    SELECT Code =  9  ,ParentCode = 7, EmployeeName  = 'Shital'
    UNION ALL
    SELECT Code =  10  ,ParentCode = 9, EmployeeName  = 'Simran'
) X

//这是CTE

WITH REC
(Code,ParentValue,EmployeeName,Parent,Level,GRP,LEVELs,[ORDER])
AS
(
SELECT Code,ParentCode ParentValue,EmployeeName,CONVERT(VARCHAR(max),Null) Parent,
1 level ,Code GRP,STR(0) LEVELs,
CAST(ROW_NUMBER()OVER(PARTITION BY ParentCode ORDER BY ParentCode) AS VARCHAR(MAX)) as [ORDER]
FROM EmployeeDetails WHERE ParentCode is null
UNION ALL
SELECT B.Code,b.ParentCode ParentValue,b.EmployeeName,CONVERT(VARCHAR(max),A.EmployeeName) Parent,
 A.level + 1 Level , A.GRP,STR(ROW_NUMBER() OVER(PArtition By B.ParentCode order by B.ParentCode)) LEVELs,
[ORDER] + CAST(ROW_NUMBER()OVER(PARTITION BY B.ParentCode ORDER BY B.ParentCode) AS VARCHAR(MAX)) AS [ORDER]
FROM EmployeeDetails  B 
INNER JOIN REC A ON B.ParentCode = A.Code
)
SELECT*
FROM REC A
ORDER BY A.[ORDER]