I have read lots of blog posts. I have read the docs. I am usually fairly good at picking up new stuff but even though I keep reading, but I just don't understand the parts of a PIVOT in SQL Server (2008).
Can someone please give it to me, nice and slow. (ie Pivot for Dummies)
If an example is needed then we can use the one in this question.
Here is how I tried to pivot that example:
SELECT OtherID, Val1, Val2, Val3, Val4, Val5
FROM
(SELECT OtherID, Val
FROM @randomTable) p
PIVOT
(
max(val)
FOR Val IN (Val1, Val2, Val3, Val4, Val5)
) AS PivotTable;
The above query gives me nulls instead of values in the Val1, Val2... columns.
But to be clear, I am not looking for a fixed query here. I need to understand PIVOT as I am looking to pivot something far more complex than this example.
Specifically what is the deal with the aggregate? I just want to take all string values that match on a given ID and put them in the same row. I am not trying to aggregate anything. (Again, see this question for my example.)