SQL/문제풀이

[LeetCode] Rising Temperature

응엉잉 2024. 3. 1. 15:00

https://leetcode.com/problems/rising-temperature/description/?envType=study-plan-v2&envId=top-sql-50

 

recordDate를 기준으로 JOIN

(a가 previous day가 되도록)

SELECT b.Id
FROM weather a
    INNER JOIN weather b ON a.recordDate = b.recordDate - 1 -- a : yesterday
WHERE a.temperature < b.temperature

LEFT JOIN이 아닌 INNER JOIN을 써줘야 연속된 날짜를 구할 수 있다.

 

 

DATE 계산을 조금 더 깔끔하게 적으면 이렇게 적을 수도 있다 !

SELECT w2.id
FROM Weather w1
JOIN Weather w2
    ON DATEDIFF(w1.recordDate, w2.recordDate) = -1
AND w2.temperature>w1.temperature