Average Selling Price - Problem
You're tasked with calculating the average selling price for each product in a company's inventory system. The challenge is that product prices change over time, and you need to match each sale with the correct price based on when it occurred.
You have two tables:
- Prices: Contains price information for products during specific date ranges
- UnitsSold: Contains sales data with purchase dates and quantities
Goal: Calculate the weighted average selling price for each product, where the weight is the number of units sold at each price point.
Key Rules:
- Round results to 2 decimal places
- If a product has no sales, its average price is 0
- Price periods never overlap for the same product
Input & Output
example_1.sql โ Basic Case
$
Input:
Prices:
| product_id | start_date | end_date | price |
|------------|------------|------------|-------|
| 1 | 2019-02-17 | 2019-02-28 | 5 |
| 1 | 2019-03-01 | 2019-03-22 | 20 |
| 2 | 2019-02-01 | 2019-02-20 | 15 |
| 2 | 2019-02-21 | 2019-03-31 | 30 |
UnitsSold:
| product_id | purchase_date | units |
|------------|---------------|-------|
| 1 | 2019-02-25 | 100 |
| 1 | 2019-03-01 | 15 |
| 2 | 2019-02-10 | 200 |
| 2 | 2019-03-22 | 30 |
โบ
Output:
| product_id | average_price |
|------------|---------------|
| 1 | 6.67 |
| 2 | 16.96 |
๐ก Note:
Product 1: (100 units ร $5) + (15 units ร $20) = $800 total revenue, 115 total units โ $800/115 = $6.96 โ $6.67. Product 2: (200 units ร $15) + (30 units ร $30) = $3900 total revenue, 230 total units โ $3900/230 = $16.96.
example_2.sql โ Product with No Sales
$
Input:
Prices:
| product_id | start_date | end_date | price |
|------------|------------|------------|-------|
| 1 | 2019-02-17 | 2019-02-28 | 5 |
| 2 | 2019-02-01 | 2019-02-20 | 15 |
UnitsSold:
| product_id | purchase_date | units |
|------------|---------------|-------|
| 1 | 2019-02-25 | 100 |
โบ
Output:
| product_id | average_price |
|------------|---------------|
| 1 | 5.00 |
| 2 | 0.00 |
๐ก Note:
Product 1 has sales: 100 units ร $5 = $5.00 average. Product 2 has no sales, so average price is $0.00 by default.
example_3.sql โ Single Product Multiple Periods
$
Input:
Prices:
| product_id | start_date | end_date | price |
|------------|------------|------------|-------|
| 1 | 2019-01-01 | 2019-01-15 | 10 |
| 1 | 2019-01-16 | 2019-01-31 | 20 |
| 1 | 2019-02-01 | 2019-02-28 | 30 |
UnitsSold:
| product_id | purchase_date | units |
|------------|---------------|-------|
| 1 | 2019-01-10 | 50 |
| 1 | 2019-01-20 | 30 |
| 1 | 2019-02-15 | 20 |
โบ
Output:
| product_id | average_price |
|------------|---------------|
| 1 | 18.00 |
๐ก Note:
Product 1 sales across three periods: (50ร$10) + (30ร$20) + (20ร$30) = $1800 total revenue, 100 total units โ $1800/100 = $18.00 average price.
Visualization
Tap to expand
Understanding the Visualization
1
Identify Price Periods
Each product has different prices during different date ranges
2
Match Sales to Prices
For each sale, find which price period it falls into based on purchase date
3
Calculate Weighted Revenue
Multiply units sold by the corresponding price for each sale
4
Compute Average
Divide total revenue by total units sold to get weighted average price
Key Takeaway
๐ฏ Key Insight: The average selling price is a weighted average where the weights are the number of units sold at each price point, not the duration of price periods. This reflects the actual revenue per unit across all sales.
Time & Space Complexity
Time Complexity
O(n log n)
Database JOIN operation with proper indexing on product_id and dates
โก Linearithmic
Space Complexity
O(n)
Temporary space for JOIN operations and result set
โก Linearithmic Space
Constraints
- 1 โค Prices.product_id, UnitsSold.product_id โค 1000
- 0 โค Prices.price โค 1000
- 1 โค UnitsSold.units โค 1000
- No overlapping periods for the same product_id in Prices table
- All dates are in format 'YYYY-MM-DD'
- Round results to exactly 2 decimal places
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code