budgeteer/postgres/queries/transactions.sql
2022-04-23 13:32:39 +02:00

64 lines
1.8 KiB
SQL

-- name: GetTransaction :one
SELECT * FROM display_transactions
WHERE id = $1;
-- name: CreateTransaction :one
INSERT INTO transactions
(date, memo, amount, account_id, payee_id, category_id, group_id, status)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
RETURNING id;
-- name: UpdateTransaction :exec
UPDATE transactions
SET date = $1,
memo = $2,
amount = $3,
payee_id = $4,
category_id = $5
WHERE id = $6;
-- name: SetTransactionReconciled :exec
UPDATE transactions
SET status = 'Reconciled'
WHERE id = $1;
-- name: DeleteTransaction :exec
DELETE FROM transactions
WHERE id = $1;
-- name: GetAllTransactionsForBudget :many
SELECT t.*
FROM display_transactions AS t
WHERE t.budget_id = $1;
-- name: GetTransactionsForAccount :many
SELECT t.*
FROM display_transactions AS t
WHERE t.account_id = $1;
-- name: DeleteAllTransactions :execrows
DELETE FROM transactions
USING accounts
WHERE accounts.budget_id = @budget_id
AND accounts.id = transactions.account_id;
-- name: GetTransactionsByMonthAndCategory :many
SELECT *
FROM transactions_by_month
WHERE transactions_by_month.budget_id = @budget_id;
-- name: GetProblematicTransactions :many
SELECT transactions.*
FROM display_transactions AS transactions
LEFT JOIN accounts
ON transactions.account_id = accounts.id
LEFT JOIN transactions AS otherGroupTransaction
ON transactions.group_id = otherGroupTransaction.group_id
AND transactions.id != otherGroupTransaction.id
AND transactions.account_id != otherGroupTransaction.account_id
LEFT JOIn accounts AS otherGroupAccount
ON otherGroupTransaction.account_id = otherGroupAccount.id
WHERE transactions.category_id IS NULL
AND accounts.on_budget
AND (otherGroupAccount.id IS NULL OR NOT otherGroupAccount.on_budget)
AND accounts.budget_id = $1;