如何将多行聚合为一个

时间:2009-06-10 14:47:17

标签: reporting-services grouping

我几天前问过similar question,但是提出更具挑战性的问题并没有什么害处。总而言之,我从SQL Server返回一个数据集,我不知道如何在报告中正确分组。

以下是一些示例数据:

SELECT companyID = 106, userID = 71152, productID = 11,taxID = 216, quantity = 78, total = 71325 UNION ALL
SELECT companyID = 106, userID = 71152, productID = 11,taxID = 245, quantity = 41, total = 40297 UNION ALL
SELECT companyID = 106, userID = 71152, productID = 12,taxID = 246, quantity = 12, total = 7685 UNION ALL
SELECT companyID = 106, userID = 51154, productID = 11,taxID = 216, quantity = 92, total = 84311 UNION ALL
SELECT companyID = 106, userID = 51154, productID = 11,taxID = 245, quantity = 145, total = 139329 UNION ALL
SELECT companyID = 106, userID = 51154, productID = 11,taxID = 275, quantity = 80, total = 87513 UNION ALL
SELECT companyID = 106, userID = 51154, productID = 12,taxID = 246, quantity = 23, total = 15800 UNION ALL
SELECT companyID = 106, userID = 51154, productID = 12,taxID = 277, quantity = 12, total = 8060 UNION ALL
SELECT companyID = 106, userID = 51221, productID = 11,taxID = 216, quantity = 4, total = 3655 UNION ALL
SELECT companyID = 106, userID = 51221, productID = 11,taxID = 245, quantity = 29, total = 29692 UNION ALL
SELECT companyID = 106, userID = 51221, productID = 11,taxID = 275, quantity = 55, total = 63679 UNION ALL
SELECT companyID = 106, userID = 51221, productID = 12,taxID = 246, quantity = 3, total = 2390 UNION ALL
SELECT companyID = 106, userID = 61221, productID = 11,taxID = 216, quantity = 162, total = 138403 UNION ALL
SELECT companyID = 106, userID = 61221, productID = 11,taxID = 245, quantity = 130, total = 123760 UNION ALL
SELECT companyID = 106, userID = 61221, productID = 11,taxID = 275, quantity = 19, total = 22073 UNION ALL
SELECT companyID = 106, userID = 61221, productID = 12,taxID = 246, quantity = 1, total = 415 UNION ALL
SELECT companyID = 114, userID = 71198, productID = 11,taxID = 245, quantity = 131, total = 134039 UNION ALL
SELECT companyID = 114, userID = 71198, productID = 11,taxID = 275, quantity = 120, total = 141423 UNION ALL
SELECT companyID = 114, userID = 71198, productID = 12,taxID = 246, quantity = 70, total = 36590 UNION ALL
SELECT companyID = 114, userID = 71198, productID = 12,taxID = 249, quantity = 1, total = 980 UNION ALL
SELECT companyID = 114, userID = 71198, productID = 12,taxID = 276, quantity = 1, total = 790 UNION ALL
SELECT companyID = 114, userID = 71198, productID = 12,taxID = 277, quantity = 64, total = 41380 UNION ALL
SELECT companyID = 114, userID = 71198, productID = 12,taxID = 279, quantity = 1, total = 690 UNION ALL
SELECT companyID = 114, userID = 71198, productID = 12,taxID = 293, quantity = 1, total = 760 UNION ALL
SELECT companyID = 114, userID = 71204, productID = 11,taxID = 245, quantity = 48, total = 48049 UNION ALL
SELECT companyID = 114, userID = 71204, productID = 11,taxID = 275, quantity = 41, total = 43729 UNION ALL
SELECT companyID = 114, userID = 71204, productID = 12,taxID = 246, quantity = 1, total = 450 UNION ALL
SELECT companyID = 114, userID = 71204, productID = 12,taxID = 249, quantity = 7, total = 5895 UNION ALL
SELECT companyID = 114, userID = 71204, productID = 12,taxID = 277, quantity = 2, total = 1300 UNION ALL
SELECT companyID = 114, userID = 71204, productID = 12,taxID = 279, quantity = 2, total = 2230 UNION ALL
SELECT companyID = 614, userID = 71198, productID = 11,taxID = 216, quantity = 54, total = 48748 UNION ALL
SELECT companyID = 614, userID = 71198, productID = 11,taxID = 245, quantity = 121, total = 113030 UNION ALL
SELECT companyID = 614, userID = 71198, productID = 12,taxID = 246, quantity = 11, total = 6080 UNION ALL
SELECT companyID = 614, userID = 71204, productID = 11,taxID = 216, quantity = 14, total = 11519 UNION ALL
SELECT companyID = 614, userID = 71204, productID = 11,taxID = 245, quantity = 32, total = 29103 UNION ALL
SELECT companyID = 614, userID = 71204, productID = 12,taxID = 249, quantity = 3, total = 3535

基本上,我们以不同的税率销售特定产品。我想显示按companyID,userID,productID对数据进行分组的数据,并显示数量和总字段的总和,类似于:

companyID   userID      productID   sumQuantity sumTotal
----------- ----------- ----------- ----------- -----------
106         51154       11          317         311153
106         51154       12          35          23860
106         51221       11          88          97026
106         51221       12          3           2390
106         61221       11          311         284236
106         61221       12          1           415
106         71152       11          119         111622
106         71152       12          12          7685
114         71198       11          251         275462
114         71198       12          138         81190
114         71204       11          89          91778
114         71204       12          12          9875
614         71198       11          175         161778
614         71198       12          11          6080
614         71204       11          46          40622
614         71204       12          3           3535

SSRS无法以这种方式对行进行分组,因为taxID是唯一的。结果可以在these screenshots中找到。

如何强制SSRS将多个记录合并为一行?

3 个答案:

答案 0 :(得分:3)

为什么不直接从SQL查询的select和grouping子句中删除taxid?那会得到你想要的东西。

答案 1 :(得分:0)

听起来您可能想要创建自己的calculated fields,其中包含汇总的总计。

编辑:就像我对其他答案的评论一样:创建一个不同的数据集,它将以您想要的方式聚合数据。除非你正在处理返回的怪异行,否则这不应该是一个性能问题,并且从长远来看,它可能比尝试从一个可能不合适的数据集中破解解决方案要好得多。

答案 2 :(得分:0)

看起来您将标题放在分组行中,将数据放在详细信息中。将标题放在标题行中,将数据放在分组行中,然后删除或隐藏详细信息。