Open accounts at each month end, by account type
The task
Finance reports the number of open accounts on the last day of every month, split by account type.
An account is open on a day when opened_on is on or before that day, and closed_on is either NULL or on or after that day (an account is still open on its closed_on day and closed from the day after).
The six month ends are 2025-01-31, 2025-02-28, 2025-03-31, 2025-04-30, 2025-05-31 and 2025-06-30. The account types are 'checking', 'savings' and 'credit'.
Return one row for every combination of month end and account type, even when none is open:
month_endaccount_typeopen_accounts
Sort by month_end, then account_type.
Expected output
month_endaccount_typeopen_accounts- 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