Month-end balance by account type
The task
Finance reports the combined balance of each account type at the end of every month, from January to June 2025. The balance is the sum of every transaction amount posted from the start of the data up to and including the last second of that month's last day. All accounts count, open or closed. Account types with nothing posted yet have a balance of 0.
The month-ends are 2025-01-31, 2025-02-28, 2025-03-31, 2025-04-30, 2025-05-31 and 2025-06-30.
Return one row for every month-end and every account_type that appears in accounts:
month_end: the last day of the month, as a dateaccount_typebalance: the cumulative sum ofamountfor that type up to the end of that day
A month with no transactions for a type still gets a row, equal to the previous month's balance. Sort by month_end, then account_type.
Expected output
month_endaccount_typebalance- Sorted by month_end ascending, then account_type 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