Skip to content
Discussion options

You must be logged in to vote

Apologies for the late reply, here you go:

console.log(
  await db.user
    .select("name", {
      balance: (q) => q.accounts.sum("balance"),
    })
    .where({ balance: { gt: 0 } }),
);
// -> [ { name: 'Alice', balance: '30' } ]

SQL:

SELECT "user"."name", "balance".r "balance"
FROM "user"
LEFT JOIN LATERAL (
  SELECT sum("accounts"."balance") r
  FROM "account" AS "accounts"
  WHERE "accounts"."user_id" = "user"."id"
) "balance" ON true
WHERE "balance".r > 0

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@IlyaSemenov
Comment options

Answer selected by IlyaSemenov
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants