86ms
This commit is contained in:
parent
fad3296f33
commit
58e924bf7e
@ -511,87 +511,82 @@ FROM
|
|||||||
AND tb.time_interval_end = at.time_interval_end;
|
AND tb.time_interval_end = at.time_interval_end;
|
||||||
|
|
||||||
-- name: QuestionsStatistics :many
|
-- name: QuestionsStatistics :many
|
||||||
WITH Funnel AS (
|
WITH QuizAnswers AS (
|
||||||
|
SELECT
|
||||||
|
session, start, result, question_id, created_at, content, quiz_id
|
||||||
|
FROM answer
|
||||||
|
WHERE answer.quiz_id = $1 AND created_at between TO_TIMESTAMP($2)::timestamp and TO_TIMESTAMP($3)::timestamp
|
||||||
|
), QuizQuestions AS (SELECT title, page, id from question where quiz_id = $1), Funnel AS (
|
||||||
SELECT
|
SELECT
|
||||||
COUNT(DISTINCT a.session) FILTER (WHERE a.start = FALSE) AS count_start_false,
|
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 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 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
|
COUNT(DISTINCT a.session) FILTER (WHERE a.result = TRUE) AS count_t_result
|
||||||
FROM
|
FROM
|
||||||
answer a
|
QuizAnswers a
|
||||||
LEFT JOIN (
|
LEFT JOIN (
|
||||||
SELECT DISTINCT a.session, q.id AS qid_true_result
|
SELECT DISTINCT a.session, q.id AS qid_true_result
|
||||||
FROM answer a
|
FROM QuizAnswers a
|
||||||
JOIN question q ON a.question_id = q.id
|
JOIN question q ON a.question_id = q.id
|
||||||
WHERE a.result = TRUE AND q.quiz_id = $1
|
WHERE a.result = TRUE
|
||||||
) AS q ON a.session = q.session
|
) AS q ON a.session = q.session
|
||||||
WHERE
|
|
||||||
a.quiz_id = $1
|
|
||||||
AND a.created_at >= TO_TIMESTAMP($2)
|
|
||||||
AND a.created_at <= TO_TIMESTAMP($3)
|
|
||||||
),
|
),
|
||||||
Results AS (
|
Results AS (
|
||||||
SELECT
|
SELECT
|
||||||
COALESCE(q.title, '') AS question_title,
|
COALESCE(q.title, '') AS question_title,
|
||||||
COUNT(*) AS total_answers,
|
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
|
CAST(COUNT(*) * 100.0 / NULLIF(SUM(COUNT(*)) FILTER (WHERE a.result = TRUE) OVER (PARTITION BY a.quiz_id), 0) AS FLOAT8) AS percentage
|
||||||
FROM
|
FROM
|
||||||
question q
|
question q
|
||||||
JOIN answer a ON q.id = a.question_id
|
LEFT JOIN QuizAnswers a ON q.id = a.question_id
|
||||||
WHERE
|
WHERE
|
||||||
a.quiz_id = $1
|
a.result = TRUE
|
||||||
AND a.created_at >= TO_TIMESTAMP($2)
|
GROUP BY
|
||||||
AND a.created_at <= TO_TIMESTAMP($3)
|
q.title, a.result, a.quiz_id
|
||||||
AND a.result = TRUE
|
HAVING
|
||||||
GROUP BY
|
COUNT(*) >= 1
|
||||||
q.title, a.quiz_id, a.result
|
),
|
||||||
HAVING
|
|
||||||
COUNT(*) >= 1
|
|
||||||
),
|
|
||||||
LastContent AS (
|
LastContent AS (
|
||||||
SELECT
|
SELECT
|
||||||
a.question_id,
|
a.question_id,
|
||||||
a.content AS last_answer_content
|
a.content AS last_answer_content,
|
||||||
|
a.result,
|
||||||
|
a.start
|
||||||
FROM
|
FROM
|
||||||
answer a
|
QuizAnswers a
|
||||||
JOIN (
|
LEFT JOIN (
|
||||||
SELECT
|
SELECT
|
||||||
session,
|
session,
|
||||||
question_id,
|
question_id,
|
||||||
MAX(created_at) AS last_created_at
|
MAX(created_at) AS last_created_at
|
||||||
FROM
|
FROM
|
||||||
answer
|
QuizAnswers
|
||||||
WHERE
|
WHERE
|
||||||
quiz_id = $1
|
start != true
|
||||||
AND start != true
|
|
||||||
AND created_at >= TO_TIMESTAMP($2)
|
|
||||||
AND created_at <= TO_TIMESTAMP($3)
|
|
||||||
GROUP BY
|
GROUP BY
|
||||||
question_id, session
|
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
|
) 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
|
||||||
),
|
),
|
||||||
Questions AS (
|
Questions AS (
|
||||||
SELECT
|
SELECT
|
||||||
q.title AS question_title,
|
q.title AS question_title,
|
||||||
q.page AS question_page,
|
MAX(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
|
LastContent lc
|
||||||
JOIN LastContent lc ON q.id = lc.question_id
|
JOIN QuizQuestions q ON q.id = lc.question_id
|
||||||
JOIN answer a ON q.id = a.question_id
|
WHERE
|
||||||
WHERE
|
lc.start != true
|
||||||
a.quiz_id = $1
|
GROUP BY
|
||||||
AND a.start != true
|
q.id, q.title, lc.last_answer_content
|
||||||
AND a.created_at >= TO_TIMESTAMP($2)
|
HAVING
|
||||||
AND a.created_at <= TO_TIMESTAMP($3)
|
COUNT(*) >= 1
|
||||||
GROUP BY
|
)
|
||||||
q.id, q.title, lc.last_answer_content
|
|
||||||
HAVING
|
|
||||||
COUNT(*) >= 1
|
|
||||||
)
|
|
||||||
SELECT
|
SELECT
|
||||||
Funnel.count_start_false,
|
Funnel.count_start_false,
|
||||||
Funnel.count_start_true,
|
Funnel.count_start_true,
|
||||||
@ -605,8 +600,8 @@ SELECT
|
|||||||
COALESCE(Questions.percentage, 0) AS questions_percentage
|
COALESCE(Questions.percentage, 0) AS questions_percentage
|
||||||
FROM
|
FROM
|
||||||
Funnel
|
Funnel
|
||||||
LEFT JOIN Results ON true
|
LEFT JOIN Results ON true
|
||||||
LEFT JOIN Questions ON Questions.percentage >= 1;
|
LEFT JOIN Questions ON Questions.percentage >= 1;
|
||||||
|
|
||||||
-- name: QuizCopyQid :one
|
-- name: QuizCopyQid :one
|
||||||
INSERT INTO quiz (
|
INSERT INTO quiz (
|
||||||
|
@ -3164,87 +3164,82 @@ func (q *Queries) MoveToHistoryQuiz(ctx context.Context, arg MoveToHistoryQuizPa
|
|||||||
}
|
}
|
||||||
|
|
||||||
const questionsStatistics = `-- name: QuestionsStatistics :many
|
const questionsStatistics = `-- name: QuestionsStatistics :many
|
||||||
WITH Funnel AS (
|
WITH QuizAnswers AS (
|
||||||
|
SELECT
|
||||||
|
session, start, result, question_id, created_at, content, quiz_id
|
||||||
|
FROM answer
|
||||||
|
WHERE answer.quiz_id = $1 AND created_at between TO_TIMESTAMP($2)::timestamp and TO_TIMESTAMP($3)::timestamp
|
||||||
|
), QuizQuestions AS (SELECT title, page, id from question where quiz_id = $1), Funnel AS (
|
||||||
SELECT
|
SELECT
|
||||||
COUNT(DISTINCT a.session) FILTER (WHERE a.start = FALSE) AS count_start_false,
|
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 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 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
|
COUNT(DISTINCT a.session) FILTER (WHERE a.result = TRUE) AS count_t_result
|
||||||
FROM
|
FROM
|
||||||
answer a
|
QuizAnswers a
|
||||||
LEFT JOIN (
|
LEFT JOIN (
|
||||||
SELECT DISTINCT a.session, q.id AS qid_true_result
|
SELECT DISTINCT a.session, q.id AS qid_true_result
|
||||||
FROM answer a
|
FROM QuizAnswers a
|
||||||
JOIN question q ON a.question_id = q.id
|
JOIN question q ON a.question_id = q.id
|
||||||
WHERE a.result = TRUE AND q.quiz_id = $1
|
WHERE a.result = TRUE
|
||||||
) AS q ON a.session = q.session
|
) AS q ON a.session = q.session
|
||||||
WHERE
|
|
||||||
a.quiz_id = $1
|
|
||||||
AND a.created_at >= TO_TIMESTAMP($2)
|
|
||||||
AND a.created_at <= TO_TIMESTAMP($3)
|
|
||||||
),
|
),
|
||||||
Results AS (
|
Results AS (
|
||||||
SELECT
|
SELECT
|
||||||
COALESCE(q.title, '') AS question_title,
|
COALESCE(q.title, '') AS question_title,
|
||||||
COUNT(*) AS total_answers,
|
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
|
CAST(COUNT(*) * 100.0 / NULLIF(SUM(COUNT(*)) FILTER (WHERE a.result = TRUE) OVER (PARTITION BY a.quiz_id), 0) AS FLOAT8) AS percentage
|
||||||
FROM
|
FROM
|
||||||
question q
|
question q
|
||||||
JOIN answer a ON q.id = a.question_id
|
LEFT JOIN QuizAnswers a ON q.id = a.question_id
|
||||||
WHERE
|
WHERE
|
||||||
a.quiz_id = $1
|
a.result = TRUE
|
||||||
AND a.created_at >= TO_TIMESTAMP($2)
|
GROUP BY
|
||||||
AND a.created_at <= TO_TIMESTAMP($3)
|
q.title, a.result, a.quiz_id
|
||||||
AND a.result = TRUE
|
HAVING
|
||||||
GROUP BY
|
COUNT(*) >= 1
|
||||||
q.title, a.quiz_id, a.result
|
),
|
||||||
HAVING
|
|
||||||
COUNT(*) >= 1
|
|
||||||
),
|
|
||||||
LastContent AS (
|
LastContent AS (
|
||||||
SELECT
|
SELECT
|
||||||
a.question_id,
|
a.question_id,
|
||||||
a.content AS last_answer_content
|
a.content AS last_answer_content,
|
||||||
|
a.result,
|
||||||
|
a.start
|
||||||
FROM
|
FROM
|
||||||
answer a
|
QuizAnswers a
|
||||||
JOIN (
|
LEFT JOIN (
|
||||||
SELECT
|
SELECT
|
||||||
session,
|
session,
|
||||||
question_id,
|
question_id,
|
||||||
MAX(created_at) AS last_created_at
|
MAX(created_at) AS last_created_at
|
||||||
FROM
|
FROM
|
||||||
answer
|
QuizAnswers
|
||||||
WHERE
|
WHERE
|
||||||
quiz_id = $1
|
start != true
|
||||||
AND start != true
|
|
||||||
AND created_at >= TO_TIMESTAMP($2)
|
|
||||||
AND created_at <= TO_TIMESTAMP($3)
|
|
||||||
GROUP BY
|
GROUP BY
|
||||||
question_id, session
|
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
|
) 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
|
||||||
),
|
),
|
||||||
Questions AS (
|
Questions AS (
|
||||||
SELECT
|
SELECT
|
||||||
q.title AS question_title,
|
q.title AS question_title,
|
||||||
q.page AS question_page,
|
MAX(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
|
LastContent lc
|
||||||
JOIN LastContent lc ON q.id = lc.question_id
|
JOIN QuizQuestions q ON q.id = lc.question_id
|
||||||
JOIN answer a ON q.id = a.question_id
|
WHERE
|
||||||
WHERE
|
lc.start != true
|
||||||
a.quiz_id = $1
|
GROUP BY
|
||||||
AND a.start != true
|
q.id, q.title, lc.last_answer_content
|
||||||
AND a.created_at >= TO_TIMESTAMP($2)
|
HAVING
|
||||||
AND a.created_at <= TO_TIMESTAMP($3)
|
COUNT(*) >= 1
|
||||||
GROUP BY
|
)
|
||||||
q.id, q.title, lc.last_answer_content
|
|
||||||
HAVING
|
|
||||||
COUNT(*) >= 1
|
|
||||||
)
|
|
||||||
SELECT
|
SELECT
|
||||||
Funnel.count_start_false,
|
Funnel.count_start_false,
|
||||||
Funnel.count_start_true,
|
Funnel.count_start_true,
|
||||||
@ -3258,8 +3253,8 @@ SELECT
|
|||||||
COALESCE(Questions.percentage, 0) AS questions_percentage
|
COALESCE(Questions.percentage, 0) AS questions_percentage
|
||||||
FROM
|
FROM
|
||||||
Funnel
|
Funnel
|
||||||
LEFT JOIN Results ON true
|
LEFT JOIN Results ON true
|
||||||
LEFT JOIN Questions ON Questions.percentage >= 1
|
LEFT JOIN Questions ON Questions.percentage >= 1
|
||||||
`
|
`
|
||||||
|
|
||||||
type QuestionsStatisticsParams struct {
|
type QuestionsStatisticsParams struct {
|
||||||
@ -3269,16 +3264,16 @@ type QuestionsStatisticsParams struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type QuestionsStatisticsRow struct {
|
type QuestionsStatisticsRow struct {
|
||||||
CountStartFalse int64 `db:"count_start_false" json:"count_start_false"`
|
CountStartFalse int64 `db:"count_start_false" json:"count_start_false"`
|
||||||
CountStartTrue int64 `db:"count_start_true" json:"count_start_true"`
|
CountStartTrue int64 `db:"count_start_true" json:"count_start_true"`
|
||||||
CountFResultWithTQuestion int64 `db:"count_f_result_with_t_question" json:"count_f_result_with_t_question"`
|
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"`
|
CountTResult int64 `db:"count_t_result" json:"count_t_result"`
|
||||||
ResultsTitle string `db:"results_title" json:"results_title"`
|
ResultsTitle string `db:"results_title" json:"results_title"`
|
||||||
ResultsPercentage float64 `db:"results_percentage" json:"results_percentage"`
|
ResultsPercentage float64 `db:"results_percentage" json:"results_percentage"`
|
||||||
QuestionsTitle string `db:"questions_title" json:"questions_title"`
|
QuestionsTitle string `db:"questions_title" json:"questions_title"`
|
||||||
QuestionsPage int16 `db:"questions_page" json:"questions_page"`
|
QuestionsPage interface{} `db:"questions_page" json:"questions_page"`
|
||||||
AnswerContent string `db:"answer_content" json:"answer_content"`
|
AnswerContent string `db:"answer_content" json:"answer_content"`
|
||||||
QuestionsPercentage float64 `db:"questions_percentage" json:"questions_percentage"`
|
QuestionsPercentage float64 `db:"questions_percentage" json:"questions_percentage"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *Queries) QuestionsStatistics(ctx context.Context, arg QuestionsStatisticsParams) ([]QuestionsStatisticsRow, error) {
|
func (q *Queries) QuestionsStatistics(ctx context.Context, arg QuestionsStatisticsParams) ([]QuestionsStatisticsRow, error) {
|
||||||
|
Loading…
Reference in New Issue
Block a user