Daily end-of-day balance from transactions with a date spine
The task
The bank wants a daily balance table for the savings accounts of its premium customers. There is no balance column, so build it from transactions: the end-of-day balance of an account on a day is the sum of all its amount values posted on or before that day (any time of that day, and including everything before the report window).
For each savings account of a premium customer, return one row for every day from 2025-04-26 to 2025-05-02 inclusive, starting from the account's opened_on day. A day before the account was opened has no row. An account with no transactions yet has a balance of 0.
Return account_id, day and balance. Sort by account_id, then day.
Expected output
account_iddaybalance- Sorted by account_id ascending, then day 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
- Campaign by day fact table with cumulative spendhard
- One row per savings account per month, with zero fillmedium
- Salary validity ranges built with LEADmedium
- Active accounts and seats per day from subscription datesmedium
- Agent by month fact table of tickets created and resolvedmedium
- Daily stock levels with a date spine and carry forwardhard
Loading the editor and PostgreSQL
Loading the editor and PostgreSQL