关于sql server:带有顺序树的递归SQL查询

Recursive SQL Query with order tree

我有一个问题要将查询作为树来排序,如下所示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
WITH UtHierarchy
AS (
    SELECT etabid
        ,ut
        ,utlib
        ,parenteut
        ,0 AS LEVEL
        ,ut AS root
    FROM RUT
    WHERE etabid = 1
        AND parenteut IS NULL

    UNION ALL

    SELECT RUT.etabid
        ,RUT.ut
        ,RUT.utlib
        ,RUT.parenteut
        ,LEVEL + 1 AS LEVEL
        ,RUT.parenteut AS root
    FROM RUT
    INNER JOIN UtHierarchy uh ON uh.ut = rut.parenteut
    WHERE RUT.ETABID = 1
    )
SELECT *
FROM UtHierarchy
ORDER BY root

我需要有以下树:

1
2
3
4
5
6
7
UT Root
UT Root
-- UT level 1
UT Root
-- UT level 1
-- -- UT level 2
UT Root

这是按0或1级的预期工作,但对于更高的级别,它会被破坏。我尝试在根列中选择要按根和UT排序的"0级"父级,但在一段时间后,我不能:(

如何解决这个问题?

谢谢你的帮助。

编辑:感谢您使用SQL颜色进行编辑:)我已经看到了最顶层的解决方案,但用户已经删除了他的帖子。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
WITH UtHierarchy
AS (
  SELECT etabid
  ,ut
  ,utlib
  ,parenteut,
  0 as profondeur,
  ut as root
  FROM RUT
  where etabid = 278
  and parenteut is null
  UNION  ALL
  SELECT RUT.etabid
  , RUT.ut
  , RUT.utlib
  , RUT.parenteut
  , profondeur + 1 as profondeur
  , root as root
  FROM RUT
  inner join UtHierarchy uh on uh.ut = rut.parenteut
  where RUT.ETABID = 278
)
select ut, parenteut, profondeur, root
from UtHierarchy
order by root

但它也不起作用

这里有一个真实数据的例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
ut  parenteutlevel  root
10  1   1   1
11  1   1   1
12  1   1   1
13  1   1   1
14  1   1   1
130 13  2   1
131 13  2   1
132 13  2   1
133 13  2   1
134 13  2   1
135 13  2   1
136 13  2   1
120 12  2   1
121 12  2   1
122 12  2   1
110 11  2   1
111 11  2   1
112 11  2   1
113 11  2   1
114 11  2   1
115 11  2   1
116 11  2   1
101 10  2   1
102 10  2   1
103 10  2   1
104 10  2   1
105 10  2   1
106 10  2   1
107 10  2   1
1       0   1

如你所见,这不是一个好的结构。我需要一棵这样的树:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
ut  parenteutlevel  root
1       0   1
10  1   1   1
101 10  2   1
102 10  2   1
103 10  2   1
104 10  2   1
105 10  2   1
106 10  2   1
107 10  2   1
11  1   1   1
110 11  2   1
111 11  2   1
112 11  2   1
113 11  2   1
114 11  2   1
115 11  2   1
116 11  2   1
12  1   1   1
120 12  2   1
121 12  2   1
122 12  2   1
13  1   1   1
130 13  2   1
131 13  2   1
132 13  2   1
133 13  2   1
134 13  2   1
135 13  2   1
136 13  2   1
14  1   1   1


由于递归调用是正确的,您的问题在于结果的排序

ORDER BY root

您可以尝试创建一个排序路径,以帮助按正确的顺序进行排序:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
WITH UtHierarchy
AS (
    SELECT etabid
        ,ut
        ,utlib
        ,parenteut
        ,0 AS LEVEL
        ,ut AS root
        ,RIGHT('000000' + CAST(ut AS varchar(MAX)), 6) AS sort
    FROM RUT
    WHERE etabid = 1
        AND parenteut IS NULL

    UNION ALL

    SELECT RUT.etabid
        ,RUT.ut
        ,RUT.utlib
        ,RUT.parenteut
        ,LEVEL + 1 AS LEVEL
        ,RUT.parenteut AS root
        ,uh.sort+'/'+RIGHT('000000' + CAST(RUT.ut AS varchar(20)), 6) AS sort
    FROM RUT
    INNER JOIN UtHierarchy uh ON uh.ut = rut.parenteut
    WHERE RUT.ETABID = 1
    )
SELECT *
FROM UtHierarchy
ORDER BY sort

编辑:

case=>cast(拼写错误)

编辑2(从测试数据中添加工作示例):

这里有一个复制粘贴测试代码。对我来说很好:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
SELECT 10 AS ut, 1 AS parenteut
INTO #RUT
UNION ALL SELECT 11, 1
UNION ALL SELECT 12, 1
UNION ALL SELECT 13, 1
UNION ALL SELECT 14, 1
UNION ALL SELECT 130, 13
UNION ALL SELECT 131, 13
UNION ALL SELECT 132, 13
UNION ALL SELECT 133, 13
UNION ALL SELECT 134, 13
UNION ALL SELECT 135, 13
UNION ALL SELECT 136, 13
UNION ALL SELECT 120, 12
UNION ALL SELECT 121, 12
UNION ALL SELECT 122, 12
UNION ALL SELECT 110, 11
UNION ALL SELECT 111, 11
UNION ALL SELECT 112, 11
UNION ALL SELECT 113, 11
UNION ALL SELECT 114, 11
UNION ALL SELECT 115, 11
UNION ALL SELECT 116, 11
UNION ALL SELECT 101, 10
UNION ALL SELECT 102, 10
UNION ALL SELECT 103, 10
UNION ALL SELECT 104, 10
UNION ALL SELECT 105, 10
UNION ALL SELECT 106, 10
UNION ALL SELECT 107, 10
UNION ALL SELECT 1, 0;

WITH UtHierarchy
AS (
    SELECT
        ut
        ,parenteut
        ,0 AS LEVEL
        ,ut AS root
        ,RIGHT('000000' + CAST(ut AS varchar(MAX)), 6) AS sort
    FROM #RUT
    WHERE
        parenteut = 0

    UNION ALL

    SELECT
        RUT.ut
        ,RUT.parenteut
        ,LEVEL + 1 AS LEVEL
        ,RUT.parenteut AS root
    ,uh.sort+'/'+RIGHT('000000' + CAST(RUT.ut AS varchar(20)), 6) AS sort
    FROM #RUT AS RUT
    INNER JOIN UtHierarchy uh ON uh.ut = rut.parenteut    
    )
SELECT *
FROM UtHierarchy
ORDER BY sort

DROP TABLE #RUT;