Juliet
Juliet

Reputation: 81536

How to aggregate several rows into one

I asked a similar question a few days ago, but there's no harm in posing a more challenging one. To make a long story short, I'm getting back a data set from SQL Server and I don't know how to group it correctly on the report.

Here's some sample data:

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

Basically, we sell a particular product at different tax rates. I want to show data which groups data by companyID, userID, productID, and displays a sum of the quantity and total fields, similar to this:

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 is unable to group the rows in this way because the taxID is unique. The result can be see in these screenshots.

How can I force SSRS to consolidate multiple records into a single row?

Upvotes: 1

Views: 962

Answers (3)

TheTXI
TheTXI

Reputation: 37905

Sounds like you may want to look into creating your own calculated fields that would contain the aggregated totals.

Edit: Like in my comment to the other answer: create a different dataset which will aggregate the data in the manner you want. Unless you are dealing with monstrous rows returned, this should not be much of a performance issue, and in the long run it's probably a lot better than trying to hack together a solution off of a dataset which may not be suitable.

Upvotes: 0

Peter Radocchia
Peter Radocchia

Reputation: 11007

Looks like you put your headers in the grouping row and your data in the detail. Put the headers in the header row, the data in the grouping row, and delete or hide the detail.

Upvotes: 0

msvcyc
msvcyc

Reputation: 2603

Why don't you just drop the taxid from the select and grouping clause of the SQL query? That will get you what you want.

Upvotes: 3

Related Questions