All problems
Jade Wu's Running Balance
hardSQLWindow FunctionsCumulative
A customer (Jade Wu, id 5) has disputed a transaction — support needs to see her balance after each transaction, in order, assuming she started at 0.
transactions
| id | customer_id | amount | type | txn_date |
|---|---|---|---|---|
| 1 | 1 | 500 | deposit | 2023-07-01 |
| 2 | 1 | -120 | withdrawal | 2023-07-03 |
| 3 | 2 | 1000 | deposit | 2023-07-02 |
| 4 | 2 | -300 | withdrawal | 2023-07-05 |
| 5 | 3 | 250 | deposit | 2023-07-04 |
| 6 | 3 | -600 | withdrawal | 2023-07-06 |
| 7 | 4 | 800 | deposit | 2023-07-07 |
| 8 | 4 | -450 | withdrawal | 2023-07-08 |
| 9 | 5 | 2000 | deposit | 2023-07-09 |
| 10 | 5 | -1500 | withdrawal | 2023-07-10 |
| 11 | 1 | -200 | transfer | 2023-07-11 |
| 12 | 3 | 400 | deposit | 2023-07-12 |
Task: Write a query that returns Jade Wu's transactions only (customer_id 5), one row each, with txn_date, amount and running_balance — her balance immediately after that transaction, starting from 0 before the first one. Rows come back oldest first, and two transactions on the same date are sequenced by id, lower first.
Example output — shape only, on an invented account; money going out is a negative amount and pulls the balance back down.
| txn_date | amount | running_balance |
|---|---|---|
| 2022-03-04 | 900 | 900 |
| 2022-03-11 | -250 | 650 |
| 2022-04-02 | 400 | 1050 |
Sign in to solve this problem
Reading problems is free for everyone — solving them (Run, Submit, and tracking what you've solved) needs an account.
Sign in