I have a temp table named #rdata like this :
Months KPI_1 KP1_2 KPI_3
-------------------------------------------
Jan-18 x x x
Feb-18 x x x
Mar-18 x x x
Apr-18 x x x
Aug-18 x x x
I want to change it to this table:
KPIs Jan-18 Feb-18 Mar-18 ....
-------------------------------------------
KPI_1 x x x
KPI_2 x x x
KPI_3 x x x
I have come this far:
----Creating a list of Months
SELECT @Columns = COALESCE(@Columns + ', ','') + QUOTENAME(Months)
FROM
(SELECT DISTINCT Months
FROM #rdata ) AS B
SET @SQL = 'SELECT ' + @Columns + ' KPI_1, KPI_2,KPI_3 FROM #rdata
) as PivotData
PIVOT
(
FOR Months IN (' + @Columns + ')
) AS PivotResult'
EXEC(@SQL)
It doesn't seem to work, any idea what am I missing?

The syntax for PIVOT provides is simpler and more readable than the syntax that may otherwise be specified in a complex series of SELECT...CASE statements.From microsoft.com