Skip to content

Dining spend that restarts each month

MediumFull bankRunning totals, LAG and LEAD12 min2 hidden datasets

The task

A budgeting feature shows how much a customer has spent on dining so far this month, and the figure starts again at the beginning of each month.

For account 20, list every transaction with category = 'dining'. Return:

  • txn_id
  • posted_at
  • amount: the signed amount (negative, since it is money out)
  • month_to_date: the sum of amount for this account's dining transactions in the same calendar month up to and including this one

A calendar month is a year and a month together, so March 2025 and March 2026 are different months. Transactions are ordered by posted_at; if two share the same posted_at, the lower txn_id comes first. Sort the result by posted_at, then txn_id.

Expected output

txn_idposted_atamountmonth_to_date
  • Sorted by posted_at ascending, then txn_id 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