Is there a way to get the top 10 posts by likes or views from yesterday? It seems the database only stores cumulative values. Thank you.
Hey ![]()
Do you have the Discourse Data Explorer plugin enabled on your site?
It’s not enabled, do I need to enable it for more detailed data to be recorded?
You should enable it, then you’ll be able to make specific queries scoped to the past day (yesterday).
Query for Top 10 Posts by Likes from Yesterday
-- Top 10 Posts by Likes from Yesterday
WITH yesterday_actions AS (
SELECT
post_id,
COUNT(*) AS like_count
FROM post_actions
WHERE
created_at::date = CURRENT_DATE - 1
AND post_action_type_id = 2 -- Like action type
GROUP BY post_id
)
SELECT
p.id AS post_id,
t.id AS topic_id,
t.title AS topic_title,
p.post_number,
u.username AS author,
ya.like_count AS likes_yesterday
FROM yesterday_actions ya
JOIN posts p ON p.id = ya.post_id
JOIN topics t ON t.id = p.topic_id
JOIN users u ON u.id = p.user_id
ORDER BY likes_yesterday DESC
LIMIT 10
Query for Top 10 Posts by Views from Yesterday
-- Top 10 Topics by Views from Yesterday
WITH yesterday_topic_views AS (
SELECT
topic_id,
COUNT(*) AS view_count
FROM topic_views
WHERE viewed_at::date = CURRENT_DATE - 1
GROUP BY topic_id
)
SELECT
t.id AS topic_id,
t.title,
u.username AS creator,
ytv.view_count AS views_yesterday
FROM yesterday_topic_views ytv
JOIN topics t ON t.id = ytv.topic_id
JOIN users u ON u.id = t.user_id
ORDER BY views_yesterday DESC
LIMIT 10
(generated using AI bot!)
1 Like