I have Acts by Users who are joined to Groups by Memberships in a PostgreSQL db.
I have a query to generate rows for a leaderboard. However, it currently excludes Users where the Acts table does not include a row with the corresponding users_id
. I want to include all group members, even those with 0 Acts.
The current query:
SELECT acts.users_id, username, avatar_url, COUNT(acts.id)
FROM acts
JOIN users ON acts.users_id = users.id
JOIN memberships on memberships.users_id = users.id
WHERE memberships.groups_id = ' + req.params.group_id + '
AND acts.created_at >= (CURRENT_DATE - 6)
GROUP BY acts.users_id, username, avatar_url
ORDER BY COUNT(acts.id) DESC
I have tried changing JOIN
before users to RIGHT JOIN
and LEFT JOIN
, but I get the same result. At one point, I think RIGHT JOIN
was working, but somehow, I have gone awry.