Biggest month-over-month spending drop per category
The task
The cards team wants to know in which month customers cut back the most in each everyday spending category.
Spending in a category and month is the sum of -amount over transactions in that category with a negative amount (money out), by calendar month of posted_at. Look at the categories groceries, dining and travel. A month with no spending in a category counts as 0.
For each category, find the month from February to June 2025 where spending fell furthest below the previous calendar month (January is the previous month of February). The drop is prev_spend - spend. If two months have the same biggest drop, use the earlier one.
Only categories that had at least one month with a drop above 0 are returned. Return one row per such category:
categorymonth: the first day of the month with the biggest dropprev_spend: spending in the previous monthspend: spending in that monthdrop_amount:prev_spend - spend
Sort by category ascending.
Expected output
categorymonthprev_spendspenddrop_amount- Sorted by category ascending.
- Checked on the sample and on 3 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.
| int | Unique id. | |
| text | Name. | |
| date | Day they became a customer. | |
| text | retail, premium or business. |
accounts126 rows in the sample
One row per account. A customer can have several. Key: account_id.
| int | Unique id. | |
| int | Owner. | |
| text | checking, savings or credit. | |
| date | Opening day. | |
| date | Closing 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.
| bigint | Unique id. | |
| int | The account. | |
| timestamp | When it posted (UTC). | |
| numeric(12,2) | Signed amount, USD. | |
| text | salary, rent, groceries, dining, travel, transfer, fees or refund. | |
| text | Merchant name. NULL for salary, transfers and fees. |
Related questions
Loading the editor and PostgreSQL
Loading the editor and PostgreSQL