특정 카테고리의 월별 주제 수 조회

데이터 탐색기에 더 익숙하신 분이 계신다면 도움을 주실 수 있을까요?

특정 카테고리에 대해 월별 주제 수 통계를 얻고 싶습니다. 실행된 날로부터 1년 치 데이터만으로도 충분합니다.

도움 주시면 감사하겠습니다.

(Superseded) What cool data explorer queries have you come up with? 의 예시 중 일부를 활용하면 원하시는 결과에 가까워질 수 있을 것 같습니다. (sql-query 태그에도 관련 주제가 많이 있습니다.)

해당 게시글들을 살펴본 뒤 원하시는 것과 일치하는 내용이 있는지 확인해 보세요. 도움이 필요하시면 구체적으로 다듬어 드릴 수 있습니다. :+1:


더 깔끔한 방법이 있을 수 있지만, 아래와 같은 방식으로 시도해 볼 수 있습니다.

-- [params]
-- int :cat_id = 6
-- date :date_from = 01/03/2022
-- date :date_to = 01/04/2022


SELECT t.category_id, count(t.category_id)
FROM topics t
WHERE t.category_id = :cat_id
AND t.created_at::date BETWEEN :date_from::date AND :date_to::date
AND t.deleted_at is null 
GROUP BY t.category_id

잠깐. 이렇게 하면 월별 집계로 나오지 않네요. 다시 한번 시도해 볼게요…


아래 방식이 더 잘 될 것 같습니다. :slightly_smiling_face: 이런 건 어떨까요?

-- [params]
-- int :cat_id = 5
-- int :months = 12


    SELECT 
        date_part('year', created_at) AS year, 
        date_part('month', created_at) AS month,
        COUNT(category_id) AS "new_topics_month"
    FROM topics t
    WHERE t.category_id = :cat_id
    AND t.deleted_at is NULL
    GROUP BY date_part('year', created_at), date_part('month', created_at) 
    ORDER BY date_part('year', created_at) DESC, date_part('month', created_at) DESC
    LIMIT :months