Convert column 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 the following table which has three column 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

Please do not post any spam link in the comment box😊

Post a Comment (0)
Previous Post Next Post