مواضيع جديدة حسب الفئة (شهرياً)
يعيد عدد المواضيع الجديدة التي تم إنشاؤها في كل فئة خلال شهر معين، مرتبة حسب عدد المواضيع. تقبل الاستعلام معلمة ‘عدد الأشهر المنقضية’، وتكون القيمة الافتراضية 0 لإعطاء نتائج الشهر الحالي.
-- [المعاملات]
-- int :months_ago = 1
WITH query_period as (
SELECT
date_trunc('month', CURRENT_DATE) - INTERVAL ':months_ago months' as period_start,
date_trunc('month', CURRENT_DATE) - INTERVAL ':months_ago months' + INTERVAL '1 month' - INTERVAL '1 second' as period_end
)
SELECT
t.category_id,
count(1) as topic_count
FROM topics t
RIGHT JOIN query_period qp
ON t.created_at >= qp.period_start
AND t.created_at <= qp.period_end
WHERE t.user_id > 0
AND t.category_id IS NOT NULL
GROUP BY t.category_id
ORDER BY topic_count DESC