Unpivot Table in SQL Server

--   Unpivot table in sql

--   Unpivot means you can display your Horizontal data vertically (Note: it is used to view the      data only)
--   I think I don't need to explain this; you will understand it easily by just looking at the         example

--   I have created a simple example

--   Just copy, paste and run. And see the difference

--   Here I have taken a temp table and have inserted the value into that. (You also can play with      your table)

Declare @Student table(ID int, name varchar(10), Maths int , English int , Science int)

--   @student is the name of the table
--   Insert values into the @student table

insert into @Student values(101, 'ashok' ,80 ,86 ,90)

insert into @Student values(105, 'vinod' ,50,80,90)
insert
into @Student values(106, 'sejal' ,90,50,90)

--   to check the values in a table you can select

select * from @Student

--   use pivot

select ID, Name, Subject, Marks from

(
select
ID, name, Maths , English , Science from @Student
)
as source
unpivot

(

Marks for Subject in ([Maths],[English],[Science])

--   Here the Subject like header can take anything according to the format

) as unpvt

--   I think I need not explain more