SSRS 2008 group variables - count items within a group
-
29-04-2021 - |
Question
I have a report which grabs a whole bunch of statistical data that represent system alarms.
I've made a drilldown style report by pulling back all the stats in a user selected date range and then I added a group on my alarm's codeId so I would have something to group on.
What I would like to do is have a count of the number of items within these groups by codeId. I plan to use the count as part of a concatenated string that makes up the whole group header. It basically says "Alarm Code # {codeId} - {Description} - Total: {Total}".
My issue is that to do a count in my query then I have to pre-group the data before it hits the report. And if I do that then I do not have my detail data to expand the group on. And running a summary and a detail dataset is a bit over my head but an option I might have to look into.
So I was hoping I could use a group variable to count the number of items contained within the group. Can anyone tell me a rough set of steps to try to pull that off?
Solution
Seems like using a Group variable here would make this more complicated than it needs to be.
Since you already have a group in SSRS, you can use the CountRows function. Put this expression into a placeholder, for example:
="Alarm Code #" & Fields!CodeID.Value
& " - " & First(Fields!Description)
& " - Total: " & CountRows()
Or, depending on size, you can add a correlated subquery to your SQL query:
SELECT
CodeID,
MyField,
MyOtherField,
(SELECT COUNT(*) FROM myTable t1 WHERE t1.CodeID = t2.CodeID) AS MyRowCount
FROM
myTable t2
This is not particularly efficient SQL, so if you are running this on hundreds of thousands of rows or more, I'd go with the first approach.