PIVOTing Data in SQL

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'

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist