From 85ee08b9a621e60df60b2c009a1c9f69059209b7 Mon Sep 17 00:00:00 2001 From: Antim Pal <134076504+iamAntimPal@users.noreply.github.com> Date: Sat, 22 Mar 2025 02:03:09 +0530 Subject: [PATCH 1/2] 1164. Product Price at a Given Date Co-Authored-By: Antim-IWP <203163676+Antim-IWP@users.noreply.github.com> --- .../1164. Product Price at a Given Date.sql | 46 +++++++++++++++++++ .../180. Consecutive Numbers.sql | 2 +- 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 LeetCode SQL 50 Solution/1164. Product Price at a Given Date.sql diff --git a/LeetCode SQL 50 Solution/1164. Product Price at a Given Date.sql b/LeetCode SQL 50 Solution/1164. Product Price at a Given Date.sql new file mode 100644 index 0000000..6fa8bd6 --- /dev/null +++ b/LeetCode SQL 50 Solution/1164. Product Price at a Given Date.sql @@ -0,0 +1,46 @@ + +1164. Product Price at a Given Date + +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. + + +Write a solution to find the prices of all products on 2019-08-16. Assume the price of all products before any change is 10. + +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 | ++------------+-------+ \ No newline at end of file diff --git a/LeetCode SQL 50 Solution/180. Consecutive Numbers.sql b/LeetCode SQL 50 Solution/180. Consecutive Numbers.sql index 04a0b6c..2ef1d2c 100644 --- a/LeetCode SQL 50 Solution/180. Consecutive Numbers.sql +++ b/LeetCode SQL 50 Solution/180. Consecutive Numbers.sql @@ -109,4 +109,4 @@ It then filters out rows where the current value is the same as both the previou Final Output: The final result is a list of distinct numbers that appear consecutively in the Logs table. -""" \ No newline at end of file +""" From d5abc631b27eb46c1548a16e8253b413bf860b1e Mon Sep 17 00:00:00 2001 From: Antim Pal <134076504+iamAntimPal@users.noreply.github.com> Date: Sat, 22 Mar 2025 02:03:30 +0530 Subject: [PATCH 2/2] Update 180. Consecutive Numbers.sql Co-Authored-By: Antim-IWP <203163676+Antim-IWP@users.noreply.github.com> --- .../180. Consecutive Numbers.sql | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/LeetCode SQL 50 Solution/180. Consecutive Numbers.sql b/LeetCode SQL 50 Solution/180. Consecutive Numbers.sql index 2ef1d2c..3a3943a 100644 --- a/LeetCode SQL 50 Solution/180. Consecutive Numbers.sql +++ b/LeetCode SQL 50 Solution/180. Consecutive Numbers.sql @@ -110,3 +110,24 @@ It then filters out rows where the current value is the same as both the previou Final Output: The final result is a list of distinct numbers that appear consecutively in the Logs table. """ + + +# Write your MySQL query statement below +# Write your MySQL query statement below +WITH + T AS (SELECT DISTINCT product_id FROM Products), + P AS ( + SELECT product_id, new_price AS price + FROM Products + WHERE + (product_id, change_date) IN ( + SELECT product_id, MAX(change_date) AS change_date + FROM Products + WHERE change_date <= '2019-08-16' + GROUP BY 1 + ) + ) +SELECT product_id, IFNULL(price, 10) AS price +FROM + T + LEFT JOIN P USING (product_id);