Skip to content

Spend mix by customer segment

HardFull bankGroup and aggregateRatios and percentagesJoins22 min2 hidden datasets

The task

The product team wants to see how each customer segment splits its everyday spending. Look at transactions with posted_at on or after 2025-04-01 00:00:00 and before 2025-07-01 00:00:00 in the categories rent, groceries, dining and travel. Ignore every other category.

A transaction belongs to the segment of the customer who owns the account. Spend is the amount turned positive (a transaction of -40.00 is 40.00 of spend).

Return segment, category, spend (the total spend of that segment in that category) and share_pct: the category's spend as a percentage of the segment's total spend over the four categories, rounded to 1 decimal place. Shares inside a segment add up to about 100.

Sort by segment ascending, then spend descending, then category ascending.

Expected output

segmentcategoryspendshare_pct
  • Sorted by segment ascending, then spend descending, then category ascending.
  • share_pct rounded to 1 decimal place.
  • 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