File tree Expand file tree Collapse file tree 1 file changed +19
-12
lines changed
LeetCode SQL 50 Solution/1164. Product Price at a Given Date Expand file tree Collapse file tree 1 file changed +19
-12
lines changed Original file line number Diff line number Diff line change 8686
8787### ** 2️⃣ Window Function (SQL) Solution**
8888``` sql
89- WITH RankedPrices AS (
90- SELECT
91- product_id,
92- new_price AS price,
93- change_date,
94- RANK() OVER (PARTITION BY product_id ORDER BY change_date DESC ) AS rnk
95- FROM Products
96- WHERE change_date <= ' 2019-08-16'
97- )
98- SELECT p .product_id , COALESCE(rp .price , 10 ) AS price
99- FROM (SELECT DISTINCT product_id FROM Products) p
100- LEFT JOIN RankedPrices rp ON p .product_id = rp .product_id AND rp .rnk = 1 ;
89+ # Write your MySQL query statement below
90+ # Write your MySQL query statement below
91+ WITH
92+ T AS (SELECT DISTINCT product_id FROM Products),
93+ P AS (
94+ SELECT product_id, new_price AS price
95+ FROM Products
96+ WHERE
97+ (product_id, change_date) IN (
98+ SELECT product_id, MAX (change_date) AS change_date
99+ FROM Products
100+ WHERE change_date <= ' 2019-08-16'
101+ GROUP BY 1
102+ )
103+ )
104+ SELECT product_id, IFNULL(price, 10 ) AS price
105+ FROM
106+ T
107+ LEFT JOIN P USING (product_id);
101108```
102109#### ** Explanation:**
1031101 . ** ` RANK() OVER (PARTITION BY product_id ORDER BY change_date DESC) ` **
You can’t perform that action at this time.
0 commit comments