From 8d9ab6cc77a1f208f4b395821c58db2bfaa0d4c5 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Sat, 14 Dec 2019 09:18:48 -0300 Subject: [PATCH] fix balances view. --- LNbits/data/schema.sql | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/LNbits/data/schema.sql b/LNbits/data/schema.sql index 60eb721..164fff1 100644 --- a/LNbits/data/schema.sql +++ b/LNbits/data/schema.sql @@ -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;