LeetCode 上升的温度
给定一个 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 |
+----+
- 使用 join 和 datediff 日期函数 求差 自连接匹配出有昨天的数据
select * from weather w1 join weather w2 on DATEDIFF(w1.RecordDate,w2.RecordDate)=1
id RecordDate Temperature w2.id 昨天的日期 w2.Temperature(昨天的温度)
2 2019-01-02 25 1 2019-01-01 10
4 2019-01-04 30 3 2019-01-03 20
4 2019-01-04 30 3 2019-01-03 20
条件帅选今天比昨天温度高的数据 w1.Temperature>w2.Temperature
select w1.id from weather w1 join weather w2 on DATEDIFF(w1.RecordDate,w2.RecordDate)=1 where w1.Temperature>w2.Temperature
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

上一篇: 删除重复的电子邮箱
下一篇: LeetCode 行程和用户
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论