diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index f38204b..d1754fd 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -518,7 +518,10 @@ WITH Funnel AS ( SELECT q.title AS question_title, ta.total_answers, - COUNT(*)::FLOAT / NULLIF(ta.total_answers, 0) AS percentage + CASE + WHEN ta.total_answers = 0 THEN NULL + ELSE COUNT(*)::FLOAT / NULLIF(ta.total_answers, 0) + END AS percentage FROM answer a JOIN @@ -536,31 +539,34 @@ WITH Funnel AS ( COUNT(*) >= 1 ), Questions AS ( - SELECT - q.id, - q.title AS question_title, - a.content AS answer_content, - CAST(COUNT(*)::FLOAT / NULLIF(q.total_answers, 0) AS INTEGER) AS percentage - FROM - answer a - JOIN ( - SELECT q.id, q.title, 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, q.title - ) 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) - GROUP BY - q.id, q.title, a.content, q.total_answers - HAVING - COUNT(*) >= 1 - ) + SELECT + q.id, + q.title AS question_title, + a.content AS answer_content, + CASE + WHEN q.total_answers = 0 THEN NULL + ELSE COUNT(*)::FLOAT / NULLIF(q.total_answers, 0) + END AS percentage + FROM + answer a + JOIN ( + SELECT q.id, q.title, 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, q.title + ) 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) + GROUP BY + q.id, q.title, a.content, q.total_answers + HAVING + COUNT(*) >= 1 + ) SELECT Funnel.count_start_false, Funnel.count_start_true, @@ -575,4 +581,6 @@ SELECT FROM Funnel, Results, - Questions; + Questions +WHERE + Questions.percentage >= 1; diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index e720399..b93a8f6 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -1612,7 +1612,10 @@ WITH Funnel AS ( SELECT q.title AS question_title, ta.total_answers, - COUNT(*)::FLOAT / NULLIF(ta.total_answers, 0) AS percentage + CASE + WHEN ta.total_answers = 0 THEN NULL + ELSE COUNT(*)::FLOAT / NULLIF(ta.total_answers, 0) + END AS percentage FROM answer a JOIN @@ -1630,31 +1633,34 @@ WITH Funnel AS ( COUNT(*) >= 1 ), Questions AS ( - SELECT - q.id, - q.title AS question_title, - a.content AS answer_content, - CAST(COUNT(*)::FLOAT / NULLIF(q.total_answers, 0) AS INTEGER) AS percentage - FROM - answer a - JOIN ( - SELECT q.id, q.title, 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, q.title - ) 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) - GROUP BY - q.id, q.title, a.content, q.total_answers - HAVING - COUNT(*) >= 1 - ) + SELECT + q.id, + q.title AS question_title, + a.content AS answer_content, + CASE + WHEN q.total_answers = 0 THEN NULL + ELSE COUNT(*)::FLOAT / NULLIF(q.total_answers, 0) + END AS percentage + FROM + answer a + JOIN ( + SELECT q.id, q.title, 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, q.title + ) 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) + GROUP BY + q.id, q.title, a.content, q.total_answers + HAVING + COUNT(*) >= 1 + ) SELECT Funnel.count_start_false, Funnel.count_start_true, @@ -1670,6 +1676,8 @@ FROM Funnel, Results, Questions +WHERE + Questions.percentage >= 1 ` type QuestionsStatisticsParams struct { @@ -1685,10 +1693,10 @@ type QuestionsStatisticsRow struct { CountTStartWithTQuestion int64 `db:"count_t_start_with_t_question" json:"count_t_start_with_t_question"` CountTResult int64 `db:"count_t_result" json:"count_t_result"` ResultsTitle string `db:"results_title" json:"results_title"` - ResultsPercentage int32 `db:"results_percentage" json:"results_percentage"` + ResultsPercentage interface{} `db:"results_percentage" json:"results_percentage"` QuestionsTitle string `db:"questions_title" json:"questions_title"` AnswerContent sql.NullString `db:"answer_content" json:"answer_content"` - QuestionsPercentage int32 `db:"questions_percentage" json:"questions_percentage"` + QuestionsPercentage interface{} `db:"questions_percentage" json:"questions_percentage"` } func (q *Queries) QuestionsStatistics(ctx context.Context, arg QuestionsStatisticsParams) ([]QuestionsStatisticsRow, error) {