I have a table in SQL used to store result like so
Result
---------------
ID | DateCreated
1 | 2014-10-10
The Items under the result above
ResultItems
---------------
ResultID | StudentID | SubjID | Test1 | Test2 | Exam
1 | 1 | 1 | 7 | 7 | 30
1 | 2 | 1 | 8 | 8 | 35
1 | 1 | 2 | 5 | 5 | 45
1 | 2 | 2 | 6 | 6 | 40
I need to select from this tables so that each subject is in its own column, with the score of each subject summed under it
Result items
Result Output
---------------
StudentID| SubjID-1 | SubjID-2
1 | 44 | 55
2 | 51 | 52
I did try quiet some queries, such as this one below, which didn't give the result I needed
SELECT r.*,
ri.StudentID,
ri.Test1,
ri.Test2,
ri.Exam,
( ri.Test1+ ri.Test2 + ri.Exam ) Total
FROM Result r
LEFT JOIN ResultItems ri
ON ri.ResultID = r.id
WHERE ri.Test1 <> '-'
AND
ri.Test2 <> '-'
AND
ri.exam <> '-';
How can I amend this query?
Edit
I read about Pivot and saw this question SQL - columns for different categories, in which case the names/id of the subject has to be know before hand, which would not work for my case