Friday, June 24, 2016

Unpivot in SQL Server Example

Given the following records below, you may want to show them in a vertical manner instead of horizontal view of columns.


To achieve the desired result, you use UNPIVOT clause in your query.
1
2
3
4
5
6
7
8
Use testdatabase
Go
SELECT [Subject],
       Grade
FROM StudentReportCard 
UNPIVOT (Grade FOR [Subject] IN (English, Math, Literature, Biology)) 
        AS U_SubjectGrade
WHERE StudentId = 1

Screenshot

0 comments:

Post a Comment