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 := ` getQuestionsStatement := `
SELECT s.SpørsmålID, s.tekst WITH FilteredQuestions AS (
FROM Spørsmål s SELECT s.SpørsmålID, s.tekst
LEFT JOIN Spørsmålsvar ss ON s.spørsmålID = ss.spørsmålID FROM Spørsmål s
LEFT JOIN SvarVurdering sv ON ss.svarID = sv.svarID AND sv.respondentID = $1 LEFT JOIN (
WHERE sv.vurderingID IS NULL SELECT ss.spørsmålID
ORDER BY RANDOM() FROM Spørsmålsvar ss
LIMIT 5; 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) stmt, err := db.Prepare(getQuestionsStatement)
@ -62,7 +78,7 @@ func GetUserQuestions(respondentID int) ([]UserQuestions, error) {
var questionAnswers []UserQuestions var questionAnswers []UserQuestions
rows, err := stmt.Query(respondentID) rows, err := stmt.Query()
if err != nil { if err != nil {
return questionAnswers, err return questionAnswers, err
} }