LeetCode Problem 1251.
Table: Prices
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| product_id | int |
| start_date | date |
| end_date | date |
| price | int |
+---------------+---------+
(product_id, start_date, end_date) is the primary key for this table.
Each row of this table indicates the price of the product_id in the period from start_date to end_date.
For each product_id there will be no two overlapping periods. That means there will be no two intersecting periods for the same product_id.
Table: UnitsSold
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| product_id | int |
| purchase_date | date |
| units | int |
+---------------+---------+
There is no primary key for this table, it may contain duplicates.
Each row of this table indicates the date, units and product_id of each product sold.
Write an SQL query to find the average selling price for each product.
average_price should be rounded to 2 decimal places.
The query result format is in the following example:
Prices table:
+------------+------------+------------+--------+
| 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 table:
+------------+---------------+-------+
| 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 |
+------------+---------------+-------+
Result table:
+------------+---------------+
| product_id | average_price |
+------------+---------------+
| 1 | 6.96 |
| 2 | 16.96 |
+------------+---------------+
Average selling price = Total Price of Product / Number of products sold.
Average selling price for product 1 = ((100 * 5) + (15 * 20)) / 115 = 6.96
Average selling price for product 2 = ((200 * 15) + (30 * 30)) / 230 = 16.96
The average selling price of a product is a weighted average:
SUM(price * units) / SUM(units)
so a sale of 100 units weighs more than a sale of 15. Averaging the prices themselves would give the wrong answer.
The price of a sale is the one whose period contains the purchase date. Join each price period to the sales of the same product with purchase_date BETWEEN start_date AND end_date. BETWEEN includes both ends, which matters here: the sale on 2019-03-01 is the first day of product 1's second period. Periods of a product never overlap, so each sale matches exactly one price.
The join is a LEFT JOIN from Prices, so a product that was never sold (product 3 in the test data) still appears. Its sums are NULL, and IFNULL(..., 0) reports 0 instead. Round the result to 2 decimals.
The Pandas version merges the sales with the prices, keeps the rows whose date is inside the period, sums revenue and units per product, and maps the averages onto all products with 0 for those without sales.
SELECT p.product_id,
IFNULL(ROUND(SUM(p.price * u.units) / SUM(u.units), 2), 0) AS average_price
FROM Prices AS p
LEFT JOIN UnitsSold AS u
ON u.product_id = p.product_id
AND u.purchase_date BETWEEN p.start_date AND p.end_date -- the price in effect on that day
GROUP BY p.product_id;
No runs yet.