You will need to use the dynamic SQL that I was thinking about (unless your columns remain static)
The query to be completed is as follows:
SELECT [Jan] = [Jan1], [Jan] = [Jan2], [Feb] = [Feb1], [March] = [March1] FROM ( SELECT [PivotColumn] = [Month] + CONVERT(VARCHAR(10), ROW_NUMBER() OVER(PARTITION BY [Month] ORDER BY ID)), Val1 FROM T ) t PIVOT ( MAX(Val1) FOR [PivotColumn] IN ([Jan1], [Jan2], [Feb1], [March1]) ) pvt
Although I'm not sure why this should be
Jan Jan Feb March 70 12 12 14
but not
Jan Jan Feb March 12 70 12 14
therefore, you may need to bother with ORDER BY in the ROW_NUMBER function.
And to build it dynamically, you can use:
-- CREATE SAMPLE TABLE AND INSERT DATA CREATE TABLE
Again, any changes to ROW_NUMBER should also be reflected in the ORDER BY in the query that generates the column names and the pivot list:
SELECT @ColumnList = @ColumnList + ', ' + QUOTENAME([Month]) + ' = ' + QUOTENAME([Month] + CONVERT(VARCHAR(10), ROW_NUMBER() OVER(PARTITION BY [Month] ORDER BY ID))), @PivotList = @PivotList + ', ' + QUOTENAME([Month] + CONVERT(VARCHAR(10), ROW_NUMBER() OVER(PARTITION BY [Month] ORDER BY ID))) FROM