Measure Cumulative Revenue Concentration
Calculate cumulative_revenue and cumulative_pct with window aggregates ordered from highest revenue downward.
- Window functions
- Aggregation
- Numeric functions
- Sorting
Exercise brief
Understand the request
Portfolio strategy lead A concentration report needs to show how quickly the largest snapshots account for each team’s revenue.
A concentration report needs to show how quickly the largest snapshots account for each team’s revenue. Calculate cumulative_revenue and cumulative_pct with window aggregates ordered from highest revenue downward.
Return
- Return team_name, snapshot_id, revenue, cumulative_revenue, and cumulative_pct.
- Round cumulative_pct to two decimals and order by team_name, revenue DESC, snapshot_id.
Constraints
- Use an explicit ROWS frame for cumulative_revenue.
- Use a full-partition window SUM as the denominator.
- Include snapshot_id as the cumulative-order tie-breaker.
Data you will use
Review the relevant tables before deciding how to join, filter, or aggregate them.
team_metrics
snapshot_idINTEGERteam_nameVARCHAR(40)period_noINTEGERrevenueINTEGERtickets_closedINTEGERquality_scoreINTEGER
Hints, when you need them
Open one clue at a time so you still do the reasoning.
Hint 1
The numerator is a running SUM and the denominator is the whole-team SUM.
Hint 2
Only the running numerator has ORDER BY and a bounded frame.
Hint 3
A stable snapshot tie-breaker makes peer progression repeatable.
Verified SQL answer
Attempt the problem first, then compare structure and reasoning—not just syntax.
Reveal solution and explanation
SELECT team_name, snapshot_id, revenue, SUM(revenue) OVER (PARTITION BY team_name ORDER BY revenue DESC, snapshot_id ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS cumulative_revenue, ROUND(SUM(revenue) OVER (PARTITION BY team_name ORDER BY revenue DESC, snapshot_id ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) * 100.0 / SUM(revenue) OVER (PARTITION BY team_name), 2) AS cumulative_pct FROM team_metrics ORDER BY team_name, revenue DESC, snapshot_id;Why this works
Two windows at different scopes preserve row grain while exposing both cumulative contribution and the partition total.
Success check
Each team ends at 100 percent and tied values advance in a deterministic physical order.
Expected result
Use this output to verify values, aliases, ordering, and row count.
| team_name | snapshot_id | revenue | cumulative_revenue | cumulative_pct |
|---|---|---|---|---|
| alpha | 105 | 150 | 150 | 25.86 |
| alpha | 102 | 120 | 270 | 46.55 |
| alpha | 103 | 120 | 390 | 67.24 |
| alpha | 101 | 100 | 490 | 84.48 |
| alpha | 104 | 90 | 580 | 100 |
| beta | 205 | 130 | 130 | 25.49 |
| beta | 202 | 110 | 240 | 47.06 |
| beta | 203 | 95 | 335 | 65.69 |
| beta | 204 | 95 | 430 | 84.31 |
| beta | 201 | 80 | 510 | 100 |
Previewing 10 of 14 expected rows. Run the query in the editor to inspect the full result.
Learn the concepts behind this answer
Strengthen your understanding with these targeted learning topics:
Continue practicing
Build the next SQL skill
Ranking & NTH Value
Solve deterministic ranking, top-N, distribution, positional-frame, and rolling-window problems.
SQL Subqueries
Practice scalar, derived-table, correlated, EXISTS, NULL-safe anti-subquery, quantified, and row-subquery patterns.
Self Joins & Hierarchical Queries
Query organization charts, trees, and parent-child relationships.
Open the interactive workspace and practice across SQL topics.