4
votes

I have an SSRS report. It looks like this:

enter image description here

Its definition looks like this:

enter image description here

As you can see it's just a bunch of numbers in different columns A to G. Those numbers were grouped by A then B then C. For each row in the report, column D,E,F,G are my detailed data. The problem is, for each group (I mean grouped by A,B,C), I need all rows for the D&E column. For F&G column, I don't want the duplicates. So for example, for the very first group, we can see for column F&G there are duplicates, the data 20, 30 appeared twice. Essentially what I want is something like this (note the duplicates were removed compared to the aforementioned report result):

enter image description here

Take a look at the data with yellow background, that's the part where I want duplicates removed. I don't know how to implement this in SSRS. Hope someone can point me to the right direction. You can download the report I'm testing at https://www.dropbox.com/s/qxvloaeuj31m0mj/Report1.rdl?dl=1. This simple report was created to isolate the problem I met in a more complex report. I can't change the logic and have to find a way to implement this. Thanks in advance.

2
You say you can't change the logic; what logic can you not change? Where are your hands tied here?R. Richards
@R.Richards I mean I can't change the stored procedure in database that generates the report data. The report that I used in my question is just a simplified version of my actual report. I've tried to make the yellow background part a sub report which uses the value of the grouping columns as parameters to the sub report. It actually works but the performance is not acceptable as the sub report will get executed for each and every group.Just a learner
Got it. Typical really, right? How about inserting the output from the current stored procedure into a temp table, then maybe working the data to get it where it needs to be before shooting it out to the report? Possible? I have done that; I don't always like it, but it works.R. Richards
IF i understand correctly your problem you will not be able to do that, you have to change your queryoupepasa
Thanks @oupepasa. Do you have any idea about how to change the data shape and how to implement it? I have no idea about it right now.Just a learner

2 Answers

3
votes

Here is my go at this. I am making an assumption that what you want is mostly like the last screenshot you included in the question.

I pulled down the RDL you made available. Thanks for that! It can make this sort of thing that much easier.

The SQL in the report is mock data, which is good for what we are trying. I assume that the stored procedure that you are sourcing the data from now gives you something similar. I made a comment about getting the results into a temp table to work with, changing/shaping the data, and then returning it to the report. That is the tact I am going with in this answer.

Here is the updated SQL from the RDL, with a few line left out for brevity.

SELECT *
INTO #Report
FROM (VALUES(1,4,1,1,10,20,30),
(1,4,1,2,11,20,30),
…
(22,666,7,14,23,150,6)
) AS t(A, B, C, D,E,F,G);

SELECT A.A, A.B, A.C, A.D, A.E  -- selecting columns A thru E
FROM #Report A 

UNION ALL

SELECT DISTINCT B.A, B.B, B.C, B.F, B.G  -- union in columns F and G
FROM #Report B
ORDER BY 1, 2, 3

DROP TABLE #Report

All this really does is use a SELECT INTO to create a temp table with the original results. Then, I split the results, and UNION it all back together with only the columns I need stacked on top of each other; also using a DISTINCT where you want to get rid of duplication.

To use this in the RDL you provided, you will have to remove the F and G columns from the Tablix, and replace the SQL with this. This is what the Tablix would look like in design mode.

enter image description here

Now, you mentioned that there is a stored procedure that you cannot change. To get results from a stored procedure into a temp table you need to do 2 things.

First, create the temp table in the same shape at the results from the procedure. The tables doesn’t really have to be a temp table; you can make it a non-temp table and drop it later, too. In the temp table case, the DDL would look something like this:

CREATE TABLE #Report (A int, B int, C int, D int, E int, F int, G int)

Then you insert the results of the stored procedure into that temp table.

INSERT INTO #Report
EXEC usp_MyStoredProcedure

And no, there is no simple SELECT INTO option with a stored procedure, in case you were wondering. A lot of people wish there was.

So, if this is the way you end up going, here is what the SQL for the report could be like.

CREATE TABLE #Report (A int, B int, C int, D int, E int, F int, G int);

INSERT INTO #Report
EXEC usp_MyStoredProcedure;

SELECT A.A, A.B, A.C, A.D, A.E  -- grabbing columns A thru E
FROM #Report A 
UNION ALL
SELECT DISTINCT B.A, B.B, B.C, B.F, B.G  -- union in columns F and G
FROM #Report B
ORDER BY 1, 2, 3

This is what the report would look like in preview mode:

enter image description here

Hope this helps you out.

2
votes

If you need an SSRS based solution rather than an SQL one, you can use Adjacent groups to control two subsequent sets of data separately.

  • In your Row Groups, right-click on your (Details) group and choose Add Group > Adjacent After....
  • In the Tablix Group window that appears, Group by [F]
  • Right-click the new (F) row group and go to Group Properties.
  • Add an extra Group expression, and Group on [G]. (You might want to also change the name of the group to FG).
  • Move the F and G fields in your report to the new row (in the FG group), and delete any empty columns.

The details group will still show every row from your dataset, i.e. every instance of D and E. The FG group will only show each distinct combination of F and G once, within each A/B/C group.

Report1 preview with FG group added