Bucket Customers Into Spend Deciles Using Trailing Twelve-Month Spend
Last updated: November 29, 2026
Quick Overview
Compute each active customer's spend over the trailing twelve months, assign spend deciles with NTILE, and report the customer count, spend range and share of total spend for each decile.
Amazon
November 29, 20260
0
0 solved
Compute each active customer's spend over the trailing twelve months, assign spend deciles with NTILE, and report the customer count, spend range and share of total spend for each decile.
A short segmentation query in the style of Amazon retail marketing analytics, used to size high-value customer programs. The interviewer checks NTILE usage, the trailing window filter, and a share-of-total calculation with a window over aggregated rows.
What the Interviewer Expects
- Filter to the trailing twelve months relative to a fixed as-of date
- Aggregate to one row per customer before NTILE
- Use NTILE(10) ordered by spend descending so decile 1 is the top
- Compute share of total spend with a window SUM over the decile rows
- Mention how NTILE splits ties across buckets
Key Topics to Cover
How to Approach This
- Clarify the schema and expected output format before writing queries.
- Use CTEs (WITH clauses) to break complex queries into readable steps.
- Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
- Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
- For pandas, prefer vectorized operations over row-by-row iteration.
Possible Follow-up Questions
- How would you make deciles stable when many customers have identical spend?
- How would you track how customers move between deciles year over year?
- Why might you prefer PERCENT_RANK or fixed spend thresholds?
- How would you include customers with zero spend?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice SQL ProblemsSample Answer
Clarify the Data
Table orders(order_id, customer_id, order_ts, order_total, status). As-of date: November 1, 2026, so the window is November 1, 2025 to October 31, 2026. Only customers with at least one shipped orde...
Approach
- Sum shipped spend per customer in the window.
NTILE(10) OVER (ORDER BY spend DESC)gives decile 1 for the top 10 percent.- Aggregate by decile: count, min, max and total spend.
- `SUM(total...