您可以通过自联接来执行此操作,以从层次结构中生成要排序的值列表,如下面的代码所示。我已经扩展为在原始示例中添加了一个额外的层次结构,以展示它是如何工作的。显然,这取决于知道层次结构级别的数量以生成合理的计划(例如,您总是可以做 10 个级别,但如果您的示例中只有 3 个层次结构级别,那将是一个很大的性能损失)。
经过进一步思考,我想您可以使用 EXEC 语句来生成特定层次结构级别所需的 SQL,而不是如下手动生成(这将进行一些优化,例如我们知道如果条目在 L3 没有任何内容,它将不会) L4也没有任何东西)。
WITH resultset (resultid, parentid, valuex) AS (
SELECT 1,0,'Grandparent' UNION ALL
SELECT 2,1,'Parent1' UNION ALL
SELECT 3,1,'Parent2' UNION ALL
SELECT 4,2,'Child1' UNION ALL
SELECT 5,2,'Child2' UNION ALL
SELECT 6,3,'Child3' UNION ALL
SELECT 7,3,'Child4' UNION ALL
SELECT 8,4,'Child1_Child1' UNION ALL
SELECT 9,7,'Child4_Child1' UNION ALL
SELECT 10,6,'Child3_Child1')
SELECT l1.resultid , l1.parentid, l1.valuex, l2.resultid l2val, l3.resultid l3val,l4.resultid l4val,
-- rewrite COALESCE so clearer how this matches the pattern below
CASE WHEN l4.resultid IS NULL THEN
CASE WHEN l3.resultid IS NULL THEN
CASE WHEN l2.resultid IS NULL THEN l1.valuex
ELSE l2.valuex END
ELSE l3.valuex END
ELSE l4.valuex END o1,
CASE WHEN l4.resultid IS NULL THEN
CASE WHEN l3.resultid IS NULL THEN
CASE WHEN l2.resultid IS NULL THEN ''
ELSE l1.valuex END
ELSE COALESCE (l2.valuex, l1.valuex, '') END
ELSE COALESCE (l3.valuex, l2.valuex, l1.valuex, '') END o2,
CASE WHEN l3.resultid IS NULL THEN ''
WHEN l4.valuex IS NULL THEN l1.valuex
ELSE l2.valuex END o3,
CASE WHEN l2.valuex IS NULL THEN ''
WHEN l4.valuex IS NULL THEN '' ELSE l1.valuex END o4
FROM resultset l1
left join resultset l2 on l1.parentid = l2.resultid
left join resultset l3 on l2.parentid = l3.resultid
left join resultset l4 on l3.parentid = l4.resultid
ORDER BY o1, o2, o3, o4
结果(对格式错误表示歉意):
RESULTID PARENTID VALUEX L2VAL L3VAL L4VAL O1 O2 O3 O4
1 0 Grandparent (null) (null) (null) Grandparent
2 1 Parent1 1 (null) (null) Grandparent Parent1
4 2 Child1 2 1 (null) Grandparent Parent1 Child1
8 4 Child1_Child1 4 2 1 Grandparent Parent1 Child1 Child1_Child1
5 2 Child2 2 1 (null) Grandparent Parent1 Child2
3 1 Parent2 1 (null) (null) Grandparent Parent2
6 3 Child3 3 1 (null) Grandparent Parent2 Child3
10 6 Child3_Child1 6 3 1 Grandparent Parent2 Child3 Child3_Child1
7 3 Child4 3 1 (null) Grandparent Parent2 Child4
9 7 Child4_Child1 7 3 1 Grandparent Parent2 Child4 Child4_Child1