1164. Product Price at a Given Date
https://leetcode.com/problems/product-price-at-a-given-date/
Product Price at a Given Date - LeetCode
Can you solve this real interview question? Product Price at a Given Date - Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
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.
2019-08-16의 모든 제품 가격을 찾는 SQL 쿼리를 작성하세요. 변경 전 모든 제품의 가격이 10이라고 가정합니다.
# 1
SELECT DISTINCT t1.product_id
, IFNULL(t2.new_price, 10) AS price
FROM Products t1 LEFT JOIN
(SELECT *
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)) as t2
ON t1.product_id = t2.product_id
# 2
-- 8월 16일 이후 변경된 가격은 10으로 통일
SELECT DISTINCT product_id, 10 as price
FROM Products
GROUP BY product_id
HAVING (min(change_date) > "2019-08-16")
UNION
-- 8월 16일 이전 변경된 가격 조회
SELECT product_id, new_price
FROM Products p2
WHERE (product_id, change_date) IN (select product_id, max(change_date)
from Products
where change_date <= "2019-08-16"
group by product_id)
'🛠️Skill > CodingTest' 카테고리의 다른 글
[leetcode] 1174. Immediate Food Delivery II / 즉시배송할 수 있는 주문비율 (0) | 2023.03.08 |
---|---|
[leetcode] 550. Game Play Analysis IV/ 이틀 연속 로그인한 유저 /LEFT JOIN/IN (0) | 2023.02.19 |
[leetcode] 577. Employee Bonus / COALESCE (0) | 2023.01.15 |
[leetcode] 1158. Market Analysis I / CASE WHEN, IFNULL (0) | 2022.12.30 |
[프로그래머스] Lv4. 저자 별 카테고리 별 매출액 집계하기 (0) | 2022.12.18 |
댓글