gpt4 book ai didi

sql - SSRS 未按级别(层次结构)显示所有行

转载 作者:行者123 更新时间:2023-12-04 08:22:20 24 4
gpt4 key购买 nike

好吧,伙计们,这让我发疯......

我有一份报告,其中包含许多功能的详细信息。这些功能可以卡在其他人身上,也可以独立存在,或者两者兼而有之。

我有以下数据作为查询结果:

Feature_ID   Parent_ID
24
24 25
20
26 12
12
21 23
26 20
22
24 23
23 26
24 27
27 28
24 22
29 20
23
25
27 29
22 26
28 12

如您所见,某些功能适合层次结构中的多个位置。然而,我在报告中得到的只是:

SSRS output

我在 Feature_ID 上分组,递归父级是 Parent_ID。我错过了什么?

最佳答案

通过您的措辞和意外的输出,感觉就像您正在寻找不同级别和功能的列表。希望以下内容有帮助。如果没有,也许您可​​以提供一些额外的上下文来了解您要查找的内容。

declare @table table (Feature_ID int, Parent_ID int);

insert @table values
(24,null),
(24,25),
(20,null),
(26,12),
(12,null),
(21,23),
(26,20),
(22,null),
(24,23),
(23,26),
(24,27),
(27,28),
(24,22),
(29,20),
(23,null),
(25,null),
(27,29),
(22,26),
(28,12);

select * from @table order by 1,2;
select * from @table order by 2,1;

with cte as (
select Feature_ID, Parent_ID, 0 [Level], CAST(Feature_ID as varchar(200)) [Path]
from @table
where Parent_ID is null

union all

select t.Feature_ID, t.Parent_ID, c.[Level] + 1, cast(c.[Path] + '|' + CAST(t.Feature_ID as varchar(200)) as varchar(200))
from @table t
join cte c
on c.Feature_ID = t.Parent_ID
)
select distinct [Level], Feature_ID
from cte
order by [Level], Feature_ID;

这给出了以下结果:
Level   Feature_ID
0 12
0 20
0 22
0 23
0 24
0 25
1 21
1 24
1 26
1 28
1 29
2 22
2 23
2 27
3 21
3 24

关于sql - SSRS 未按级别(层次结构)显示所有行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39795248/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com