1164. Product Price at a Given Date
DifficultyMedium
Description
Table: Products
+---------------+---------+ | Column Name | Type | +---------------+---------+ | product_id | int | | new_price | int | | change_date | date | +---------------+---------+ (product_id, change_date) is the primary key (combination of columns with unique values) of this table. Each row of this table indicates that the price of some product was changed to a new price at some date.
Initially, all products have price 10.
Write a solution to find the prices of all products on the date 2019-08-16.
Return the result table in any order.
The result format is in the following example.
Example 1:
Input: Products table: +------------+-----------+-------------+ | product_id | new_price | change_date | +------------+-----------+-------------+ | 1 | 20 | 2019-08-14 | | 2 | 50 | 2019-08-14 | | 1 | 30 | 2019-08-15 | | 1 | 35 | 2019-08-16 | | 2 | 65 | 2019-08-17 | | 3 | 20 | 2019-08-18 | +------------+-----------+-------------+ Output: +------------+-------+ | product_id | price | +------------+-------+ | 2 | 50 | | 1 | 35 | | 3 | 10 | +------------+-------+
Solutions
Solution 1: Subquery + Join
Thinking
Each product keeps the last price change on or before \(2019\)-\(08\)-\(16\), or \(10\) if none. A subquery takes MAX(change_date) in that window per product and joins back for the price. The distinct product list left-joins that result so missing prices become \(10\).
We can use a subquery to find the price of the last price change for each product before the given date, and record it in the P table. Then, we can find all product_ids in the T table. Finally, we can left join the T table with the P table on product_id to get the final result.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | |
Solution 2
Thinking
Method 1 finds the last change with an aggregate subquery. Method 2 left-joins in-range changes and RANKs them by change_date descending, keeping \(rk=1\). Rows with no change still rank \(1\), and IFNULL fills \(10\).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | |