Used to convert rows to columns.

Example 1:

SELECT 
       ROW1, 
       ROW2, 
       ROW3
FROM(
	SELECT 
		COL1 ,COL2
        FROM 
		tbTable
        WHERE 
		COL3 = 'Test'
	) t1
PIVOT ( MAX(COL2) 
        FOR COL1 IN (
			ROW1, 
			ROW2, 
			ROW3
		    )
       ) t2

Example 2:

SELECT Type,YearMonth,TypeCount FROM #tempData

SELECT 
	* 
FROM 
	#tempData d
PIVOT
	(MAX(TypeCount) FOR YearMonth IN ([2018-4],[2018-5],[2018-6],[2018-7],[2018-8],[2018-9],[2018-10],[2018-11],[2018-12],[2019-1],[2019-2],[2019-3],[2019-4],[2019-5])) AS MaxCount

Sources:

Last modified: May 20, 2019

Author

Comments

Write a Reply or Comment