"꾸준하고 완벽한 한 걸음"

PS

[LeetCode] 197. Rising Temperature [MySQL]

kimyoungrok 2025. 2. 26. 15:08
728x90

문제

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


풀이

이전 날짜보다 현재 날짜의 temperature 가 더 높아진 행의 id 를 출력하는 문제다.

결과에는 id만 나와야 하므로, LAG 대신 조건을 만족하는 행을 조인해주는 방식으로 해결했다.

또한, 이전 날짜와 현재 날짜의 차이는 하루여야 하므로 DATEDIFF의 결과가 1인 행에 대해서만 계산했다.

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

소스코드

https://github.com/rogi-rogi/problem-solving/blob/main/leetcode/sql/easy/197-rising-temperature.sql

728x90