One row per savings account per month, with zero fill
The task
Build a monthly fact table for savings accounts at the grain one row per account per calendar month, for January to June 2025.
- An account has a row for a month when it existed at some point in that month:
opened_onis before the first day of the next month, andclosed_onisNULLor on or after the first day of that month. - Only accounts with
account_typeequal tosavingsare included. - Count and add up the account's transactions posted in that month (from the first day at 00:00 up to, but not including, the first day of the next month).
- A month with no transactions still gets a row, with 0 transactions and a net amount of 0.
Return account_id, month (the first day of the month), transactions (count) and net_amount (the sum of amount). Sort by account_id, then month.
Expected output
account_idmonthtransactionsnet_amount- Sorted by account_id ascending, then month 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
- Active accounts and seats per day from subscription datesmedium
- Agent by month fact table of tickets created and resolvedmedium
- Daily end-of-day balance from transactions with a date spinehard
- Daily stock levels with a date spine and carry forwardhard
- Monthly recurring revenue by plan with empty monthsmedium
- Campaign by day fact table with cumulative spendhard
Loading the editor and PostgreSQL
Loading the editor and PostgreSQL