초안
이 쿼리는 해결됨/미해결 여부, 첫 번째 답변 | 첫 번째 답변 작성자 | 첫 번째 답변까지 소요 시간(시간), 그리고 해결 게시글 | 해결자 | 해결까지 소요 시간(시간)을 반환합니다(카테고리 및 ‘주제 생성 이후’ 파라미터 포함).
혹시 확인해 주실 분이 있다면 좋겠습니다.
![]()
-- [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