Rising Temperature - LeetCode

Pepzera·5일 전

SQL코딩테스트

목록 보기
17/18

Rising Temperature 문제

출처 : LeetCode Rising Temperature

Table
Weather

Column NameType
idint
recordDatedate
temperatureint

id is the column with unique values for this table.
There are no different rows with the same recordDate.
This table contains information about the temperature on a certain day.

Q.

Write a solution to find all dates id with higher temperatures compared to its previous dates (yesterday).
Return the result table in any order.
The result format is in the following example.


질문

어제보다 기온이 더 높았던 날짜의 id를 찾는 문제

주의!!

  • 비교 대상은 바로 전날(yesterday)이다! 날짜를 잘 확인하자

내 답안 📕

WITH new_temp AS (
    SELECT *
         , LAG(temperature, 1) OVER(ORDER BY recordDate) AS yesterday_temp
         , DATEDIFF(recordDate, LAG(recordDate, 1) OVER(ORDER BY recordDate)) AS date_diff
    FROM Weather
)

SELECT id AS 'Id'
FROM new_temp
WHERE temperature > yesterday_temp
  AND date_diff = 1

0개의 댓글