treasuredb/schema/views/03_History.sql
2017-01-29 16:08:28 +01:00

34 lines
1.1 KiB
SQL

-- Log of internal transfers
DROP VIEW IF EXISTS History;
CREATE VIEW History AS
-- internal transfers with account as source
SELECT DATE(timestamp) AS date,
d.purpose AS purpose,
d.category AS category,
d.debtor AS account,
t.credId AS credId,
t.amount AS debit,
NULL AS credit,
d.targetCredit AS contra,
d.billId AS billId,
t.note AS note
FROM Transfer t
LEFT JOIN Debit AS d ON d.billId = t.billId
-- internal transfers with account as target
UNION
SELECT DATE(timestamp) AS date,
d.purpose AS purpose,
d.category AS category,
c.account AS account,
d.targetCredit AS credId,
NULL AS debit,
t.amount AS credit,
t.credId AS contra,
d.billId AS billId,
t.note AS note
FROM Transfer t
LEFT JOIN Debit AS d ON d.billId = t.billId
LEFT JOIN Credit AS c ON c.credId = d.targetCredit
ORDER BY date ASC
;