I have a table in a database with two three columns ID, NAME, SUBJECT, SCORE
There are only three possible values for subject math, physics,biology
and for each there is a corresponding score in the score field. Because each student can take more than one course, the NAME
column is not unique.
I want to expand the table such that each of the values for subject become a column so i no longer have the subject and score column, In other words I will have NAME, MATH,PHYSICS,BIOLOGY
where the values of math physics and biology are the scores and the NAME
column then becomes unique
I presently do this by using the query
SELECT T1.NAME, T2.SCORE AS BIOLOGY, T3.SCORE AS PHYSICS, T4.SCORE AS MATH
FROM studenttable T1
LEFT JOIN studenttable T2
ON T1.ID = T2.ID AND T2.SUBJECT = 'biology'
LEFT JOIN studenttable T3
ON T1.ID = T3.ID AND T3.SUBJECT = 'physics'
LEFT JOIN studenttable T4
ON T1.ID = T4.ID AND T4.SUBJECT = 'math'
This seems to work but I was wondering if there is a better way to achieve this? or a more tidier way ? Please do tell