File tree Expand file tree Collapse file tree 2 files changed +68
-1
lines changed
Expand file tree Collapse file tree 2 files changed +68
-1
lines changed Original file line number Diff line number Diff line change 1+
2+ 1164 . Product Price at a Given Date
3+
4+ Table: Products
5+
6+ + -- -------------+---------+
7+ | Column Name | Type |
8+ + -- -------------+---------+
9+ | product_id | int |
10+ | new_price | int |
11+ | change_date | date |
12+ + -- -------------+---------+
13+ (product_id, change_date) is the primary key (combination of columns with unique values ) of this table.
14+ Each row of this table indicates that the price of some product was changed to a new price at some date .
15+
16+
17+ 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 .
18+
19+ Return the result table in any order.
20+
21+ The result format is in the following example.
22+
23+
24+
25+ Example 1 :
26+
27+ Input:
28+ Products table:
29+ + -- ----------+-----------+-------------+
30+ | product_id | new_price | change_date |
31+ + -- ----------+-----------+-------------+
32+ | 1 | 20 | 2019 - 08 - 14 |
33+ | 2 | 50 | 2019 - 08 - 14 |
34+ | 1 | 30 | 2019 - 08 - 15 |
35+ | 1 | 35 | 2019 - 08 - 16 |
36+ | 2 | 65 | 2019 - 08 - 17 |
37+ | 3 | 20 | 2019 - 08 - 18 |
38+ + -- ----------+-----------+-------------+
39+ Output:
40+ + -- ----------+-------+
41+ | product_id | price |
42+ + -- ----------+-------+
43+ | 2 | 50 |
44+ | 1 | 35 |
45+ | 3 | 10 |
46+ + -- ----------+-------+
Original file line number Diff line number Diff line change @@ -109,4 +109,25 @@ It then filters out rows where the current value is the same as both the previou
109109
110110Final Output:
111111The final result is a list of distinct numbers that appear consecutively in the Logs table.
112- " " "
112+ " " "
113+
114+
115+ # Write your MySQL query statement below
116+ # Write your MySQL query statement below
117+ WITH
118+ T AS (SELECT DISTINCT product_id FROM Products),
119+ P AS (
120+ SELECT product_id, new_price AS price
121+ FROM Products
122+ WHERE
123+ (product_id, change_date) IN (
124+ SELECT product_id, MAX (change_date) AS change_date
125+ FROM Products
126+ WHERE change_date <= ' 2019-08-16'
127+ GROUP BY 1
128+ )
129+ )
130+ SELECT product_id, IFNULL(price, 10 ) AS price
131+ FROM
132+ T
133+ LEFT JOIN P USING (product_id);
You can’t perform that action at this time.
0 commit comments