Voici une requête de badge pour quelqu’un qui répond à un message dans une catégorie (id=19). La condition post_number > 1 garantit qu’il s’agit d’une réponse et non du premier message.
SELECT DISTINCT ON (p.user_id)
p.user_id, p.id post_id, p.created_at granted_at
FROM badge_posts p
JOIN topics t ON t.id = p.topic_id
WHERE category_id = 19 AND p.post_number > 1
AND (:backfill OR p.id IN (:post_ids) )