Pivot Table in SQL Server

--Pivot table in sql

-- Pivot means you can display your vertical data into horizontally view (Note: it is used to view data only)
--I think no need to explain just look example you will understand easily

--I have created simple example just copy past and run and see the difference and learn

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

Declare @Student table(ID int, name varchar(10),Subject varchar(10), Marks int) -- @student is name of table

-- Insert values into @student table

insert into @Student values(101, 'ashok' , 'Maths',86),(101, 'ashok' , 'English',80),(101, 'ashok' , 'Science',80)

insert into @Student values(105, 'vinod' , 'Maths',90),(105, 'vinod' , 'English',70),(105, 'vinod' , 'Science',60)

-- to check values in table you can select

select * from @Student

--use pivot

select ID, Name, [Maths], [English] , [Science] from

(
select
ID, name, Subject , Marks from @Student
)
as source
pivot

(

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

--i think no need to explain more

--Look at max(Marks) it means i am taking max value you can use any other aggrigate function like sum , min