I am trying to find a way to calculate the running balance of an account.
Below is my schema and the query I tried.
CREATE TABLE transactions(
id INTEGER NOT NULL PRIMARY KEY
,date DATE NOT NULL
,dr INTEGER NOT NULL
,cr INTEGER NOT NULL
,amt INTEGER NOT NULL
);
CREATE TABLE accounts(
id INTEGER NOT NULL PRIMARY KEY
,name VARCHAR(55) DEFAULT NULL
);
INSERT INTO accounts(id,name) VALUES (1,'C1');
INSERT INTO accounts(id,name) VALUES (2,'C2');
INSERT INTO accounts(id,name) VALUES (3,'C3');
INSERT INTO transactions(id,date,dr,cr,amt) VALUES (1,'2020-01-01',1,2,100);
INSERT INTO transactions(id,date,dr,cr,amt) VALUES (2,'2020-01-01',1,2,200);
INSERT INTO transactions(id,date,dr,cr,amt) VALUES (3,'2020-01-02',1,3,100);
INSERT INTO transactions(id,date,dr,cr,amt) VALUES (4,'2020-01-03',3,2,100);
SELECT date, dr as id, balance FROM (
SELECT date, dr, SUM(amt) OVER (PARTITION BY dr ORDER BY date ) as balance FROM transactions
GROUP BY date, dr, amt
UNION ALL
SELECT date, cr, -SUM(amt) OVER (PARTITION BY cr ORDER BY date ) as balance FROM transactions
GROUP BY date, cr, amt
) as balances
GROUP BY date, dr, balance
ORDER BY date, dr, balance
Output
date | id | balance |
---|---|---|
2020-01-01 | 1 | 300 |
2020-01-01 | 2 | -300 |
2020-01-02 | 1 | 400 |
2020-01-02 | 3 | -100 |
2020-01-03 | 2 | -400 |
2020-01-03 | 3 | 100 |
Trying to arrive at the output as shown below, taking into consideration of debit and credit accounts. But as you can see, the balance of C3
on 2020-01-03
is 100
instead of 0
. What am I doing wrong?
id | name | date | balance |
---|---|---|---|
1 | C1 | 2020-01-01 | 300 |
2 | C2 | 2020-01-01 | -300 |
1 | C1 | 2020-01-02 | 400 |
3 | C3 | 2020-01-02 | -100 |
2 | C2 | 2020-01-03 | -400 |
3 | C3 | 2020-01-03 | 000 |
2
Answers
finally found the solution, though very convoluted! Any other optimized solutions ?
View on DB Fiddle
Union the dr’s and crs