How to get balance amount based on credit CRD
and debit DEB
for each customer cust
from following txn_tbl
table
SQL> SELECT * FROM txn_tbl;
CUS AMT TXN_CDE
--- ---------- ----------
A 500 CRD
B 400 CRD
A 350 CRD
C 235 DEB
C 800 CRD
B 132 DEB
D 673 CRD
This is the query that i had tried
SELECT cust, LAG(amt,1,0) OVER (PARTITION BY cust ORDER BY cust) "Bal"
FROM
(SELECT cust, SUM(amt)amt
FROM txn_tbl
GROUP BY cust, txn_cde
ORDER BY 2);