带有顺序树的递归SQL查询

时间:2012-04-04 14:15:49

标签: sql-server tsql tree common-table-expression recursive-query

我在将查询命名为树时遇到问题

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 Root
UT Root
-- UT level 1
UT Root
-- UT level 1
-- -- UT level 2
UT Root

这适用于0级或1级,但是对于更高级别,它会被破坏。我尝试在root列中选择'level 0'父级按root和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  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

正如你所看到的,它不是一个好的结构。 我需要一棵树:

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    

1 个答案:

答案 0 :(得分:9)

ince Recursive调用是正确的,你的问题在于结果的排序

  

ORDER BY root

您可以尝试创建排序路径,以帮助按正确的顺序获取它们:

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;