Given a table Products that tracks price changes for products over time, find the price of all products on a specific date (2019-08-16).
The table has the following structure:
product_id: The unique identifier for each productnew_price: The new price after the changechange_date: The date when the price was changed
Key Rules:
- All products initially have a price of 10
- If a product's price was never changed before or on 2019-08-16, return price 10
- If a product had multiple price changes, use the most recent price on or before 2019-08-16
Table Schema
| Column Name | Type | Description |
|---|---|---|
product_id
PK
|
int | Unique identifier for each product |
new_price
|
int | The new price after the change |
change_date
PK
|
date | The date when the price was changed |
Input & Output
| product_id | new_price | change_date |
|---|---|---|
| 1 | 20 | 2019-08-14 |
| 2 | 50 | 2019-08-15 |
| 1 | 30 | 2019-08-17 |
| 3 | 35 | 2019-08-18 |
| product_id | price |
|---|---|
| 1 | 20 |
| 2 | 50 |
| 3 | 10 |
For product 1: Price changed to 20 on 2019-08-14 (before target date), then to 30 on 2019-08-17 (after target date). So we use 20.
For product 2: Price changed to 50 on 2019-08-15 (before target date). So we use 50.
For product 3: Price changed to 35 on 2019-08-18 (after target date). No changes before target date, so we use default price 10.
| product_id | new_price | change_date |
|---|---|---|
| 1 | 25 | 2019-08-16 |
| 2 | 40 | 2019-08-15 |
| 2 | 45 | 2019-08-16 |
| product_id | price |
|---|---|
| 1 | 25 |
| 2 | 45 |
For product 1: Price changed exactly on target date 2019-08-16 to 25.
For product 2: Had two price changes - 40 on 2019-08-15 and 45 on 2019-08-16. We use the most recent change on the target date, which is 45.
| product_id | new_price | change_date |
|---|---|---|
| 5 | 100 | 2019-08-20 |
| product_id | price |
|---|---|
| 5 | 10 |
Product 5 had a price change on 2019-08-20, which is after our target date 2019-08-16. Since there are no price changes on or before the target date, we return the default price of 10.
Constraints
-
1 ≤ product_id ≤ 1000 -
0 ≤ new_price ≤ 1000000 -
change_dateis a valid date between 2000-01-01 and 2050-12-31