I am trying to group a student result based on subject. but I want the column to be sorted in descending order. for instance the columns: ca1, ca2, ca3, exam and total should be sorted in THIRD TERM only.
The code below is what I have tried:
select subject, ca1, ca2, ca3, score, total, term,
MAX(CASE WHEN (term = 'FIRST TERM') THEN total END) AS 'fterm',
MAX(CASE WHEN (term = 'SECOND TERM') THEN total END) AS 'sterm',
MAX(CASE WHEN (term = 'THIRD TERM') THEN total END) AS 'tterm'
from results where school_code = :code AND class = :class AND year =:year AND score != 0
GROUP BY subject order by term desc
1