今天和大家聊的问题叫做 上升的温度,我们先来看题面: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 。返回结果 不要求顺序 。
解题
首先我们找到每一天的前一天,然后判断温度是否升高了,筛选出温度上升的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;
好了,今天的文章就到这里,如果觉得有所收获,请顺手点个在看或者转发吧,你们的支持是我最大的动力 。