Converting N rows to columns, including custom headers

Hello,

I have been stuck on this for a long time now, I hope that someone can help!
Please refer to this SQLFiddle for the table structure: /#!18/78f96/3 (Please append this to the SQL fiddle URL, as I am not allow to post links...)

SQL server 2008 used.

Goal:

I need to create a raw data report from several tables like this one. Below template is the only way I have found so far that could allow the data to be usable for further analysis after export. Please note that there are 2 different types: 'IN' and 'EX' that I would need to display as in the below example. I would then also need to rename the Type column to something like 'Internal Check', for the 'IN' Type.

Template:

I hope the idea is clear, if not please let me know, I can update this post and / or the related fiddle.
In addition to above request, I would also like to know how I could incorporate the result to another query, basically doing a join on SN.

Many thanks in advance!

Search for dynamic pivot https://sqlblogcasts.com/blogs/madhivanan/archive/2008/08/27/dynamic-pivot-in-sql-server-2005.aspx

or

1 Like