Skip to content

Commit 017b5fc

Browse files
committed
Upload 142
1 parent d90fe2f commit 017b5fc

File tree

2 files changed

+145
-0
lines changed

2 files changed

+145
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ The problems and both submitted and site solutions are documented in individual
174174
| 139 | [Second Highest Salary](https://leetcode.com/problems/second-highest-salary/description/) | [Solution](solutions/139_second_highest_salary.md) | LeetCode | Medium | |
175175
| 140 | [Group Sold Products By The Date](https://leetcode.com/problems/group-sold-products-by-the-date/description/) | [Solution](solutions/140_group_sold_products_by_the_date.md) | DataLemur | Easy | `GROUP_CONCAT()` |
176176
| 141 | [List the Products Ordered in a Period](https://leetcode.com/problems/list-the-products-ordered-in-a-period/) | [Solution](solutions/141_list_the_products_ordered_in_a_period.md) | DataLemur | Easy | |
177+
| 142 | [Product Price at a Given Date](https://leetcode.com/problems/product-price-at-a-given-date/description/) | [Solution](solutions/142_product_price_at_a_given_date.md) | LeetCode | Medium | `UNION ALL`, `LEFT JOIN`, Window Function |
177178
<!-- Index End - WARNING: Do not delete or modify this markdown comment. -->
178179

179180
## Author(s)
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
# SQL Everyday \#142
2+
3+
## Product Price at a Given Date
4+
5+
Site: LeetCode\
6+
Difficulty per Site: Medium
7+
8+
## Problem
9+
10+
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`.
11+
12+
Return the result table in *any order*. [[Full Description](https://leetcode.com/problems/product-price-at-a-given-date/description/)]
13+
14+
## Submitted Solution
15+
16+
```sql
17+
-- Submitted Solution
18+
# Write your MySQL query statement below
19+
20+
WITH cte1 AS (
21+
SELECT
22+
DISTINCT product_id
23+
FROM Products
24+
),
25+
cte2 AS (
26+
SELECT
27+
product_id
28+
,MAX(change_date) AS change_date
29+
FROM Products
30+
WHERE change_date <= '2019-08-16'
31+
GROUP BY product_id
32+
)
33+
SELECT
34+
p1.product_id
35+
,COALESCE(p3.new_price, 10) AS price
36+
FROM cte1 AS p1
37+
LEFT JOIN cte2 AS p2 ON p1.product_id = p2.product_id
38+
LEFT JOIN Products AS p3 ON p2.product_id = p3.product_id AND p2.change_date = p3.change_date
39+
;
40+
```
41+
42+
## Site Solution
43+
44+
```sql
45+
-- LeetCode Solution
46+
-- Solution #1: Divide cases by using `UNION ALL`
47+
SELECT
48+
product_id,
49+
10 AS price
50+
FROM
51+
Products
52+
GROUP BY
53+
product_id
54+
HAVING
55+
MIN(change_date) > '2019-08-16'
56+
UNION ALL
57+
SELECT
58+
product_id,
59+
new_price AS price
60+
FROM
61+
Products
62+
WHERE
63+
(product_id, change_date) IN (
64+
SELECT
65+
product_id,
66+
MAX(change_date)
67+
FROM
68+
Products
69+
WHERE
70+
change_date <= '2019-08-16'
71+
GROUP BY
72+
product_id
73+
)
74+
75+
-- Solution #2: Divide cases by using `LEFT JOIN`
76+
SELECT
77+
UniqueProductId.product_id,
78+
IFNULL (LastChangedPrice.new_price, 10) AS price
79+
FROM
80+
(
81+
SELECT DISTINCT
82+
product_id
83+
FROM
84+
Products
85+
) AS UniqueProductIds
86+
LEFT JOIN (
87+
SELECT
88+
Products.product_id,
89+
new_price
90+
FROM
91+
Products
92+
JOIN (
93+
SELECT
94+
product_id,
95+
MAX(change_date) AS change_date
96+
FROM
97+
Products
98+
WHERE
99+
change_date <= "2019-08-16"
100+
GROUP BY
101+
product_id
102+
) AS LastChangedDate USING (product_id, change_date)
103+
GROUP BY
104+
product_id
105+
) AS LastChangedPrice USING (product_id)
106+
107+
-- Solution #3: Use the window function
108+
SELECT
109+
product_id,
110+
IFNULL (price, 10) AS price
111+
FROM
112+
(
113+
SELECT DISTINCT
114+
product_id
115+
FROM
116+
Products
117+
) AS UniqueProducts
118+
LEFT JOIN (
119+
SELECT DISTINCT
120+
product_id,
121+
FIRST_VALUE (new_price) OVER (
122+
PARTITION BY
123+
product_id
124+
ORDER BY
125+
change_date DESC
126+
) AS price
127+
FROM
128+
Products
129+
WHERE
130+
change_date <= '2019-08-16'
131+
) AS LastChangedPrice USING (product_id);
132+
```
133+
134+
## Notes
135+
136+
TBD
137+
138+
## NB
139+
140+
`UNION ALL`, `LEFT JOIN`, Window Function
141+
142+
Go to [Index](../?tab=readme-ov-file#index)\
143+
Go to [Overview](../?tab=readme-ov-file)
144+

0 commit comments

Comments
 (0)