diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index d64e675..7f6690b 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -500,30 +500,22 @@ WITH Funnel AS ( AND a.created_at <= TO_TIMESTAMP($3) ), Results AS ( - SELECT - q.title AS question_title, - 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 - WHERE - a.quiz_id = $1 - AND a.created_at >= TO_TIMESTAMP($2) - AND a.created_at <= TO_TIMESTAMP($3) - 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 - HAVING - COUNT(*) >= 1 - ), + SELECT + q.title AS question_title, + COUNT(*) AS total_answers, + COUNT(*) * 100.0 / NULLIF(SUM(COUNT(*)) FILTER (WHERE a.result = TRUE) OVER (PARTITION BY a.quiz_id), 0) 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.title, a.quiz_id + HAVING + COUNT(*) >= 1 + ), Questions AS ( SELECT q.title AS question_title, diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index 1853b7a..e3d6998 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -1594,30 +1594,22 @@ WITH Funnel AS ( AND a.created_at <= TO_TIMESTAMP($3) ), Results AS ( - SELECT - q.title AS question_title, - 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 - WHERE - a.quiz_id = $1 - AND a.created_at >= TO_TIMESTAMP($2) - AND a.created_at <= TO_TIMESTAMP($3) - 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 - HAVING - COUNT(*) >= 1 - ), + SELECT + q.title AS question_title, + COUNT(*) AS total_answers, + COUNT(*) * 100.0 / NULLIF(SUM(COUNT(*)) FILTER (WHERE a.result = TRUE) OVER (PARTITION BY a.quiz_id), 0) 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.title, a.quiz_id + HAVING + COUNT(*) >= 1 + ), Questions AS ( SELECT q.title AS question_title, @@ -1667,7 +1659,7 @@ type QuestionsStatisticsRow struct { CountFResultWithTQuestion int64 `db:"count_f_result_with_t_question" json:"count_f_result_with_t_question"` CountTResult int64 `db:"count_t_result" json:"count_t_result"` ResultsTitle string `db:"results_title" json:"results_title"` - ResultsPercentage float64 `db:"results_percentage" json:"results_percentage"` + ResultsPercentage int32 `db:"results_percentage" json:"results_percentage"` QuestionsTitle string `db:"questions_title" json:"questions_title"` AnswerContent sql.NullString `db:"answer_content" json:"answer_content"` QuestionsPercentage float64 `db:"questions_percentage" json:"questions_percentage"`