Skip to content

Commit 690fbdd

Browse files
authored
Merge pull request #33 from iamAntimPal/Branch-1
Branch 1
2 parents 5834599 + d5abc63 commit 690fbdd

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
+------------+-------+

LeetCode SQL 50 Solution/180. Consecutive Numbers.sql

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,25 @@ It then filters out rows where the current value is the same as both the previou
109109
110110
Final Output:
111111
The 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);

0 commit comments

Comments
 (0)