1

How to pivot a table in MySQL,

PIvoting table around date column in Mysql

I have a mysql data table as follows

**Student_id    Student_name    Subject Activity    Marks** 

1             Raja          Maths   null              44    
1             Raja          IT  practical     33    
1             Raja          Tamil   null              22    
1             Raja          Histry  null              54    
2             Ganga         Maths   null              33    
2             Ganga         IT  null              22    
2             Ganga         Histry  null              44    
2             Ganga         Tamil   null              89    

So i need to pivot my table as below,

Student_id  Student_name    Mths    IT  Tamil   Histry  Activity

1            Raja            44 33  22  54  practical

2            Ganga           33 22  89  44  null

I tried to many ways by using the stackoverflow, but i couldnot able to achieve the target, need a help from the mysql expert,

Raymond Nijland
  • 11,488
  • 2
  • 22
  • 34

2 Answers2

0

I was able to complete the my task by using @Raymond Nijland's suggestion

PIvoting table around date column in Mysql

wadie
  • 496
  • 1
  • 10
  • 24
0
select *
from 
(
 select Student_id, Student_name, Subject, Activity, Marks
 from #your_table
 ) src
 pivot
 (
 sum(Marks)
 for Subject in ([Maths], [IT], [Tamil], [Histry])
 ) piv;

Histry is written Mistakenly.. check your table data please.

Aditya Dhanraj
  • 189
  • 1
  • 1
  • 12