3

我在将查询作为树排序时遇到问题

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

我需要有以下树:

UT 根
UT 根
-- UT 1级
UT 根
-- UT 1级
-- -- UT 2级
UT 根

这在级别 0 或 1 中正常工作,但对于更高级别,它已损坏。我尝试在根列中选择“0级”父级以按根和ut排序,但是在解决这个问题一段时间后,我不能:(

如何解决这个问题?

谢谢你的帮助。

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

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

但它也不起作用

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

ut parenteutlevel 根
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

如您所见,这不是良好的结构。我需要一棵树:

ut parenteutlevel 根
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    
4

1 回答 1

9

因为递归调用是正确的,你的问题在于结果的排序

按根排序

您可以尝试创建一个排序路径来帮助它们以正确的顺序排列:

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(从您的测试数据中添加一个工作示例):

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

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;
于 2012-04-04T15:00:14.580 回答