Skip to content
Advertisement

Unpivot with column name

I have a table StudentMarks with columns Name, Maths, Science, English. Data is like

I want to get it arranged like the following:

With unpivot I am able to get Name, Marks properly, but not able to get the column name in the source table to the Subject column in the desired result set.

How can I achieve this?

I have so far reached the following query (to get Name, Marks)

Advertisement

Answer

Your query is very close. You should be able to use the following which includes the subject in the final select list:

See SQL Fiddle with demo

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement