Rising Temperature

수이·2025년 4월 17일

🟢 코드카타 / SQL

목록 보기
85/88

Write a solution to find all dates' id with higher temperatures compared to its previous dates (yesterday). Return the result table in any order.
문제링크

조건정리

  1. 하루전날 온도 비교 > 오늘이 더 더울 경우
  2. ID만 출력

풀이

  1. LAG 활용 🔴
SELECT id
FROM
(
    SELECT *, 
    LAG(temperature) OVER (ORDER BY recorddate) AS yesterday_temp
    FROM weather
) AS a
WHERE temperature > yesterday_temp

lag 활용해서 이전 행의 온도만 가져오는 식으로 풀었다
테스트케이스에서는 통과했는데 정답에서 틀렸다고 나옴🥲

2015-01-01 이전 날짜가 없어서 null값으로 출력돼서 그런 것 같아서 방식 바꿈

  1. self join 🟢
SELECT today.id
FROM weather today 
INNER JOIN weather yesterday ON DATEDIFF(today.recorddate, yesterday.recorddate) = 1 
WHERE today.temperature > yesterday.temperature

단순해보이지만 생각보다 어려운 문제였따💦

0개의 댓글