Algorithm/Leetcode

리트코드 - Rising temperature

JHeaon 2024. 5. 13. 09:50

 

문제

https://leetcode.com/problems/rising-temperature/description/

 

해결

쉽게 말하자면 전 날의 온도와 현재 온도를 비교하여 전 날보다 높은 날의 ID을 가져오는 문제이다.

 

여기서 문제는 오늘날의 온도와 전 날의 온도를 비교해야 하는 부분인데 처음에는 select문에서 IF()함수와 LAG()함수를 이용하여 처리하려고 했으나, IF문 조건에 맞지 않는 것들이 NULL값으로 반환되고 이를 WHERE문으로 처리하는 것이 힘들었다.

 

따라서 이를 join을 통해 전 날짜와 현 날짜를 합친 테이블을 만든 뒤에 where문 조건으로 처리하는식으로 코드를 작성하였다. 

 

select today.id
from weather today left join weather yesterday on today.recordDate = date_add(yesterday.recordDate, interval 1 day)
where today.temperature - yesterday.temperature > 0

 

 

'Algorithm/Leetcode'의 다른글

  • 현재글 리트코드 - Rising temperature