diff --git a/README.md b/README.md index 36f7dad..959d487 100644 --- a/README.md +++ b/README.md @@ -88,6 +88,7 @@ Have a good contributing! - [1045. Customers Who Bought All Products](./leetcode/medium/1045.%20Customers%20Who%20Bought%20All%20Products.sql) - [1070. Product Sales Analysis III](./leetcode/medium/1070.%20Product%20Sales%20Analysis%203.sql) - [1158. Market Analysis 1](./leetcode/medium/1158.%20Market%20Analysis%201.sql) + - [1164. Product Price at a Given Date](./leetcode/medium/1164.%20Product%20Price%20at%20a%20Given%20Date.sql) - [1174. Immediate Food Delivery II](./leetcode/medium/1174.%20Immediate%20Food%20Delivery%20II.sql) - [1193. Monthly Transactions I](./leetcode/medium/1193.%20Monthly%20Transactions%20I.sql) - [1204. Last Person to Fit in the Bus](./leetcode/medium/1204.%20Last%20Person%20to%20Fit%20in%20the%20Bus.sql) diff --git a/leetcode/medium/1164. Product Price at a Given Date.sql b/leetcode/medium/1164. Product Price at a Given Date.sql new file mode 100644 index 0000000..dd9d4ba --- /dev/null +++ b/leetcode/medium/1164. Product Price at a Given Date.sql @@ -0,0 +1,36 @@ +/* +Question 1164. Product Price at a Given Date +Link: https://leetcode.com/problems/product-price-at-a-given-date/description/?envType=study-plan-v2&envId=top-sql-50 + +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. +*/ + +SELECT DISTINCT + p.product_id, + COALESCE( + ( + SELECT p1.new_price + FROM Products AS p1 + WHERE + p1.change_date <= '2019-08-16' + AND p1.product_id = p.product_id + ORDER BY p1.change_date DESC + LIMIT 1 + ), 10 + ) AS price +FROM Products AS p