Search for number of solved topics per user

I think you should be able to do it with two if you use group_users. Maybe something like:

-- [params]
-- string :group_name = staff
-- date :date_from = 01/04/2022
-- date :date_to = 01/05/2022


SELECT
gu.group_id,
ua.user_id,
COUNT(1) AS solved_count
FROM user_actions ua
JOIN group_users gu on ua.user_id = gu.user_id
WHERE ua.action_type = 15
AND gu.group_id = (SELECT id FROM groups WHERE name = :group_name)
AND ua.created_at::date BETWEEN :date_from::date AND :date_to::date
GROUP BY gu.group_id, ua.user_id
ORDER BY solved_count DESC

(FYI group_name is case sensitive)


Update: Belatedly realised I added in a third when I made the group look-up a bit more user-friendly than using a straight group_id. But it could be done with two. :slightly_smiling_face:

3 Likes