[LeetCode/SQL] 1164. Product Price at a Given Date

Sooyeon·2024년 1월 15일
0

문제풀이 

목록 보기
85/95
post-thumbnail

[LeetCode/SQL]


📌 1164. Product Price at a Given Date

문제

풀이

각 상품별 2019-08-16일자 기준으로 변경된 가격을 출력하라. 
만약 16일 이후 변경되었다면,디폴트 가격은 10

방법 I

WITH cte AS
(SELECT *, RANK() OVER (PARTITION BY product_id ORDER BY change_date DESC) AS r
FROM Products
WHERE change_date<= '2019-08-16')

SELECT product_id, new_price AS price
FROM cte
WHERE r = 1
UNION
SELECT product_id, 10 AS price
FROM Products
WHERE product_id NOT IN (SELECT product_id FROM cte)

0개의 댓글