-- Split customers into quartiles by lifetime spend
SELECT user_id,
total_spent,
NTILE(4) OVER (ORDER BY total_spent DESC) AS spend_quartile
FROM user_totals;
-- Each user's bucket → label
SELECT user_id,
CASE NTILE(4) OVER (ORDER BY total_spent DESC)
WHEN 1 THEN 'platinum'
WHEN 2 THEN 'gold'
WHEN 3 THEN 'silver'
WHEN 4 THEN 'bronze'
END AS tier
FROM user_totals;
-- Deciles within each category
SELECT product, price,
NTILE(10) OVER (PARTITION BY category ORDER BY price) AS price_decile
FROM products;
Create a free account and build your private vault. Share publicly whenever you want.