Question

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.

Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top