PIVOTing Data in SQL

You can use the PIVOT SQL command to transpose Row data into Column data. This means that you can take a set of rows and make fields out of them! Each row in the results table (based on the particular query / data) will represent a field. In this next small example, I have pivoted 6 fields (RowField1 – RowField6) depending on the first table’s Field values:

SELECT DISTINCT * FROM (SELECT Field1, Field2, Field3FROM [Table]) aPIVOT(MIN(Field2)FOR a.Field1 IN (RowField1,RowField2,RowField3,RowField4,RowField5,RowField6)) AS bWHERE b.RowField3 = 'TEST'
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Related Posts