So I have these following tables that are causing me a bit of a headache. What I want to do is to create a query that returns all assignments for a given course with a assignment info (grade, average grade etc.) or just null in those fields if the student did not hand in or has not handed in his assignment. I have tried left join
but I can't manage to figure it out for those tables. They are old and badly constructed but it's all I have.
CourseInst_Students Holds a registration for a student in an instance of a course
| ID_CourseInst| SSN |
| =============|=====================|
| 24744 | 080590-XXXX | my ssn
Assignments_CourseInst Registers assignments to an instance of a course
| ID_Assignment| ID_CourseInst |
| =============|=====================|
| 37978 | 24744 |
| 37979 | 24744 |
| 37992 | 24744 |
| 38046 | 24744 |
Assignments
| ID_Assignment| Title |
| =============|===========================|
| 37978 | Og þá var kátt í höllinni |
| 37979 | Test for Assignments |
| 37992 | Test 2 |
| 38046 | Eitthvað gott verkefni |
Assignments_Solutions Holds an info (grade, handin date etc.) for a group of students (1 to many). No record if the student/'s have not handed in their solutions.
| ID_Assignment| ID_Group | Grade |
| =============|===========================|=========|
| 37978 | 808046 | 10 | only one group has handed in.
Assignments_Solutions_Groups Registers student's SSN to a group.
| ID_Group | SSN |
| =============|===========================|
| 808046 | 221180-XXXX | not my SSN (see mine above)
My preferred results when searching for assignments for my SSN would be something like:
| ID_Assignment| Title | Grade |
| =============|===========================|=========|
| 37978 | Og þá var kátt í höllinni | NULL |
| 37979 | Test for Assignments | NULL |
| 37992 | Test 2 | NULL |
| 38046 | Eitthvað gott verkefni | NULL |
Can anybody figure this out ? Thanks!
UPDATE
Here is what I've got so far
select A.ID_Assignment, A.Title, ASo.Grade
from CourseInst_Students as CS
join Assignments_CourseInst as AC on CS.ID_CourseInst= AC.ID_CourseInst
join Assignments as A on AC.ID_Assignment = A.ID_Assignment
left join Assignments_Solutions as ASo on A.ID_Assignment = ASo.ID_Assignment
left join Assignments_Solutions_Groups as ASGs on ASo.ID_Group = ASGs.ID_Group
where CS.SSN = '080590-XXXX'
AND CS.id_namskeid = 24744
and I get the following results:
| ID_Assignment| Title | Grade |
| =============|===========================|=========|
| 37978 | Og þá var kátt í höllinni | 10 | <- this is someone else's handin. I want NULL here
| 37979 | Test for Assignments | NULL |
| 37992 | Test 2 | NULL |
| 38046 | Eitthvað gott verkefni | NULL |