天天看點

​LeetCode刷題實戰197:上升的溫度

算法的重要性,我就不多說了吧,想去大廠,就必須要經過基礎知識和業務邏輯面試+算法面試。是以,為了提高大家的算法能力,這個公衆号後續每天帶大家做一道算法題,題目就從LeetCode上面選 !

今天和大家聊的問題叫做 上升的溫度,我們先來看題面:

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

Write an SQL query to find all dates' id with higher temperature compared to its previous dates (yesterday).

Return the result table in any order.

題意

編寫一個 SQL 查詢,來查找與之前(昨天的)日期相比溫度更高的所有日期的 id 。傳回結果 不要求順序 。

​LeetCode刷題實戰197:上升的溫度

解題

首先我們找到每一天的前一天,然後判斷溫度是否升高了,篩選出溫度上升的id即可。

select w1.Id as Id
from Weather w1
#連接配接Weather表(自連接配接)
inner join Weather w2
#連接配接條件,w2是w1的前一天
on datediff(w1.RecordDate, w2.RecordDate) = 1
#篩選條件:溫度升高
where w1.Temperature > w2.Temperature;           

複制

好了,今天的文章就到這裡,如果覺得有所收獲,請順手點個在看或者轉發吧,你們的支援是我最大的動力 。