Skip to content

One row per savings account per month, with zero fill

MediumFull bankAnalytics engineeringDates and timeJoins18 min2 hidden datasets

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_on is before the first day of the next month, and closed_on is NULL or on or after the first day of that month.
  • Only accounts with account_type equal to savings are 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.

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