Skip to content

Month-end balance by account type

HardFull bankDates and timeRunning totals, LAG and LEADJoins25 min2 hidden datasets

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 date
  • account_type
  • balance: the cumulative sum of amount for 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.

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