分层 SQL 查询、SSRS 2008 和求和列

发布于 2024-10-29 00:18:37 字数 1920 浏览 1 评论 0原文

我正在构建 SSRS 报告,但在弄清楚如何实现它方面遇到了一些困难。

首先,这是数据的基本结构:

  • Level 1a
    • 2a 级
    • 2b级
    • 2c 级
      • 3a级
  • Level 1b
  • Level 1c

在数据库中,每个级别都有一个 id 和一个父 id,其中根(级别 1 的父级)的所有父 id 均为 0。此外,每个级别都有一个包含数值的列。例如,我可能:

Level1a
ParentId = 0
Id = 1
DataValue = 42

Level2a
ParentId = 1
Id = 2
DataValue = 1

目前,我已经实现了一个公共表表达式来获取数据的层次结构和级别:

WITH cteTable(id, parentId, data, level) AS 
(SELECT id, 
        parentId, 
        data, 
        1 AS level 
FROM sampleTable 
WHERE (parentId = 0) 
UNION ALL 
SELECT b.id, 
       b.parentId, 
       b.data, 
       c.level + 1 AS Expr1 
FROM sampleTable AS b 
INNER JOIN cteTable AS c ON b.parentId= p.id
) 
SELECT id, 
       parentId,  
       data, 
       level 
FROM cteTable

因此,使用之前的示例数据,查询的结果将是:

+----+----------+------+-------+
| id | parentId | data | level |
|----+----------+------+-------+
| 1  | 0        | 42   | 1     |
| 2  | 1        | 1    | 2     |
+----+----------+------+-------+

从这里,我需要构建一个报告,将数据汇总到根级别。例如,报告将显示 id 1 的数据总和为 43。我不确定的一件事是级别有多深 - 在某些情况下没有子级别,而在其他情况下树的位置深几层。

我现在正在考虑两个选择。一种是创建一个深入报告,该报告将显示每个级别的数据以及可用的总和。例如:

-Level1a SUM_LEVEL2_AND_LEVEL1a_DATA
   Level2a DATA
   Level2b DATA
  -Level2c SUM_LEVEL3_AND_LEVEL2c_DATA 
      Level3a DATA
Level1b DATA
Level1c DATA

另一种是将数据求和到根并在报表中显示一个简单的表格,仅包含父级总计。例如:

Level1a SUM_LEVEL1A_AND_ALL_CHILDREN_DATA
Level1b SUM_LEVEL1B_AND_ALL_CHILDREN_DATA
Level1c SUM_LEVEL1C_AND_ALL_CHILDREN_DATA

我似乎无法弄清楚如何使用我所做的谷歌搜索来获取深入报告(有一个链接here 这似乎很有用,但我就是无法让它工作)。

更改数据库架构不是一个选项。

有谁知道我可以使用哪些好的资源来开始,或者对如何继续进行有任何想法?

如果我遗漏了什么,请告诉我......

I am building an SSRS report and am having a bit of trouble figuring out how to implement it.

First, here is the basic structure of the data:

  • Level 1a
    • Level 2a
    • Level 2b
    • Level 2c
      • Level 3a
  • Level 1b
  • Level 1c

In the database, each level has an id and a parent id where all parent ids at the root (parents of level 1) are 0. Additionally, each level has a column with a numeric value. For example, I might have:

Level1a
ParentId = 0
Id = 1
DataValue = 42

Level2a
ParentId = 1
Id = 2
DataValue = 1

Currently, I have implemented a common table expression to get the hierarchy structure and levels of the data:

WITH cteTable(id, parentId, data, level) AS 
(SELECT id, 
        parentId, 
        data, 
        1 AS level 
FROM sampleTable 
WHERE (parentId = 0) 
UNION ALL 
SELECT b.id, 
       b.parentId, 
       b.data, 
       c.level + 1 AS Expr1 
FROM sampleTable AS b 
INNER JOIN cteTable AS c ON b.parentId= p.id
) 
SELECT id, 
       parentId,  
       data, 
       level 
FROM cteTable

so the with the sample data from earlier, the results of the query would be:

+----+----------+------+-------+
| id | parentId | data | level |
|----+----------+------+-------+
| 1  | 0        | 42   | 1     |
| 2  | 1        | 1    | 2     |
+----+----------+------+-------+

From here, I need to build a report that will sum up the data to the root level. For example, the report would show that the data sum for id 1 is 43. One thing I don't know for certain is how deep the levels go - there will be some instances where there are no child levels and others where the tree goes a few levels deep.

There are two options I am considering right now. One is to create a drill down report which will show the data at each level with a sum where available. For example:

-Level1a SUM_LEVEL2_AND_LEVEL1a_DATA
   Level2a DATA
   Level2b DATA
  -Level2c SUM_LEVEL3_AND_LEVEL2c_DATA 
      Level3a DATA
Level1b DATA
Level1c DATA

The other is to sum the data to the root and show a simple table in the report with the parent totals only. For example:

Level1a SUM_LEVEL1A_AND_ALL_CHILDREN_DATA
Level1b SUM_LEVEL1B_AND_ALL_CHILDREN_DATA
Level1c SUM_LEVEL1C_AND_ALL_CHILDREN_DATA

I can't seem to figure out how to get the drill down report working with the googling I've done (there is a link here that seemed to be useful, but I just couldn't get it to work).

Changing the database schema is not an option.

Does anyone know of any good resources I could use to get started or have any ideas on how to proceed with this?

Let me know if I am missing anything...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怪我入戏太深 2024-11-05 00:18:37

我终于找到了解决这个问题的方法。

公共表表达式 (CTE) 是一个良好的开端。我对其所做的唯一更改是在递归定义中 - 我没有为每个孩子收集直接父母的 id,而是为其分配了祖父母的 id(直接父母拥有的父母 id)。这意味着每个孩子都被分配了最终的父 ID,而不是仅在层次结构中向上移动一级。从那时起,在报告中使用它就非常简单了。

我将 CTE 放在它自己的存储过程中 - 唯一的目的是恢复整个层次结构。从那里,我创建了另一个存储过程,它将 CTE 与另一个存储过程结合起来。此时,我应用了过滤器并返回了最终结果集。

I finally found a solution to this one.

The common table expression (CTE) was a good start. The only change I made to it was in the recursive definition - instead of collecting the id of the immediate parent for each child, I assigned it the grandparent's id (the parent id the immediate parent had). This meant that every child was assigned the ultimate parent id instead of moving up only one step in the hierarchy. From there, it was pretty straightforward to use this in the report.

I put the CTE in a stored procedure of its own - the only purpose is to bring back the entire hierarchy. From there, I created another stored procedure, which combined the CTE with a different stored procedure. At this time, I applied the filters and returned the final result set.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文