balances view and other minor edits.
This commit is contained in:
+13
-1
@@ -15,8 +15,20 @@ CREATE TABLE IF NOT EXISTS wallets (
|
||||
CREATE TABLE IF NOT EXISTS apipayments (
|
||||
payhash text PRIMARY KEY,
|
||||
amount integer NOT NULL,
|
||||
fee integer NOT NULL,
|
||||
fee integer NOT NULL DEFAULT 0,
|
||||
wallet text NOT NULL,
|
||||
pending boolean NOT NULL,
|
||||
memo text
|
||||
);
|
||||
|
||||
CREATE VIEW IF NOT EXISTS 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
|
||||
UNION ALL
|
||||
SELECT wallet, sum(amount + fee) AS s -- outgoing, sum fees
|
||||
FROM apipayments
|
||||
WHERE amount < 0 -- do sum pending
|
||||
)
|
||||
GROUP BY wallet;
|
||||
|
||||
Reference in New Issue
Block a user