DRAFT
This query would give solved/unsolved, the first reply | first replier | hours to first reply, as well as the solution post | solver | hours to solution (with category and ‘topic created since’ parameters).
If anyone wants to sanity check it that would be great.
-- [params]
-- int :category_id = 6
-- date :since = 2013-01-01
WITH solved_topics AS (
SELECT ua.target_topic_id AS topic_id,
ua.user_id,
ua.target_post_id AS post_id,
p.created_at
FROM user_actions ua
JOIN posts p on p.id = ua.target_post_id
WHERE action_type = 15
),
first_reply AS (
SELECT p.topic_id,
MIN(id) AS post_id
FROM posts p
WHERE p.deleted_at ISNULL
AND p.post_type = 1
AND p.post_number > 1
GROUP BY p.topic_id
)
SELECT CASE WHEN st.topic_id IS NOT NULL THEN 'Solved' ELSE 'Unsolved' END AS "Solved?",
t.id as topic_id,
t.user_id AS question_user_id,
t.created_at::date AS "Topic Posted On:",
t.views,
p.user_id AS first_reply_user_id,
COALESCE(fr.post_id, 0) AS first_reply_post_id,
COALESCE(CEIL(extract(epoch FROM (p.created_at - t.created_at))/3600.00),0) AS "Time to First Reply (hrs)",
st.user_id AS solved_by_user_id,
COALESCE(st.post_id,0) AS solution_post_id,
COALESCE(CEIL(extract(epoch FROM (st.created_at - t.created_at))/3600.00),0) AS "Time to Solution (hrs)"
FROM topics t
LEFT JOIN solved_topics st ON t.id = st.topic_id
LEFT JOIN first_reply fr ON fr.topic_id = t.id
LEFT JOIN posts p ON p.id = fr.post_id
WHERE t.category_id = :category_id
AND t.created_at >= :since
AND t.deleted_at ISNULL
AND t.visible = TRUE
ORDER BY t.created_at