For hver spørgsmålsliste er de forventede svar og de indsendte svar (du skal bruge en FULL OUTER JOIN
for at gøre dette, en LEFT
deltage er ikke nok), og tæl antallet af kampe. Sammenlign derefter dette antal med antallet af de forventede svar.
select question_id, case when cnt = sum_test then 1 else 0 end as mark
from (
select question_id, count(*) cnt, sum(test) sum_test
from (
select coalesce(q.question_id, s.question_id) as question_id,
correct_option_id,
submitted_option_id,
case when correct_option_id = submitted_option_id then 1 else 0 end as test
from question_answer q full outer join user_exam_answer s
on q.question_id = s.question_id and q.correct_option_id = s.submitted_option_id
) x
group by question_id
) y
Du kan finde en live demo her