Skip to content

Commit

Permalink
Use left join and COALESCE
Browse files Browse the repository at this point in the history
  • Loading branch information
ChaituVR committed Jul 20, 2024
1 parent 310fc37 commit 924ad1e
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/graphql/operations/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ export default async function (parent, args) {
const query = `
SELECT
u.*,
SUM(l.vote_count) as votesCount,
SUM(l.proposal_count) as proposalsCount,
COALESCE(SUM(l.vote_count), 0) as votesCount,
COALESCE(SUM(l.proposal_count), 0) as proposalsCount,
MAX(l.last_vote) as lastVote
FROM users u
INNER JOIN leaderboard l ON l.user = u.id
LEFT JOIN leaderboard l ON l.user = u.id
WHERE 1=1 ${queryStr}
GROUP BY u.id
ORDER BY ${orderBy} ${orderDirection} LIMIT ?, ?
Expand All @@ -56,8 +56,8 @@ export default async function (parent, args) {
`
SELECT
user,
SUM(vote_count) as votesCount,
SUM(proposal_count) as proposalsCount,
COALESCE(SUM(vote_count), 0) as votesCount,
COALESCE(SUM(proposal_count) ,0) as proposalsCount,
MAX(last_vote) as lastVote
FROM leaderboard
WHERE user IN (?)
Expand Down

0 comments on commit 924ad1e

Please sign in to comment.