update repo method

This commit is contained in:
Pavel 2024-03-17 22:08:19 +03:00
parent d65f806e0c
commit 195bb48824
2 changed files with 30 additions and 40 deletions

@ -499,59 +499,49 @@ WITH Funnel AS (
AND a.created_at >= TO_TIMESTAMP($2)
AND a.created_at <= TO_TIMESTAMP($3)
),
TotalAnswers AS (
SELECT
q.id AS question_id,
COUNT(*) AS total_answers
FROM
question q
JOIN answer a ON q.id = a.question_id
WHERE
a.quiz_id = $1
AND a.created_at >= TO_TIMESTAMP($2)
AND a.created_at <= TO_TIMESTAMP($3)
GROUP BY
q.id
),
Results AS (
SELECT
q.title AS question_title,
ta.total_answers,
CAST(COUNT(*) * 100.0 / NULLIF(ta.total_answers, 0) AS INTEGER) AS percentage
COUNT(*) AS total_answers,
CAST(
COUNT(CASE WHEN a.result = TRUE THEN 1 ELSE NULL END) * 100.0 / NULLIF(SUM(COUNT(CASE WHEN a.result = TRUE THEN 1 ELSE NULL END)) OVER (PARTITION BY q.id), 0) AS FLOAT8
) AS percentage
FROM
answer a
JOIN
question q ON a.question_id = q.id
JOIN
TotalAnswers ta ON q.id = ta.question_id
JOIN question q ON a.question_id = q.id
WHERE
a.quiz_id = $1
AND a.created_at >= TO_TIMESTAMP($2)
AND a.created_at <= TO_TIMESTAMP($3)
AND a.result = TRUE
AND q.id IN (
SELECT DISTINCT a.question_id
FROM answer a
WHERE a.quiz_id = $1
AND a.result = TRUE
)
GROUP BY
q.id, q.title, ta.total_answers
q.id, q.title
HAVING
COUNT(*) >= 1
),
Questions AS (
SELECT
q.title AS question_title,
a.content AS answer_content,
CAST(
COUNT(CASE WHEN a.result = FALSE THEN 1 ELSE NULL END) * 100.0 / NULLIF(SUM(COUNT(CASE WHEN a.result = FALSE THEN 1 ELSE NULL END)) OVER (PARTITION BY q.id), 0) AS FLOAT8
) AS percentage
FROM
question q
JOIN answer a ON q.id = a.question_id
WHERE
a.quiz_id = $1
AND a.created_at >= TO_TIMESTAMP($2)
AND a.created_at <= TO_TIMESTAMP($3)
GROUP BY
q.id, q.title, a.content
HAVING
COUNT(*) >= 1
SELECT
q.title AS question_title,
a.content AS answer_content,
CAST(
COUNT(CASE WHEN a.result = FALSE THEN 1 END) * 100.0 / NULLIF(SUM(COUNT(CASE WHEN a.result = FALSE THEN 1 END)) OVER (PARTITION BY q.id), 0) AS FLOAT8
) AS percentage
FROM
question q
JOIN answer a ON q.id = a.question_id
WHERE
a.quiz_id = $1
AND a.created_at >= TO_TIMESTAMP($2)
AND a.created_at <= TO_TIMESTAMP($3)
GROUP BY
q.id, q.title, a.content
HAVING
COUNT(*) >= 1
)
SELECT
Funnel.count_start_false,

@ -140,7 +140,7 @@ func (r *StatisticsRepository) GetQuestionsStatistics(ctx context.Context, req D
if resp.Questions[row.QuestionsTitle] == nil {
resp.Questions[row.QuestionsTitle] = make(map[string]float64)
}
resp.Questions[row.QuestionsTitle][row.AnswerContent.String] = float64(row.QuestionsPercentage)
resp.Questions[row.QuestionsTitle][row.AnswerContent.String] = row.QuestionsPercentage
}
return resp, nil