Product Price at a Given Date - Problem
Product Price at a Given Date
You are working as a data analyst for an e-commerce platform that tracks product price changes over time. The company maintains a
Table Structure:
•
•
•
Note: The combination of
Important Business Rule: All products start with an initial price of $10 before any price changes are recorded.
Your Task:
Find the price of all products on the specific date
Output: Return a result table with
You are working as a data analyst for an e-commerce platform that tracks product price changes over time. The company maintains a
Products table that records every price change for every product.Table Structure:
Products table:•
product_id (int): Unique identifier for each product•
new_price (int): The updated price of the product•
change_date (date): The date when the price was changedNote: The combination of
(product_id, change_date) is unique, meaning each product can only have one price change per day.Important Business Rule: All products start with an initial price of $10 before any price changes are recorded.
Your Task:
Find the price of all products on the specific date
2019-08-16. For products that had no price changes by this date, return the default price of $10. For products that had price changes, return the most recent price as of this date.Output: Return a result table with
product_id and price columns in any order. Input & Output
example_1.sql — Basic Price Changes
$
Input:
Products table:
+------------+-----------+-------------+
| product_id | new_price | change_date |
+------------+-----------+-------------+
| 1 | 20 | 2019-08-14 |
| 2 | 50 | 2019-08-11 |
| 1 | 30 | 2019-08-15 |
| 1 | 35 | 2019-08-16 |
| 3 | 20 | 2019-08-17 |
+------------+-----------+-------------+
›
Output:
+------------+-------+
| product_id | price |
+------------+-------+
| 1 | 35 |
| 2 | 50 |
| 3 | 10 |
+------------+-------+
💡 Note:
Product 1: Latest price change on or before 2019-08-16 is 35 (on 2019-08-16). Product 2: Latest price change on or before 2019-08-16 is 50 (on 2019-08-11). Product 3: No price changes on or before 2019-08-16, so default price 10 is used.
example_2.sql — All Default Prices
$
Input:
Products table:
+------------+-----------+-------------+
| product_id | new_price | change_date |
+------------+-----------+-------------+
| 1 | 25 | 2019-08-17 |
| 2 | 40 | 2019-08-18 |
| 3 | 60 | 2019-08-19 |
+------------+-----------+-------------+
›
Output:
+------------+-------+
| product_id | price |
+------------+-------+
| 1 | 10 |
| 2 | 10 |
| 3 | 10 |
+------------+-------+
💡 Note:
All price changes occurred after 2019-08-16, so all products should have the default price of 10 on the target date.
example_3.sql — Mixed Scenarios
$
Input:
Products table:
+------------+-----------+-------------+
| product_id | new_price | change_date |
+------------+-----------+-------------+
| 1 | 100 | 2019-08-10 |
| 1 | 120 | 2019-08-16 |
| 2 | 80 | 2019-08-20 |
| 3 | 90 | 2019-08-15 |
+------------+-----------+-------------+
›
Output:
+------------+-------+
| product_id | price |
+------------+-------+
| 1 | 120 |
| 2 | 10 |
| 3 | 90 |
+------------+-------+
💡 Note:
Product 1: Has two changes before/on target date, latest is 120. Product 2: Only change is after target date, so default 10. Product 3: Has one change before target date, so price is 90.
Constraints
- 1 ≤ Products table rows ≤ 104
- 1 ≤ product_id ≤ 5000
- 0 ≤ new_price ≤ 106
- change_date is between 2019-01-01 and 2019-12-31
- (product_id, change_date) is unique - no duplicate price changes on same date
Visualization
Tap to expand
Understanding the Visualization
1
Timeline Setup
Create a timeline for each product showing all price changes
2
Target Date Line
Draw a vertical line at 2019-08-16
3
Find Latest Price
For each product, find the most recent price change before or on the target date
4
Apply Defaults
Products with no changes before the target date get the default price of $10
Key Takeaway
🎯 Key Insight: Use window functions to rank price changes by date for each product, then select the most recent valid price. Products without changes before the target date automatically get the default price of $10.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code