๐ข ๋ณธ ํฌ์คํ ์ ํ์ฉ๋๋ ๊ธฐ๋ณธ ๋ฌธ์ ๋ฐ ์๋ฃ ์ถ์ฒ๋
๋ฆฌํธ์ฝ๋ Problems, https://leetcode.com/problemset/all/์์ ๋ฐํ๋๋ค.
โ ๋ฌธ์
https://leetcode.com/problems/product-price-at-a-given-date/
Table : Products
Write an SQL query 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 query result format is in the following example.
โ ํ์ด
๋ฌธ์ ์๊ตฌ์ฌํญ
2019.08.16 ๊ธฐ์ค ๋ชจ๋ ์ํ์ ๊ฐ๊ฒฉ์ ์ถ๋ ฅํ๋๋ฐ
๋ฐ์ดํฐ๋ change_date์ ์ ํ๋ณ ๊ฐ๊ฒฉ์ด ์๋กญ๊ฒ ๋ณ๊ฒฝ๋์์ ์๋ ค์ค๋ค.
2019.08.16 ๊น์ง ๋ณํ๊ฐ ์์๋ ์ํ์ ๊ฐ๊ฒฉ์ 10์ผ๋ก ์ถ๋ ฅํ๊ณ ๋๋จธ์ง๋ ์ต์ ์ผ์์ ๋ณ๊ฒฝ ๊ฐ๊ฒฉ์ ์ถ๋ ฅํ๋ฉด ๋๋ค.
2019.08.16 ๊ธฐ์ค ์ํ ์์ด๋๋ณ ๊ฐ์ฅ ์ต๊ทผ ๊ฐ๊ฒฉ ๋ณ๊ฒฝ์ผ์์ ๊ฐ๊ฒฉ์ ์ถ๋ ฅํ๋ ์ฟผ๋ฆฌ๋ฌธ1๊ณผ
2019.08.16๊น์ง ๊ฐ๊ฒฉ ๋ณ๊ฒฝ์ด ์์๋ ์ํ์ ๊ฒฝ์ฐ(์๋ธ์ฟผ๋ฆฌ๋ก ์์ด๋ ๋น๊ต) new_price ์ปฌ๋ผ์ ๊ฐ์ 10์ผ๋ก ์ง์ ํด์ union์ผ๋ก ์งํฉ์ฒ๋ฆฌ ํด์ค๋ค.
select product_id, new_price as price
from Products
where (product_id, change_date) in (
select product_id, max(change_date)
from Products
where change_date <= '2019-08-16'
group by product_id
)
union
select distinct product_id, 10 as price
from Products
where product_id not in (
select product_id from Products
where change_date <= '2019-08-16')
'Growth ๐ณ > Practice ๐ป' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Leet code] 619. Biggest Single Number (0) | 2023.09.02 |
---|---|
[Leet code] 1173. Immediate Food Delivery I (0) | 2023.08.03 |
[LeetCode] 1321. Restaurant Growth (3) | 2023.06.17 |
[LeetCode] 1158. Market Analysis I (0) | 2023.06.08 |
[LeetCode] 1070. Product Sales Analysis III (0) | 2023.05.22 |