【问题标题】:SSRS not showing all rows by level (hierarchy)SSRS 未按级别显示所有行(层次结构)
【发布时间】:2016-09-30 15:40:52
【问题描述】:

好吧,伙计们,这快把我逼疯了……

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

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

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

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

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

【问题讨论】:

  • 你的结果应该是什么?我看到您的 tablix 显示错误数据,因为 24 应该在 level 0 中,那么您使用的级别表达式是什么?
  • @alejandrozuleta 我使用=level() 作为级别字段。
  • 你的预期结果是什么?
  • 冷静下来,伙计们。我已经破解了!作为查询的一部分,我提取了每个 Feature_ID 的完整层次结构路径和每个父级的相应路径。

标签: sql reporting-services ssrs-2012


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-04
    • 2023-04-03
    • 2017-09-28
    • 1970-01-01
    • 2016-02-02
    相关资源
    最近更新 更多