0

Sorry if I asked this dumb question as it puzzled me how to do such for several days.

I have a table in MySQL named Grades. enter image description here

Now, what I want to do is to make an output like this one. enter image description here

Is there an SQL to do such? Thank you so much in advance.

levi palmer
  • 205
  • 5
  • 21
  • possible duplicate of [MYSQL - Rows to Columns](http://stackoverflow.com/questions/1241178/mysql-rows-to-columns) – Bill Karwin Mar 25 '14 at 03:11
  • See also my answer to [MySQL dynamic cross tab](http://stackoverflow.com/questions/8977855/mysql-dynamic-cross-tab) – Bill Karwin Mar 25 '14 at 03:12

1 Answers1

0

You can apply a case/when and group by the student and the class. If you want only a single person, just apply a where clause... or even if you want all students for a class, or a given school year... etc.

SELECT
      g.studNumber,
      g.subjCode,
      MAX( case when g.period = 1 then g.grade else 0 end ) as 1st_period,
      MAX( case when g.period = 2 then g.grade else 0 end ) as 2nd_period,
      MAX( case when g.period = 3 then g.grade else 0 end ) as 3rd_period,
      MAX( case when g.period = 4 then g.grade else 0 end ) as 4th_period,
      AVG( g.grade ) as Ave
   from
      Grades g
   group by 
      g.studNumber,
      g.subjCode
DRapp
  • 47,638
  • 12
  • 72
  • 142