使用列分组计算矩阵中字段的总和

时间:2013-09-26 06:50:43

标签: reporting-services ssrs-2008 calculated-columns ssrs-grouping

我正在处理带有列分组的ssrs报告。以下是我的情景。

Matrix 1:

ID     2012  2013
1      20    40
1      30    50

Total  50    90

Matrix 2:

ID     2012  2013
1      60    70
1      60    80

Total  120   150

我需要矩阵1和矩阵2的总和,如下所示:

ID     2012  2013
1      170   240

但我的结果如下:

ID     2012  2013
1      410   410

我已经在所有3个矩阵中应用了列分组,并给出了表达式以获得矩阵3的总和:=Sum(Fields!amount1.Value, "dsmatrix1") + Sum(Fields!Tamount1.Value, "dsmatrix2")

请帮我解决这个问题。

谢谢!

1 个答案:

答案 0 :(得分:0)

我想我知道发生了什么事。如果我错了,请纠正我。

根据我所看到的情况,我猜测Matrix 1和Matrix 2每个只有三个字段,ID字段,金额字段(“amount1”或“Tamount1”)和年份字段。

您的列分组正在操纵数据的显示,以显示按年分列的所有值。从单个数据集查看数据时,此方法可以正常工作。但是,您的公式指定应添加dsmatrix1的Amount1字段和dsmatrix2的Tamount1字段中的所有内容的总和。这不考虑列分组。您的表达式实际上是从两个数据集中获取所有值并将它们一起添加。

不了解您的查询结构或数据过滤方式的更多信息,我最好的猜测是您需要另一个SQL数据集。在这种情况下,您将从前两个数据集中获取查询,并使用“Union All”命令将它们合并。请注意,您将需要使用Union All而不仅仅是Union。更多相关内容:What is the difference between UNION and UNION ALL?

您的最终结果应如下所示:

--This will be your dsmatrix1 query copied and pasted
Select ...

Union All

--This will be your dsmatrix2 query copied and pasted
Select ...

--Place one single Order by clause at the bottom
Order by ...

注意:要使两个查询正确联合,您需要确保每个查询具有相同数量的字段,每个字段具有相同的数据类型。然后,您可以将第三个矩阵指向新数据集。

希望有所帮助!