I'm sure something is missing here.
I have a dataset like this:
FK RowNumber Value Type Status
1 1 aaaaa A New
1 2 bbbbb B Good
1 3 ccccc A Bad
1 4 ddddd C Good
1 5 eeeee B Good
2 1 fffff C Bad
2 2 ggggg A New
2 3 hhhhh C Bad
3 1 iiiii A Good
3 2 jjjjj A Good
I would like to query the results from the top 3 and collapse them as columns, so the set of final results looks like this:
FK Value1 Type1 Status1 Value2 Type2 Status2 Value3 Type3 Status3
1 aaaaa A New bbbbb B Good ccccc A Bad
2 fffff C Bad ggggg A New hhhhh C Bad
3 iiiii A Good jjjjj A Good
How to do this in SQL Server 2005?
I am trying to use this PIVOT , but I am still very unfamiliar with this keyword and cannot make it work the way I want.
SELECT * --Id, [1], [2], [3] FROM ( SELECT Id, Value, Type, Status , ROW_NUMBER() OVER (PARTITION BY Id ORDER Status, Type) as [RowNumber] FROM MyTable ) as T PIVOT ( -- I know this section doesn't work. I'm still trying to figure out PIVOT MAX(T.Value) FOR RowNumber IN ([1], [2], [3]), MAX(T.Type) FOR RowNumber IN ([1], [2], [3]), MAX(T.Status) FOR RowNumber IN ([1], [2], [3]) ) AS PivotTable;
My actual dataset is a bit more complicated than this, and I need the first 10 records, not the top 3, so I don’t want to just do CASE WHEN RowNumber = X THEN... for each of them.
Update
I tested all of the answers below and found that most of them look approximately the same, with no apparent difference in performance in smaller datasets (about 3 thousand records), however, when performing queries against large datasets, there was a slight difference.
Here are the results of my tests using 80,000 records and queries for 5 columns in the top 10 rows, so my final result was 50 columns + Id column. I would suggest you test them yourself to decide which one is best for you and your environment.
The answer to the question about the blue fight , showing the deployment and re-rotation of the data, averaged the fastest by 12 seconds. I also liked this answer because it was easier for me to read and maintain.
Aaron's answer and koderoid answer both suggest using MAX(CASE WHEN RowNumber = X THEN ...) , and were close after averaging about 13 seconds.
Rodney's answer using multiple PIVOT expressions averaged over 16 seconds, although this can be faster with fewer PIVOT statements (my tests had 5).
And the first half of Aaron's answer, which suggested using CTE and OUTER APPLY , was the slowest. I don’t know how long it will take to start, because I canceled it after 2 minutes, and that was with about 3 thousand records, 3 rows and 3 columns instead of 80 thousand records, 10 rows and 5 columns.