budgeteer/postgres/queries/accounts.sql
Jan Bader 337d588c3c
All checks were successful
continuous-integration/drone/push Build is passing
Include all accounts
Before this change, accounts without transactions in the specified timespan had not been included
2022-01-10 10:09:34 +00:00

22 lines
634 B
SQL

-- name: CreateAccount :one
INSERT INTO accounts
(name, budget_id)
VALUES ($1, $2)
RETURNING *;
-- name: GetAccount :one
SELECT accounts.* FROM accounts
WHERE accounts.id = $1;
-- name: GetAccounts :many
SELECT accounts.* FROM accounts
WHERE accounts.budget_id = $1
ORDER BY accounts.name;
-- name: GetAccountsWithBalance :many
SELECT accounts.id, accounts.name, accounts.on_budget, SUM(transactions.amount)::decimal(12,2) as balance
FROM accounts
LEFT JOIN transactions ON transactions.account_id = accounts.id AND transactions.date < NOW()
WHERE accounts.budget_id = $1
GROUP BY accounts.id, accounts.name
ORDER BY accounts.name;