get questions based on answercount

Co-authored-by: haraldnilsen <harald_998@hotmail.com>
Signed-off-by: Sindre Kjelsrud <kjelsrudsindre@gmail.com>
This commit is contained in:
Sindre Kjelsrud 2024-01-14 13:08:36 +01:00
parent 35545070a9
commit bda57ee28e
Signed by untrusted user who does not match committer: sidski
GPG key ID: D2BBDF3EDE6BA9A6

View file

@ -45,13 +45,29 @@ func GetUserQuestions(respondentID int) ([]UserQuestions, error) {
}
getQuestionsStatement := `
SELECT s.SpørsmålID, s.tekst
FROM Spørsmål s
LEFT JOIN Spørsmålsvar ss ON s.spørsmålID = ss.spørsmålID
LEFT JOIN SvarVurdering sv ON ss.svarID = sv.svarID AND sv.respondentID = $1
WHERE sv.vurderingID IS NULL
ORDER BY RANDOM()
LIMIT 5;
WITH FilteredQuestions AS (
SELECT s.SpørsmålID, s.tekst
FROM Spørsmål s
LEFT JOIN (
SELECT ss.spørsmålID
FROM Spørsmålsvar ss
LEFT JOIN SvarVurdering sv ON ss.svarID = sv.svarID
GROUP BY ss.spørsmålID
HAVING COUNT(sv.vurderingID) < 10
) AS subquery ON s.spørsmålID = subquery.spørsmålID
WHERE subquery.spørsmålID IS NOT NULL
LIMIT 5
)
SELECT * FROM FilteredQuestions
UNION ALL
SELECT * FROM (
SELECT s.SpørsmålID, s.tekst
FROM Spørsmål s
ORDER BY RANDOM()
LIMIT 5
) RandomQuestions
WHERE NOT EXISTS (SELECT 1 FROM FilteredQuestions)
`
stmt, err := db.Prepare(getQuestionsStatement)
@ -62,7 +78,7 @@ func GetUserQuestions(respondentID int) ([]UserQuestions, error) {
var questionAnswers []UserQuestions
rows, err := stmt.Query(respondentID)
rows, err := stmt.Query()
if err != nil {
return questionAnswers, err
}