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 o
Explanation of the pivot query
FROM
(SELECT OtherID, Val, amount
FROM @randomTable) p
These are the columns that become the "base data" for the pivot. Do not include columns that don't do anything. Just as you don't put non-GROUP BY columns into the SELECT clause, you don't list out unused columns in a PIVOT source.
PIVOT
(
max(amount)
FOR Val IN (Val1, Val2, Val3, Val4, Val5)
) AS PivotTable;
This part says that you are creating 5 new columns named "Val1" through "Val5". These column names represent values in the column Val. So it is expected that your table will contain something like this
otherID Val amount
1 Val1 1
2 Val2 2
1 Val3 3
1 Val1 5
(etc) (this column contains one of Val1 - Val5, or null)
So you now have 5 new columns that did not exist before. What goes into the column?
So, to illustrate, using the sample data above, we have otherID=1 and val=Val1. In the output table, there is only one cell representing this combination of Max(amount) for each (otherID/val) combination
otherID Val1 Val2 Val3 Val4 Val5
1 <x> ... ... ... ...
(etc)
For the cell marked <x> , only one value is allowed, so <x> cannot contain multiple amount values. That is the reason why we need to aggregate it, in this case using MAX(amount). So in fact, the output looks like this
(unpivoted columns) (pivoted, creates "new" columns)
otherID | Val1 Val2 Val3 Val4 Val5
1 | MAX(amount) Max(amount) << cell value = aggregate function
(etc)
The SELECT statement is what then outputs these columns
SELECT OtherID, Val1, Val2, Val3, Val4, Val5