Topics stats of Solved/Unsolved topics, inc. 'time to solution'

:warning: 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. :slight_smile: :+1:

-- [params]
-- int :category_id = 6
-- date :since = 2013-01-01

WITH solved_topics AS (

SELECT ua.target_topic_id AS topic_id,
       ua.target_post_id AS post_id,
FROM user_actions ua
JOIN posts p on = 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?", as topic_id,
       t.user_id AS question_user_id,
       t.created_at::date AS "Topic Posted On:",
       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 = st.topic_id
LEFT JOIN first_reply fr ON fr.topic_id =
LEFT JOIN posts p ON = 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