NTILE on an aggregated CTE.
Rank customers by total lifetime spend and bucket them into 4 equal-sized quartiles (quartile 1 = highest spenders). Columns: `customer_id`, `total_spend`, `spend_quartile`. Order by total_spend descending, then customer_id. Table: `ci_orders`.
| Column | Type | Key |
|---|---|---|
| order_id | INT | PK |
| customer_id | INT | |
| product_name | TEXT | |
| category | TEXT | |
| order_date | DATE | |
| revenue | NUMERIC |
Each hint you reveal reduces the XP you can earn. Try the query first.
Aggregate per-customer SUM(revenue) in a CTE.
NTILE(4) OVER (ORDER BY total_spend DESC) in the outer SELECT.
With 8 customers, each quartile has exactly 2.