wallet_txns(txn_id, account_id, txn_type, amount, created_at)
For each account return its net balance: deposits add amount, withdrawals
subtract it, and every other txn_type is ignored entirely.
txn_type contains values other than 'deposit' and 'withdrawal'. A pending
or reversed row must contribute nothing rather than being treated as either.
Tables
wallet_txns
txn_id account_id txn_type amount created_at
------ ---------- ---------- ------ -------------------
1 10 deposit 100.00 2026-05-01 10:00:00
2 10 withdrawal 30.00 2026-05-02 10:00:00
3 10 reversed 500.00 2026-05-03 10:00:00
4 11 deposit 50.00 2026-05-01 10:00:00
5 11 withdrawal 80.00 2026-05-04 10:00:00
6 12 pending 999.00 2026-05-05 10:00:00
Expected result
account_id balance
---------- -------
10 70.00
11 -30.00
12 0.00
Sign in to write and run your own code.
SELECT query