Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: SQL
Expertise: Advanced
Jul 19, 2016

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, Field3

FROM [Table]

) a

PIVOT(
MIN(Field2)
FOR a.Field1 IN (RowField1,
RowField2,
RowField3,
RowField4,
RowField5,
RowField6)
) AS b
WHERE b.RowField3 = 'TEST'
Hannes du Preez
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date