devxlogo

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'

Charlie has over a decade of experience in website administration and technology management. As the site admin, he oversees all technical aspects of running a high-traffic online platform, ensuring optimal performance, security, and user experience.

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.