From 10d8a177239a5c179da343d20044ff8b7042bb30 Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Sat, 31 Aug 2013 19:11:14 +0200 Subject: [PATCH] SQL: Two new convenience views transaction_nicesplits and transaction_cashsums --- brmbar3/SQL | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/brmbar3/SQL b/brmbar3/SQL index bae5a15..82c22e4 100644 --- a/brmbar3/SQL +++ b/brmbar3/SQL @@ -93,3 +93,22 @@ CREATE VIEW account_balances AS LEFT JOIN accounts ON accounts.id = ts.account GROUP BY ts.account, accounts.name, accounts.acctype ORDER BY crbalance ASC; + +-- Transaction splits in a form that's nicer to query during manual inspection +CREATE VIEW transaction_nicesplits AS + SELECT ts.id AS id, ts.transaction AS transaction, ts.account AS account, + (CASE WHEN ts.side = 'credit' THEN -ts.amount ELSE ts.amount END) AS amount, + a.currency AS currency, ts.memo AS memo + FROM transaction_splits AS ts LEFT JOIN accounts AS a ON a.id = ts.account + ORDER BY ts.id; + +-- List transactions with summary information regarding their cash element +-- (except in case of transfers between cash and debt accounts, which will cancel out). +CREATE VIEW transaction_cashsums AS + SELECT t.id AS id, t.time AS time, SUM(ts.amount) AS cash, t.description AS description + FROM transactions AS t + LEFT JOIN transaction_nicesplits AS ts ON ts.transaction = t.id + LEFT JOIN accounts AS a ON a.id = ts.account + WHERE a.currency = (SELECT currency FROM accounts WHERE name = 'BrmBar Cash') + AND a.acctype IN ('cash', 'debt') + GROUP BY t.id ORDER BY t.id;