I think something like this would give you the numbers youâre after:
-- [params]
-- date :start_date
-- date :end_date
WITH staff_data AS (
SELECT user_id, true as is_staff
FROM group_users
WHERE group_id = 3
),
month_stats AS (
SELECT
date_trunc('month', p.created_at)::date AS month,
COUNT(*) FILTER (WHERE p.post_number = 1) AS total_topics,
COUNT(*) FILTER (WHERE p.post_number <> 1) AS total_posts,
COUNT(DISTINCT p.user_id) FILTER (WHERE p.post_number <> 1 AND is_staff IS NOT TRUE) AS non_staff_users,
COUNT(*) FILTER (WHERE p.post_number <> 1 AND is_staff IS NOT TRUE) AS non_staff_posts,
COUNT(DISTINCT p.user_id) FILTER (WHERE p.post_number <> 1 AND is_staff) AS staff_users,
COUNT(*) FILTER (WHERE p.post_number <> 1 AND is_staff) AS staff_posts
FROM posts p
LEFT JOIN topics t ON t.id = p.topic_id
LEFT JOIN staff_data s ON p.user_id = s.user_id
WHERE p.created_at::date BETWEEN :start_date AND :end_date
AND t.archetype = 'regular'
AND t.deleted_at IS NULL
AND p.deleted_at IS NULL
AND p.post_type = 1
AND p.user_id > 0
GROUP BY month
)
SELECT
ms.month AS "Month",
ms.total_topics AS "All Topics",
ms.total_posts AS "All Posts",
ms.non_staff_posts AS "Non-staff posts",
ROUND(ms.non_staff_posts * 100.0 / NULLIF(ms.total_posts, 0),1) || '%' AS "Non-staff posts (% of total)",
ms.non_staff_posts / NULLIF(ms.total_topics, 0) AS "Average non-staff posts per topic",
ms.non_staff_users AS "Non-staff users who posted",
ms.staff_posts AS "Staff posts",
ROUND(ms.staff_posts * 100.0 / NULLIF(ms.total_posts, 0),1) || '%' AS "Staff posts (% of total)",
ms.staff_posts / NULLIF(ms.total_topics, 0) AS "Average staff posts per topic",
ms.staff_users AS "Staff users who posted"
FROM month_stats ms
ORDER BY "Month"
Which would give you something like this:
And a bit of blurb thrown in for good measure:
This query is designed to provide a monthly statistical summary of forum activity, specifically focusing on the distinction between contributions from âstaffâ and ânon-staffâ users within a given time frame. The metrics calculated include the total number of topics created, all posts made, the number of unique non-staff users who posted, the count and percentage of posts made by non-staff, the average number of non-staff posts per topic, as well as the matching figures for staff members. The information is intended to give insights into user engagement, content generation, and the participation rate of staff versus non-staff members in the forumâs discussions. The query ensures accuracy by only considering âregularâ (non-PM) topics and excludes any deleted posts or topics, whispers/small posts/moderator actions, and posts by system users, within the specified date range.
For this one, the âstaffâ criteria is that theyâre in the automatic @staff
group, which includes both admins and mods - though this can be adjusted to just target the admins only, or even a custom group of employees who arenât technically âsite staffâ as such. If you definitively want to exclude moderators you can swop that group_id
at the start to â1â instead.
Is that the kind of thing youâre looking for?