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 | 1879 | 1.285541e+06 | 69.1 | 5.04 |
| 2 | 1879 | 309075 | 16.6 | 1.86 |
| 3 | 1879 | 144672 | 7.8 | 1.33 |
| 4 | 1879 | 79978 | 4.3 | 1.1 |
| 5 | 1879 | 39916 | 2.1 | 1.01 |
holds on the current window