Revenue by customer quintile
WITH revenue AS (
SELECT customer_id, SUM(total_usd) AS spent, COUNT(*) AS orders
FROM orders GROUP BY customer_id
),
ranked AS (
SELECT spent, orders, NTILE(5) OVER (ORDER BY spent DESC) AS quintile
FROM revenue
)
SELECT quintile,
COUNT(*) AS customers,
ROUND(SUM(spent)) AS revenue,
ROUND(100.0 * SUM(spent) / (SELECT SUM(spent) FROM revenue), 1) AS revenue_share_pct,
ROUND(AVG(orders), 2) AS orders_per_customer
FROM ranked
GROUP BY quintile
ORDER BY quintile;View data table
| quintile | customers | revenue | revenue_share_pct | orders_per_customer |
|---|---|---|---|---|
| 1 | 1567 | 1.484701e+06 | 69.2 | 5.17 |
| 2 | 1567 | 355985 | 16.6 | 2.12 |
| 3 | 1567 | 170156 | 7.9 | 1.54 |
| 4 | 1566 | 92522 | 4.3 | 1.16 |
| 5 | 1566 | 42706 | 2 | 1.02 |
holds on the current window