I was working on SSRS to generate a report for a client. But instead of the conventional way, they'd like what was data in the rows to be displayed as column name.
Assuming the problem is same as the following. The inventory system holds a list of products for each client, and tagged by different categories.
In order to retrieve an exhausted list of products, we normally use:
And the result is pretty straight forward.
But it gets harder and harder to read and understand because of the duplicated category information is not properly categorised. In which case, we can use keywords like where and order by to filter the result. Before you do anything, wouldn’t it be better if data rows to be displayed as data column names in the result, or a crosstab report?
That’s how the Pivot function becomes useful:
Inside the PIVOT function, it performs action on ‘Product’ column, and categorises the result according to ‘Category’ data columns. And here’s what you get:
You might already notice the hard coded part of query, and that’s really annoying to use when automate the whole query process, and actually make it work in SSRS.
In order to tackle this problem, here is how you can do pivot dynamically:
For more information about the pivot syntax, here is what options you can use: