SQL 上升的温度

197 上升的温度

SQL架构

表: Weather

±--------------±--------+

| Column Name | Type |

±--------------±--------+

| id | int |

| recordDate | date |

| temperature | int |

±--------------±--------+

id 是这个表的主键

该表包含特定日期的温度信息

编写一个 SQL 查询,来查找与之前(昨天的)日期相比温度更高的所有日期的 id 。

返回结果 不要求顺序 。

查询结果格式如下例。

示例 1:

输入:

Weather 表:

±---±-----------±------------+

| id | recordDate | Temperature |

±---±-----------±------------+

| 1 | 2015-01-01 | 10 |

| 2 | 2015-01-02 | 25 |

| 3 | 2015-01-03 | 20 |

| 4 | 2015-01-04 | 30 |

±---±-----------±------------+

输出:

±---+

| id |

±---+

| 2 |

| 4 |

±---+

解释:

2015-01-02 的温度比前一天高(10 -> 25)

2015-01-04 的温度比前一天高(20 -> 30)

解决方案:

提供思路

如同之前的思路,该表join自身一次(cross join 笛卡尔集,详细可以百度查询),筛选条件。由于不同的sql有类似的语法,下面写出相关语法。

上代码:

sql 复制代码
//1
select a.id
from weather as a cross join weather as b 
     on datediff(a.recordDate, b.recordDate) = 1
where a.Temperature  > b.Temperature ;

//2
select a.id
from weather as a cross join weather as b 
     on timestampdiff(day, a.recordDate , b.recordDate ) = -1
where a.Temperature  > b.Temperature ;

以上是碰到的第一百九十七题,后续持续更新。感觉对你有帮助的小伙伴可以帮忙点个赞噢!

相关推荐
美好的事情能不能发生在我身上11 小时前
Leetcode热题100中的:哈希专题
算法·leetcode·哈希算法
逆境不可逃11 小时前
LeetCode 热题 100 之 41.缺失的第一个正数
算法·leetcode·职场和发展
keyborad pianist12 小时前
MySQL篇 Day1
数据库·mysql
数据知道12 小时前
MongoDB投影:如何只查询需要的字段,减少网络传输开销?
网络·数据库·mongodb
海兰13 小时前
ES 9.3.0 DSL 示例:从索引创建到混合搜索与 RRF 排序
大数据·数据库·elasticsearch
Volunteer Technology13 小时前
Oracle高级部分(触发器)
数据库·oracle
We་ct13 小时前
LeetCode 173. 二叉搜索树迭代器:BSTIterator类 实现与解析
前端·算法·leetcode·typescript
zhangyueping838513 小时前
5、MYSQL-DQL-多表关系
数据库·mysql
踩坑记录13 小时前
leetcode hot100 79. 单词搜索 medium 递归回溯
leetcode
kimi-22213 小时前
在 AutoDL 容器内安装 PostgreSQL + pgvector
数据库·postgresql