Products that make up 80 percent of revenue
The task
Merchandising wants a Pareto list: the smallest set of best-selling products that together reach at least 80% of completed-order revenue.
Product revenue is the sum of quantity * unit_price over order_items that belong to orders with status = 'completed'. Only products with at least one completed sale are considered. Rank products by revenue, highest first; if two products have the same revenue, the lower product_id comes first. Let the cumulative share of a product be the revenue of that product and every product ranked above it, divided by the total revenue of all products, times 100.
Return every product whose cumulative share before it (the share of the products ranked above it, 0 for the first) is below 80. That is every product up to and including the one that first reaches or passes 80%.
product_idproduct_namerevenuecumulative_share_pct: the cumulative share including this product, rounded to 1 decimal place
Sort by revenue descending, then product_id.
Expected output
product_idproduct_namerevenuecumulative_share_pct- Sorted by revenue descending, then product_id ascending.
cumulative_share_pctrounded to 1 decimal place.- Checked on the sample and on 2 hidden datasets. How grading works
Tables Online store · click a name to insert it
customers150 rows in the sample
One row per customer account. Key: customer_id.
| int | Unique id. | |
| text | Name on the account. | |
| text | NULL when the customer never gave one (marketplace buyers). | |
| text | Two-letter country code. | |
| date | Day the account was created. | |
| text | organic, paid_search, social, referral or email. NULL when unknown. |
products36 rows in the sample
The catalog. Key: product_id.
| int | Unique id. | |
| text | Display name. | |
| text | Kitchen, Home, Outdoors, Books, Beauty or Toys. | |
| numeric(10,2) | Current list price in USD. | |
| date | First day on sale. |
orders700 rows in the sample
One row per order. Key: order_id.
| int | Unique id. | |
| int | Who placed it. | |
| timestamp | When it was placed (UTC). | |
| text | completed, cancelled or refunded. | |
| text | web, app or marketplace. | |
| numeric(10,2) | Shipping charged, USD. | |
| text | Coupon used, NULL if none. |
order_items1,266 rows in the sample
One row per product in an order. Key: order_id, product_id.
| int | The order. | |
| int | The product. | |
| int | Units bought. | |
| numeric(10,2) | Price paid per unit, after discounts, USD. |
returns79 rows in the sample
Items sent back. Key: return_id.
| int | Unique id. | |
| int | The original order. | |
| int | The product returned. | |
| date | Day the return arrived. | |
| int | Units returned. | |
| text | damaged, wrong_item, not_as_described or changed_mind. NULL when not given. |
Related questions
Loading the editor and PostgreSQL
Loading the editor and PostgreSQL