From d79dcb5a5c3f5c4c0f287f00c9b6d9c727fdf24a Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Fri, 2 Jan 2015 19:35:39 +0100 Subject: [PATCH] SQL transaction_cashsums: Update view to differentiate cash_credit, cash_debit This allows looking at credit changes as well. --- brmbar3/SQL | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/brmbar3/SQL b/brmbar3/SQL index 8623ed5..32a3cb1 100644 --- a/brmbar3/SQL +++ b/brmbar3/SQL @@ -108,10 +108,18 @@ CREATE VIEW transaction_nicesplits AS -- 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 + SELECT t.id AS id, t.time AS time, SUM(credit_cash) AS cash_credit, SUM(debit_cash) AS cash_debit, 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') + LEFT JOIN (SELECT cts.amount AS credit_cash, cts.transaction AS cts_t + FROM transaction_nicesplits AS cts + LEFT JOIN accounts AS a ON a.id = cts.account OR a.id = cts.account + WHERE a.currency = (SELECT currency FROM accounts WHERE name = 'BrmBar Cash') + AND a.acctype IN ('cash', 'debt') + AND cts.amount < 0) credit ON cts_t = t.id + LEFT JOIN (SELECT dts.amount AS debit_cash, dts.transaction AS dts_t + FROM transaction_nicesplits AS dts + LEFT JOIN accounts AS a ON a.id = dts.account OR a.id = dts.account + WHERE a.currency = (SELECT currency FROM accounts WHERE name = 'BrmBar Cash') + AND a.acctype IN ('cash', 'debt') + AND dts.amount > 0) debit ON dts_t = t.id GROUP BY t.id ORDER BY t.id;