如何通过Sql Server查询明智地排列Parent_Id和Child_Id中的表数据

时间:2018-07-05 05:57:39

标签: sql sql-server sql-server-2005 sql-server-2012

如何通过SQL Server查询明智地排列Parent_Id和Child_Id中的表数据?

我的查询

 select CLevel_Id,Category_Name,Parent_Id,Child_Id,Level_Id from Category_Level
 order by Parent_Id asc

当前输出

CLevel_Id   Category_Name   Parent_Id   Child_Id    Level_Id
12             Jewelry           1           0             1
14             Rings             2           1             2
15             Men-Rings         3           2             3
17             Women-Rings       4           2             3
18             Earrings          5           1             2
20             Women-Earings     6           5             3
1013           Metal-Fashion     7           3             4
1015           Diamond-Fashion   8           4             4
1016           Semi-Set          9           6             4

预期产量

CLevel_Id   Category_Name   Parent_Id   Child_Id    Level_Id
12             Jewelry           1           0             1
14             Rings             2           1             2
15             Men-Rings         3           2             3
1013           Metal-Fashion     7           3             4
17             Women-Rings       4           2             3
1015           Diamond-Fashion   8           4             4
18             Earrings          5           1             2
20             Women-Earings     6           5             3
1016           Semi-Set          9           6             4

请帮助我

1 个答案:

答案 0 :(得分:3)

使用递归CTE。

假设您每个级别最多9个。使用一位数字作为seq级别。如果您的数字超过9,则需要使用两位数字,例如01、02等

; with
rcte as
(
    -- Anchor member, seq = 1
    select  *, seq = convert(varchar(100), '1')
    from    Category_Level
    where   Child_Id    = 0

    union all

    -- recursive member, concatenate to the seq
    select  c.*, seq = convert(varchar(100), 
                    r.seq 
                +   convert(varchar(10), row_number() over (partition by r.seq 
                                                                order by c.Child_Id)))
    from    Category_Level c
        inner join rcte r   on  c.Child_Id  = r.Parent_Id
)
select  *
from    rcte 
order by seq

/* RESULT
CLevel_Id   Category_Name        Parent_Id   Child_Id    Level_Id    seq
----------- -------------------- ----------- ----------- ----------- -------
12          Jewelry              1           0           1           1
14          Rings                2           1           2           11
15          Men-Rings            3           2           3           111
1013        Metal-Fashion        7           3           4           1111
17          Women-Rings          4           2           3           112
1015        Diamond-Fashion      8           4           4           1121
18          Earrings             5           1           2           12
20          Women-Earings        6           5           3           121
1016        Semi-Set             9           6           4           1211

(9 rows affected)
*/