Write a SQL query that computes each product's revenue, its share of total revenue, and the running cumulative share in descending revenue order, the table behind a Pareto chart. It tests aggregating before windowing, deterministic tie-breaking in running totals, zero-total handling, and rounding percentages correctly.
For each product with at least one sale, compute total `revenue` (the sum of `quantity * unit_price`), its share of the grand total `revenue_pct`, and the running cumulative share `cumulative_pct`. Return `product_id`, `product_name`, `revenue`, `revenue_pct` and `cumulative_pct`.
```hint Make the running order unique
Decide what fixes the order of the running total when two products earn the same revenue.
```
### Input Table(s)
Table `products` (primary key `product_id`, all columns NOT NULL):
| Column | Type | Meaning |
|---|---|---|
| product_id | INT | Identifier of the product |
| product_name | VARCHAR(100) | Name of the product |
Table `sales` (primary key `sale_id`, all columns NOT NULL):
| Column | Type | Meaning |
|---|---|---|
| sale_id | INT | Identifier of the sale |
| product_id | INT | Product sold; every value exists in `products` |
| quantity | INT | Units sold; at least 0 |
| unit_price | DECIMAL(10,2) | Price per unit; at least 0 |
### Output Contract
One row per product that has at least one row in `sales`, with these columns in this order:
| Column | Definition |
|---|---|
| product_id | The product's `product_id` |
| product_name | The product's `product_name` |
| revenue | Sum of `quantity * unit_price` over the product's sales, rounded to 2 decimals |
| revenue_pct | 100 times the product's revenue divided by the total revenue of all products, rounded to 2 decimals |
| cumulative_pct | 100 times the running sum of revenue up to and including this row, in output order, divided by the total revenue, rounded to 2 decimals |
Sort by `revenue` descending, then `product_id` ascending. `cumulative_pct` accumulates in this order and is computed from unrounded revenue.
### Example
`products`:
| product_id | product_name |
|---|---|
| 1 | Alpha |
| 2 | Beta |
`sales`:
| sale_id | product_id | quantity | unit_price |
|---|---|---|---|
| 1 | 1 | 5 | 10.00 |
| 2 | 2 | 5 | 10.00 |
Expected output:
| product_id | product_name | revenue | revenue_pct | cumulative_pct |
|---|---|---|---|---|
| 1 | Alpha | 50.00 | 50.00 | 50.00 |
| 2 | Beta | 50.00 | 50.00 | 100.00 |
Both products have revenue 50 and the grand total is 100, so each contributes 50%. The tiebreaker `product_id` ascending puts Alpha (id 1) first with a cumulative 50%, then Beta with a cumulative 100%.
A second input with uneven revenue and a product that never sold:
`products`:
| product_id | product_name |
|---|---|
| 1 | Alpha |
| 2 | Beta |
| 3 | Gamma |
| 4 | Delta |
`sales`:
| sale_id | product_id | quantity | unit_price |
|---|---|---|---|
| 1 | 1 | 3 | 10.00 |
| 2 | 2 | 1 | 50.00 |
| 3 | 1 | 2 | 5.00 |
| 4 | 3 | 1 | 20.00 |
Expected output:
| product_id | product_name | revenue | revenue_pct | cumulative_pct |
|---|---|---|---|---|
| 2 | Beta | 50.00 | 45.45 | 45.45 |
| 1 | Alpha | 40.00 | 36.36 | 81.82 |
| 3 | Gamma | 20.00 | 18.18 | 100.00 |
Alpha earns 30 + 10 = 40, Beta 50 and Gamma 20, for a total of 110. Delta has no sales and is left out. Alpha's cumulative share is 90 / 110 = 81.818..., which rounds to 81.82, even though 45.45 + 36.36 is 81.81.
### Constraints and Clarifications
- There are 1 to 10,000 products and 0 to 100,000 sales. With no sales the result is empty.
- `quantity` and `unit_price` are at least 0, so a product can have sales and a revenue of 0. Such a product is still returned.
- If the grand total revenue is 0, return `0.00` for both `revenue_pct` and `cumulative_pct` on every row.
- A product with several sales gets one row; its sales are summed.
- Ties on revenue are broken by `product_id` ascending, both for the output order and for the running total.
Overview: Write a SQL query that computes each product's revenue, its share of total revenue, and the running cumulative share in descending revenue order, the table behind a Pareto chart. It tests aggregating before windowing, deterministic tie-breaking in running totals, zero-total handling, and rounding percentages correctly.
For each product with at least one sale, compute total revenue (the sum of quantity * unit_price), its share of the grand total revenue_pct, and the running cumulative share cumulative_pct. Return product_id, product_name, revenue, revenue_pct and cumulative_pct.
Input Table(s)
Table products (primary key product_id, all columns NOT NULL):
Column
Type
Meaning
product_id
INT
Identifier of the product
product_name
VARCHAR(100)
Name of the product
Table sales (primary key sale_id, all columns NOT NULL):
Column
Type
Meaning
sale_id
INT
Identifier of the sale
product_id
INT
Product sold; every value exists in products
quantity
INT
Units sold; at least 0
unit_price
DECIMAL(10,2)
Price per unit; at least 0
Output Contract
One row per product that has at least one row in sales, with these columns in this order:
Column
Definition
product_id
The product's product_id
product_name
The product's product_name
revenue
Sum of quantity * unit_price over the product's sales, rounded to 2 decimals
revenue_pct
100 times the product's revenue divided by the total revenue of all products, rounded to 2 decimals
cumulative_pct
100 times the running sum of revenue up to and including this row, in output order, divided by the total revenue, rounded to 2 decimals
Sort by revenue descending, then product_id ascending. cumulative_pct accumulates in this order and is computed from unrounded revenue.
Example
products:
product_id
product_name
1
Alpha
2
Beta
sales:
sale_id
product_id
quantity
unit_price
1
1
5
10.00
2
2
5
10.00
Expected output:
product_id
product_name
revenue
revenue_pct
cumulative_pct
1
Alpha
50.00
50.00
50.00
2
Beta
50.00
50.00
100.00
Both products have revenue 50 and the grand total is 100, so each contributes 50%. The tiebreaker product_id ascending puts Alpha (id 1) first with a cumulative 50%, then Beta with a cumulative 100%.
A second input with uneven revenue and a product that never sold:
products:
product_id
product_name
1
Alpha
2
Beta
3
Gamma
4
Delta
sales:
sale_id
product_id
quantity
unit_price
1
1
3
10.00
2
2
1
50.00
3
1
2
5.00
4
3
1
20.00
Expected output:
product_id
product_name
revenue
revenue_pct
cumulative_pct
2
Beta
50.00
45.45
45.45
1
Alpha
40.00
36.36
81.82
3
Gamma
20.00
18.18
100.00
Alpha earns 30 + 10 = 40, Beta 50 and Gamma 20, for a total of 110. Delta has no sales and is left out. Alpha's cumulative share is 90 / 110 = 81.818..., which rounds to 81.82, even though 45.45 + 36.36 is 81.81.
Constraints and Clarifications
There are 1 to 10,000 products and 0 to 100,000 sales. With no sales the result is empty.
quantity
and
unit_price
are at least 0, so a product can have sales and a revenue of 0. Such a product is still returned.
If the grand total revenue is 0, return
0.00
for both
revenue_pct
and
cumulative_pct
on every row.
A product with several sales gets one row; its sales are summed.
Ties on revenue are broken by
product_id
ascending, both for the output order and for the running total.