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($2)
AND a.created_at <= TO_TIMESTAMP($3) 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 ( Results AS (
SELECT SELECT
q.title AS question_title, q.title AS question_title,
ta.total_answers, COUNT(*) AS total_answers,
CAST(COUNT(*) * 100.0 / NULLIF(ta.total_answers, 0) AS INTEGER) AS percentage 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 FROM
answer a answer a
JOIN JOIN question q ON a.question_id = q.id
question q ON a.question_id = q.id
JOIN
TotalAnswers ta ON q.id = ta.question_id
WHERE WHERE
a.quiz_id = $1 a.quiz_id = $1
AND a.created_at >= TO_TIMESTAMP($2) AND a.created_at >= TO_TIMESTAMP($2)
AND a.created_at <= TO_TIMESTAMP($3) 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 GROUP BY
q.id, q.title, ta.total_answers q.id, q.title
HAVING HAVING
COUNT(*) >= 1 COUNT(*) >= 1
), ),
Questions AS ( Questions AS (
SELECT SELECT
q.title AS question_title, q.title AS question_title,
a.content AS answer_content, a.content AS answer_content,
CAST( 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 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 ) AS percentage
FROM FROM
question q question q
JOIN answer a ON q.id = a.question_id JOIN answer a ON q.id = a.question_id
WHERE WHERE
a.quiz_id = $1 a.quiz_id = $1
AND a.created_at >= TO_TIMESTAMP($2) AND a.created_at >= TO_TIMESTAMP($2)
AND a.created_at <= TO_TIMESTAMP($3) AND a.created_at <= TO_TIMESTAMP($3)
GROUP BY GROUP BY
q.id, q.title, a.content q.id, q.title, a.content
HAVING HAVING
COUNT(*) >= 1 COUNT(*) >= 1
) )
SELECT SELECT
Funnel.count_start_false, Funnel.count_start_false,

@ -140,7 +140,7 @@ func (r *StatisticsRepository) GetQuestionsStatistics(ctx context.Context, req D
if resp.Questions[row.QuestionsTitle] == nil { if resp.Questions[row.QuestionsTitle] == nil {
resp.Questions[row.QuestionsTitle] = make(map[string]float64) 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 return resp, nil