Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
student name student mark
raj 50
raj 45
raj 60
raj 70
raj 65
megh 87
megh 63
megh 89
megh 35
megh 25
naveen 60
naveen 70
naveen 65
naveen 87
naveen 63

I want to change the data while selecting in below format

studentname studentmark sub1 sub2 sub3 sub4 sub5
----------- ------------ ---- ---- ---- ---- ----
Posted
Comments
King Fisher 12-Jan-15 4:51am    
don't you have Subject Name for Marks ?
Member 11337367 12-Jan-15 4:58am    
no just u give sub1 i can understand..
Member 11337367 12-Jan-15 5:42am    
Please help me out..
Rajesh waran 12-Jan-15 6:19am    
I have Updated my Solution.You can Refer now.

 
Share this answer
 
Comments
Member 11337367 12-Jan-15 4:56am    
can you writen give using pivot am unable to write that
Refer this links,
http://www.mindfiresolutions.com/Using-Pivot-Operator-in-SQL-Server-1181.php[^]
http://blog.sqlauthority.com/2008/06/07/sql-server-pivot-and-unpivot-table-examples/[^]
http://sqlhints.com/2014/03/10/pivot-and-unpivot-in-sql-server/[^]

Edit:
Try like this,
I just create a table similar to your table.
SQL
CREATE TABLE #test (student_name nvarchar(max) , student_marks float , subject nvarchar(max))

INSERT INTO #test VALUES('Raj',50,'Sub1')
INSERT INTO #test VALUES('Raj',55,'Sub2')
INSERT INTO #test VALUES('Raj',60,'Sub3')
INSERT INTO #test VALUES('Raj',65,'Sub4')
INSERT INTO #test VALUES('Raj',70,'Sub5')

INSERT INTO #test VALUES('Rajesh',70,'Sub1')
INSERT INTO #test VALUES('Rajesh',75,'Sub2')
INSERT INTO #test VALUES('Rajesh',80,'Sub3')
INSERT INTO #test VALUES('Rajesh',85,'Sub4')
INSERT INTO #test VALUES('Rajesh',90,'Sub5')

And Try Pivot like this,
SQL
select student_name, [1], [2], [3],[4],[5]
from
(
  select student_name, student_marks,
    row_number() over(partition by student_name order by subject) rn
  from #test
) d
pivot
(
  max(student_marks)
  for rn in ([1], [2], [3],[4],[5])
) piv;

I'm showing 5 Subject Data's here.

Edit2: Here you can use order by for student name

SQL
with cte as (select student_name,[1], [2],[3],[4],[5]
from
(
  select student_name, student_marks,
    row_number() over(partition by student_name order by subject) rn 
  from #test
) d
pivot
(
  max(student_marks)
  for rn in ([1], [2], [3],[4],[5])
) piv) select * from cte order by student_name desc


Edit3:If it not work,No need to use CTE.try like this,I used "order by" here at end of query.
SQL
select student_name,[1], [2],[3],[4],[5]
from
(
  select student_name, student_marks,
    row_number() over(partition by student_name order by subject) rn 
  from #test
) d
pivot
(
  max(student_marks)
  for rn in ([1], [2], [3],[4],[5])
) piv order by student_name desc ;

Hope this may helps you.
 
Share this answer
 
v4
Comments
Member 11337367 12-Jan-15 4:56am    
Please can you write that using pivot...?
Rajesh waran 12-Jan-15 6:11am    
I have updated my solution .Refer it now
Member 11337367 12-Jan-15 7:13am    
Thank you very much this what am exactly looking for..My long time issue has been resolved by ur answer...
Rajesh waran 12-Jan-15 7:15am    
You're welcome.If my solution is correct to you, just accept my solution using "Accept Answer" button.In order to remove this question from Unanswered Queue.
Member 11337367 13-Jan-15 0:10am    
One more thing we could use order by in this quey..if it is possible very i have write that..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900