I've got a query that returns data like so:
student | course | grade |
---|---|---|
a-student | ENG-W05 | 100 |
a-student | MAT-W05 | 85 |
a-student | ENG-W06 | 100 |
b-student | MAT-W05 | 90 |
b-student | SCI-W05 | 75 |
The data is grouped by student and course. Ideally, I'd like to have the above data transformed into the below:
student | ENG-W05 | MAT-W05 | ENG-W06 | SCI-W05 |
---|---|---|---|---|
a-student | 100 | 85 | 100 | NULL |
b-student | NULL | 90 | NULL | 75 |
So, after the transformation, each student only has one record, with all of their grades (and any missing courses graded as null).
Does anyone have any ideas? Obviously, this is fairly simple to do if I take the data out and transform it in a language (like Python), but I'd love to get the data in the desired format with an SQL query.
Also, would it be possible to have the columns order alphabetically (ascending)? So, the final output would be:
student | ENG-W05 | ENG-W06 | MAT-W05 | SCI-W05 |
---|---|---|---|---|
a-student | 100 | 100 | 85 | NULL |
b-student | NULL | NULL | 90 | 75 |
EDIT: To clarify, the values in course
aren't known. The ones I provided are just examples. So ideally, if more course values found there way into that first query result (the first table), they would still be mapped to columns in the final result (without needing to change the query). In reality, I actually have >1k distinct values for the course
column, and so I can't manually write out each one.