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 | 1867 | 1.27212e+06 | 69.1 | 5.02 |
| 2 | 1867 | 306198 | 16.6 | 1.85 |
| 3 | 1866 | 143587 | 7.8 | 1.33 |
| 4 | 1866 | 79396 | 4.3 | 1.1 |
| 5 | 1866 | 39606 | 2.2 | 1.01 |
holds on the current window