lundi 12 janvier 2015

Select items by column value

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 didnt 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 <> '-';


Can you help with the query


Aucun commentaire:

Enregistrer un commentaire