hyezdata 님의 블로그

Rising Temperature 본문

코딩테스트

Rising Temperature

hyezdata 2026. 5. 7. 14:17
# 전날에 비해 높은 온도인 날 id 출력

select w1.id as Id
from weather w1
    join weather w2
        on datediff(w1.recordDate, w2.recordDate) = 1
where w1.temperature > w2.temperature

 

첫번째 답

# 전날에 비해 높은 온도인 날 id 출력

select id as Id
from (
    select id, temperature, lag(temperature, 1) over (order by recordDate) as before_temp
    from Weather
) a
where temperature >= before_temp

 

-> 이전 행이 반드시 전날일거라는 가능성 없을 수도 있음

-> 그래서 lag 쓰면 안됨 쓰면 안됨이 아니라 이렇게 쓰면 안됨

날짜가 연속되지 않은 경우

 

만약 lag를 쓰고 싶다면

(다른 사람 쿼리)

select id
from (select *,
lag(temperature) over(order by recordDate) as prrev,
lag(recordDate) over(order by recordDate) as prev_date
from Weather) p
where temperature>p.prrev and datediff(recordDate,prev_date)=1

 

 

Rising Temperature - LeetCode

Can you solve this real interview question? Rising Temperature - Table: Weather +---------------+---------+ | Column Name | Type | +---------------+---------+ | id | int | | recordDate | date | | temperature | int | +---------------+---------+ id is the co

leetcode.com

 

728x90
반응형

'코딩테스트' 카테고리의 다른 글

Trips and Users  (0) 2026.05.11
Game Play Analysis I  (0) 2026.05.07
Delete Duplicate Emails  (0) 2026.05.06
Department Top Three Salaries  (0) 2026.05.06
Department Highest Salary  (0) 2026.05.06