Joh*_*nHC 8 sql reporting-services ssrs-2012
好的伙计们,这让我发疯了......
我有一份报告可以提供许多功能的详细信息.这些功能可以挂掉别人,有自己的存在或者两者兼而有之.
作为查询结果,我有以下数据:
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
Run Code Online (Sandbox Code Playgroud)
如您所见,某些功能适合层次结构中的多个位置.但是,我在报告中得到的是:
我在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;
Run Code Online (Sandbox Code Playgroud)
这给出了以下结果:
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
Run Code Online (Sandbox Code Playgroud)