23 lines
614 B
SQL
23 lines
614 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, SUM(transactions.amount)::decimal(12,2) as balance
|
|
FROM accounts
|
|
LEFT JOIN transactions ON transactions.account_id = accounts.id
|
|
WHERE accounts.budget_id = $1
|
|
AND transactions.date < NOW()
|
|
GROUP BY accounts.id, accounts.name
|
|
ORDER BY accounts.name; |