Showing posts with label sql. Show all posts
Showing posts with label sql. Show all posts

Saturday, May 9, 2015

Convert colum to row without using unpivot operator

In this post I will show you how to convert column to row in SQL.For this post I am going to use following table which have three colum stu_name,sibject and marks.

image

SELECT  stu_name,
        MAX(CASE WHEN subject = 'ECO' THEN marks
                 ELSE 0
            END) ECO,
        MAX(CASE WHEN subject = 'HIS' THEN marks
                 ELSE 0
            END) HIS,
        MAX(CASE WHEN subject = 'MAT' THEN marks
                 ELSE 0
            END) MAT,
        MAX(CASE WHEN subject = 'GEO' THEN marks
                 ELSE 0
            END) GEO,
        MAX(CASE WHEN subject = 'SCI' THEN marks
                 ELSE 0
            END) SCI
FROM    col_to_rows
GROUP BY stu_name
image