I have the following SQL query that count the distinct quizzes where a tag is below a certain threshold.
SELECT
COUNT(DISTINCT "quizzes"."id")
FROM
"quizzes"
INNER JOIN "sessions" ON "sessions"."id" = "quizzes"."session_id"
INNER JOIN "subscriptions" ON "subscriptions"."id" = "sessions"."subscription_id"
LEFT OUTER JOIN "quiz_answers" ON "quiz_answers"."quiz_id" = "quizzes"."id"
LEFT OUTER JOIN "answers" ON "answers"."id" = "quiz_answers"."answer_id"
LEFT OUTER JOIN "tag_scores" ON "tag_scores"."answer_id" = "answers"."id"
LEFT OUTER JOIN "tags" ON "tags"."id" = "tag_scores"."tag_id"
WHERE
(subscriptions.state = 'subscribed')
AND (
tags.id = 56
and score <= 10
);
The score
is a sum of the same tag present under a same quiz.
I want to be able to add in the query something like this:
SUM(tag_scores.score) group by tags.id, quizzes.id AS score
Such that the score is calculated not on the particular tag score but on the sum of the same tags grouped by tags.id
and quizzes.id
.
The output should be simply the count number of the result.
I'm having a hard time how to add this to the query.
Any idea or hint how to continue?