I have an SSRS grid report with a header row that SUM()s select child rows.
In the SQL query, one Shift table inner joins to many Parts tables, meaning an Hours field on the Shift table is duplicated per part. I use the “Hide duplicates” SSRS feature on the Hours cell to fix this visually. The issue is that although the duplicates are hidden, the SUM() function (on the report, not the query) still includes them in its summation of hours.
So, I might have the following table and header. Duplicates of Hours (and Shift) are hidden, but still (undesirably) included in the summation.
SHIFT HOURS PART ON HAND
===== ======= ==== =======
SUM: 8 SUM: 90
===== ======= ==== =======
1st 3 X12Z 20
J23Z 10
2nd 1 Y36P 30
3rd 1 Q90F 30
I am simplifying the actual report, so displaying the data in a different way isn’t an option. Also, these are already in a group, so I’m not sure I can add another group. How do I get the SUM() function to return the sum of the hours shown?
EDIT:
I have posted a thorough answer to my own question, but if there is a better way to do this or my answer could be improved, please feel free to offer your critique. If there are still answers that significantly improve on my method, I will accept them.
Given how presumably common this format on a report is, I expected there to be more answers or at least a better way to do this. But, alas, my method follows:
Rather than implement a
cursorand loop through the table, run thisupdateon the original results in a stored procedure. It clears the values of duplicated fields (i.e., shift, hours) where the current row’s part isn’t the part of the first row returned for this Shift. If there is anorder byin the original query, include it in the nestedselect:Essentially, the formula is to:
update‘swhereusing the “child” table’s identifying field.select‘swhereusing the parent table’s identifying field.