I am making a "quiz app". Each quiz have multiple questions (quizComponents), and each question has multiple answer options (quizComponentOptions) that the participant can select from. The answers are save in the quizResults table. I have these tables:
I would like to get a list of all quizes, with a total number of components in each quiz, and also number of correct / incorrect answers in each quiz for any given personId. Something like:
quizTitle total_quiz_components correct_answers incorrect_answers
"Quiz 1" 3 2 1
"Quiz 2" 10 1 9
The following query gives me all rows in Quiz, with the total number of quiz components every quiz contains.
SELECT q.quizTitle, count(qc.componentId) AS total_quiz_components FROM Quiz q
LEFT JOIN QuizComponents qc ON qc.quizId=q.quizId
GROUP BY q.quizId;
But then I'm stuck in this relations madness.
you need case based aggregation
to get the correct and incorrect answers.
SELECT q.quizTitle,
count(qc.componentId) AS total_quiz_components ,
sum ( case when CO.optionId = QR.answerOptionId then 1 else 0 end) as correct_answers,
sum ( case when CO.optionId <> QR.answerOptionId then 1 else 0 end) as incorrect_answers,
FROM Quiz q
LEFT JOIN QuizComponents qc ON qc.quizId=q.quizId
LEFT JOIN QuizResults QR on
QR.componentId = QC.componentId
LEFT JOIN CorrectOptions CO
on CO.componentId = QR.componentId
GROUP BY q.quizTitle;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.