Attempting to create Monthly Leaderboard query - head exploding

I haven’t run the full query. The first thing I would do is check that all the WITH statements are returning the results you are looking for. You can check them in your final query by running something like SELECT * FROM r.

If the WITH statements are returning the correct results, but rows are being returned in your final query that you expect to be excluded, then the issue is with the joins in the query. If the final result should only include rows that are found in your r table, the problem will be the left join you are using for the r table. A left join will return the complete set of records from Table A, with the matching records (where available) in Table B. If there is no match, the right side will contain null. Try using an inner join instead (JOIN and INNER JOIN are the same thing.)

Here’s a good reference on joins: A Visual Explanation of SQL Joins.