customers(customer_id) and orders(order_id, customer_id, net_revenue)
Rank customers by lifetime revenue, biggest first, and return the running share of total revenue they account for.
Every customer in customers appears, including those who never ordered, with
revenue 0. Ties are broken by customer_id ascending so the running total is
deterministic.
Return customer_id, revenue and cumulative_revenue_pct rounded to two
decimals, ordered by revenue descending then customer_id.
The denominator is the total across everybody, so it is a window over the whole result rather than a correlated subquery per row.
Tables
customers
customer_id
-----------
1
2
3
4
5
orders
order_id customer_id net_revenue
-------- ----------- -----------
1 1 500.00
2 2 60.00
3 2 40.00
4 3 200.00
5 4 100.00
Expected result
customer_id revenue cumulative_revenue_pct
----------- ------- ----------------------
1 500.00 55.56
3 200.00 77.78
2 100.00 88.89
4 100.00 100.00
5 0.00 100.00