Привет!
Я надеюсь понять, какой процент новых тем в определённой категории не получил ответа в первую неделю и какой процент был решён в первую неделю. Вот мой запрос:
-- [params]
-- string :start_date = 2021-10-06
-- string :end_date = 2021-10-12
WITH solved_topics as
(SELECT
t.created_at::DATE,
t.id,
count(distinct case when u.action_type=15 and u.created_at < (t.created_at + INTERVAL '1 week') and u.target_topic_id=t.id then 1 ELSE NULL END) as solves,
count(distinct case when p.created_at < (t.created_at + INTERVAL '1 week') and p.topic_id=t.id and p.post_number>1 then 1 ELSE NULL END) as responses
from topics t
join user_actions as u
on u.target_topic_id = t.id
join posts as p
on p.topic_id = t.id
where t.created_at BETWEEN :start_date AND :end_date
AND t.user_id > 0
AND p.deleted_at is NULL
AND t.category_id is NOT NULL
group by t.created_at::DATE, t.id)
SELECT
st.created_at as date,
count(distinct st.id) as "Number of New Topics",
sum(st.solves)::INT as "# Solved within a week",
round(sum(st.solves)/count(distinct st.id), 2) as "% Solved",
(count(distinct st.id) - sum(st.responses))::INT as "Topics with No Response in First Week",
round((count(distinct st.id) - sum(st.responses))/count(distinct st.id), 2) as "% with No Response in First Week"
from solved_topics as st
group by date
order by date asc
Я уверен, что есть более изящные способы сделать это, но я проверил несколько дат вручную, и эти числа соответствуют тому, что я вижу, просматривая отдельные темы.
Я предполагаю, что для любого дня за последнюю неделю отчёт, доступный по адресу /admin/reports/topics_with_no_response, должен показывать одинаковое количество тем без ответа. В большинстве случаев так и есть, но в других случаях он показывает больше тем без ответа, чем я получаю в своём запросе.
Есть ли какие-либо идеи, почему это может происходить? Есть ли способ увидеть запрос, который формирует отчёт «Темы без ответа», чтобы я мог провести сравнение?
Заранее спасибо!