fix balances view.

This commit is contained in:
fiatjaf 2019-12-14 09:18:48 -03:00
parent b4426ba021
commit 8d9ab6cc77

View File

@ -21,14 +21,18 @@ CREATE TABLE IF NOT EXISTS apipayments (
memo text
);
CREATE VIEW IF NOT EXISTS balances AS
DROP VIEW IF EXISTS balances;
CREATE VIEW balances AS
SELECT wallet, coalesce(sum(s), 0) AS balance FROM (
SELECT wallet, sum(amount) AS s -- incoming
FROM apipayments
WHERE amount > 0 AND pending = false -- don't sum pending
GROUP BY wallet
UNION ALL
SELECT wallet, sum(amount + fee) AS s -- outgoing, sum fees
FROM apipayments
WHERE amount < 0 -- do sum pending
GROUP BY wallet
)
GROUP BY wallet;