Skip to content

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.

intUnique id.
textName.
dateDay they became a customer.
textretail, premium or business.
accounts126 rows in the sample

One row per account. A customer can have several. Key: account_id.

intUnique id.
intOwner.
textchecking, savings or credit.
dateOpening day.
dateClosing 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.

bigintUnique id.
intThe account.
timestampWhen it posted (UTC).
numeric(12,2)Signed amount, USD.
textsalary, rent, groceries, dining, travel, transfer, fees or refund.
textMerchant name. NULL for salary, transfers and fees.

Related questions

Loading the editor and PostgreSQL