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
Price Period 1Feb 17-28: $5Price Period 2Mar 1-22: $20Sale 1Feb 25100 unitsSale 2Mar 115 unitsWeighted Average CalculationRevenue: (100 ร— $5) + (15 ร— $20) = $800Total Units: 100 + 15 = 115Average Price: $800 รท 115 = $6.96๐ŸŽฏ Key: Weight prices by sales volume, not time periods!
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

n
2n
โšก Linearithmic
Space Complexity
O(n)

Temporary space for JOIN operations and result set

n
2n
โšก 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
Asked in
Amazon 45 Meta 32 Google 28 Microsoft 22
52.4K Views
High Frequency
~15 min Avg. Time
1.9K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen