Running balance for one bank account
The task
A customer disputes their January statement for account 20. Rebuild it with a running balance.
List every transaction on account 20 posted before 2025-02-01. The balance starts at 0, so the running balance after a transaction is the sum of amount for all transactions on that account up to and including it. Transactions are ordered by posted_at. If two transactions share the same posted_at, the one with the lower txn_id comes first.
Return:
txn_idposted_atamountbalance: the running balance after this transaction
Sort by posted_at, then txn_id.
Expected output
txn_idposted_atamountbalance- Sorted by posted_at ascending, then txn_id ascending.
- Checked on the sample and on 2 hidden datasets. How grading works
Tables Digital bank · click a name to insert it
customers70 rows in the sample
One row per customer. Key: customer_id.
| int | Unique id. | |
| text | Name. | |
| date | Day they became a customer. | |
| text | retail, premium or business. |
accounts126 rows in the sample
One row per account. A customer can have several. Key: account_id.
| int | Unique id. | |
| int | Owner. | |
| text | checking, savings or credit. | |
| date | Opening day. | |
| date | Closing day. NULL while open. |
transactions9,906 rows in the sample
One row per posted transaction. Positive amounts are money in, negative are money out. Key: txn_id.
| bigint | Unique id. | |
| int | The account. | |
| timestamp | When it posted (UTC). | |
| numeric(12,2) | Signed amount, USD. | |
| text | salary, rent, groceries, dining, travel, transfer, fees or refund. | |
| text | Merchant name. NULL for salary, transfers and fees. |
Related questions
Loading the editor and PostgreSQL
Loading the editor and PostgreSQL