[SQL]LeetCode197. 上升的温度 | Rising Temperature
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10180555.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
SQL架构
1 Create table If Not Exists Weather (Id int, RecordDate date, Temperature int) 2 Truncate table Weather 3 insert into Weather (Id, RecordDate, Temperature) values ('1', '2015-01-01', '10') 4 insert into Weather (Id, RecordDate, Temperature) values ('2', '2015-01-02', '25') 5 insert into Weather (Id, RecordDate, Temperature) values ('3', '2015-01-03', '20') 6 insert into Weather (Id, RecordDate, Temperature) values ('4', '2015-01-04', '30')
Given a Weather
table, write a SQL query to find all dates' Ids with higher temperature compared to its previous (yesterday's) dates.
+---------+------------------+------------------+ | Id(INT) | RecordDate(DATE) | Temperature(INT) | +---------+------------------+------------------+ | 1 | 2015-01-01 | 10 | | 2 | 2015-01-02 | 25 | | 3 | 2015-01-03 | 20 | | 4 | 2015-01-04 | 30 | +---------+------------------+------------------+
For example, return the following Ids for the above Weather
table:
+----+ | Id | +----+ | 2 | | 4 | +----+
给定一个 Weather
表,编写一个 SQL 查询,来查找与之前(昨天的)日期相比温度更高的所有日期的 Id。
+---------+------------------+------------------+ | Id(INT) | RecordDate(DATE) | Temperature(INT) | +---------+------------------+------------------+ | 1 | 2015-01-01 | 10 | | 2 | 2015-01-02 | 25 | | 3 | 2015-01-03 | 20 | | 4 | 2015-01-04 | 30 | +---------+------------------+------------------+
例如,根据上述给定的 Weather
表格,返回如下 Id:
+----+ | Id | +----+ | 2 | | 4 | +----+
163ms
1 # Write your MySQL query statement below 2 SELECT t.id 3 FROM (SELECT i.*, 4 IF(@last_date + interval 1 day = RecordDate and @last_temp < Temperature, 1, 0) AS rownum, 5 @last_temp := Temperature, 6 @last_date := RecordDate 7 FROM (select @last_date := null, @last_temp := null) AS _init 8 , Weather i 9 ORDER BY i.RecordDate) AS t 10 WHERE t.rownum = 1;
166ms
1 # Write your MySQL query statement below 2 select b.keepId as Id 3 from (select if(Temperature > @prevTemp and datediff(RecordDate, @prevDate) = 1, a.Id, NULL) as keepId, 4 @prevTemp := Temperature, @prevDate := RecordDate 5 from (select * from Weather order by RecordDate) a, 6 (select @prevTemp := NULL, @prevDate := NULL) init) b 7 where keepId is not NULL
169ms
1 # Write your MySQL query statement below 2 3 SELECT Id 4 FROM 5 ( SELECT Id, Temperature, 6 @Higher := If(@Pretemp < Temperature AND 1 = DATEDIFF(RecordDate,@Predate), 1, 0) h, 7 @Predate := RecordDate predate, 8 @Pretemp := Temperature pretemp 9 FROM (SELECT @Higher := 0, @Pretemp := 101, @Predate := 1000-01-01) w1, 10 (SELECT * FROM Weather ORDER BY RecordDate) w2 11 ) t WHERE t.h = 1
170ms
1 # Write your MySQL query statement below 2 select f2.Id from (select f.Id, if(Temperature>@prev and datediff(RecordDate, @prevDate)=1, 1, 0) as rising, @prev:=Temperature, @prevDate:=RecordDate from (select Id, RecordDate, Temperature from (select * from Weather order by RecordDate asc) ordered, (select @rising:=NULL, @prev:=NULL, @prevDate:=NULL) tmp) f) f2 where f2.rising = 1 order by f2.Id;
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了