This commit is contained in:
pasha1coil 2025-09-13 00:59:30 +03:00
parent 2fbd1b5569
commit 68b5338189
2 changed files with 110 additions and 7 deletions

@ -557,7 +557,8 @@ SELECT
a.content AS last_answer_content, a.content AS last_answer_content,
a.result, a.result,
a.start, a.start,
a.session a.session,
a.quiz_id
FROM FROM
QuizAnswers a QuizAnswers a
LEFT JOIN ( LEFT JOIN (
@ -583,17 +584,13 @@ SELECT
q.page AS question_page, q.page AS question_page,
lc.last_answer_content AS answer_content, lc.last_answer_content AS answer_content,
CAST( 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 COUNT(CASE WHEN lc.result = FALSE THEN 1 END) * 100.0 / NULLIF(SUM(COUNT(CASE WHEN lc.result = FALSE THEN 1 END)) OVER (PARTITION BY q.id), 0) AS FLOAT8
) AS percentage ) AS percentage
FROM FROM
question q question q
JOIN LastContent lc ON q.id = lc.question_id JOIN LastContent lc ON q.id = lc.question_id
JOIN answer a ON q.id = a.question_id AND a.session = lc.session
WHERE WHERE
a.quiz_id = $1 lc.quiz_id = $1
AND a.start = FALSE
AND a.created_at >= TO_TIMESTAMP($2)
AND a.created_at <= TO_TIMESTAMP($3)
GROUP BY GROUP BY
q.id, q.title, lc.last_answer_content q.id, q.title, lc.last_answer_content
HAVING HAVING

@ -48,3 +48,109 @@
-- RETURNING * -- RETURNING *
-- ) -- )
-- SELECT * from inserted_utms; -- SELECT * from inserted_utms;
-- WITH QuizAnswers AS (
-- SELECT
-- session, start, result, question_id, created_at, content, quiz_id
-- FROM answer
-- WHERE answer.quiz_id = 27930 AND created_at between TO_TIMESTAMP(1757785974)::timestamp and TO_TIMESTAMP(1749155806)::timestamp
-- ), QuizQuestions AS (SELECT title, page, id from question where quiz_id = 27930), Funnel AS (
-- SELECT
-- COUNT(DISTINCT a.session) FILTER (WHERE a.start = FALSE) AS count_start_false,
-- COUNT(DISTINCT a.session) FILTER (WHERE a.start = TRUE) AS count_start_true,
-- COUNT(DISTINCT CASE WHEN a.result = FALSE AND qid_true_result IS NOT NULL THEN a.session END) AS count_f_result_with_t_question,
-- COUNT(DISTINCT a.session) FILTER (WHERE a.result = TRUE) AS count_t_result
-- FROM
-- QuizAnswers a
-- LEFT JOIN (
-- SELECT DISTINCT a.session, q.id AS qid_true_result
-- FROM QuizAnswers a
-- JOIN question q ON a.question_id = q.id
-- WHERE a.result = TRUE AND a.quiz_id = 27930
-- ) AS q ON a.session = q.session
-- ),
-- Results AS (
-- SELECT
-- COALESCE(q.title, '') AS question_title,
-- COUNT(*) AS total_answers,
-- CAST(COUNT(*) * 100.0 / NULLIF(SUM(COUNT(*)) FILTER (WHERE a.result = TRUE) OVER (PARTITION BY a.quiz_id), 0) AS FLOAT8) AS percentage
-- FROM
-- question q
-- JOIN answer a ON q.id = a.question_id
-- WHERE
-- a.quiz_id = 27930
-- AND a.created_at >= TO_TIMESTAMP(1757785974)
-- AND a.created_at <= TO_TIMESTAMP(1749155806)
-- AND a.result = TRUE
-- AND a.start = FALSE
-- GROUP BY
-- q.title, a.quiz_id, a.result
-- HAVING
-- COUNT(*) >= 1
-- ),
-- LastContent AS (
-- SELECT
-- a.question_id,
-- a.content AS last_answer_content,
-- a.result,
-- a.start,
-- a.session,
-- a.quiz_id
-- FROM
-- QuizAnswers a
-- LEFT JOIN (
-- SELECT
-- session,
-- question_id,
-- MAX(created_at) AS last_created_at
-- FROM
-- QuizAnswers
-- WHERE
-- quiz_id = 27930
-- AND start = FALSE
-- AND created_at >= TO_TIMESTAMP(1757785974)
-- AND created_at <= TO_TIMESTAMP(1749155806)
-- GROUP BY
-- question_id, session
-- ) AS last_created_at_one_session ON a.session = last_created_at_one_session.session AND a.question_id = last_created_at_one_session.question_id AND a.created_at = last_created_at_one_session.last_created_at
-- WHERE a.start = FALSE
-- ),
-- Questions AS (
-- SELECT
-- q.title AS question_title,
-- q.page AS question_page,
-- lc.last_answer_content AS answer_content,
-- CAST(
-- COUNT(CASE WHEN lc.result = FALSE THEN 1 END) * 100.0 / NULLIF(SUM(COUNT(CASE WHEN lc.result = FALSE THEN 1 END)) OVER (PARTITION BY q.id), 0) AS FLOAT8
-- ) AS percentage
-- FROM
-- question q
-- JOIN LastContent lc ON q.id = lc.question_id
-- WHERE
-- lc.quiz_id = 27930
-- GROUP BY
-- q.id, q.title, lc.last_answer_content
-- HAVING
-- COUNT(*) >= 1
-- )
-- SELECT
-- Funnel.count_start_false,
-- Funnel.count_start_true,
-- Funnel.count_f_result_with_t_question,
-- Funnel.count_t_result,
-- COALESCE(Results.question_title, '') AS results_title,
-- COALESCE(Results.percentage, 0) AS results_percentage,
-- COALESCE(Questions.question_title, '') AS questions_title,
-- COALESCE(Questions.question_page, 0) AS questions_page,
-- COALESCE(Questions.answer_content, '') AS answer_content,
-- COALESCE(Questions.percentage, 0) AS questions_percentage
-- FROM
-- Funnel
-- LEFT JOIN Results ON true
-- LEFT JOIN Questions ON Questions.percentage >= 1;