1WITH pivot_data AS
2(
3SELECT customerid, -- Grouping Column
4dbcolumnname, -- Spreading Column
5data -- Aggregate Column
6FROM pivot2
7)
8SELECT customerid, [firstname], [middlename], [lastname]
9FROM pivot_data
10PIVOT (max(data) FOR dbcolumnname IN ([firstname],[middlename],[lastname])) AS p;
11