题目链接:https://leetcode-cn.com/problems/rising-temperature/

题目

给定一个 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 |

+----+

来源:力扣(LeetCode)

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

著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

解答

第一感觉,用 oracle 中的分析函数,偏移函数 laglead,不过不知道这里支不支持窗口函数,测试一番再说。

---- oracle ----
/* Write your PL/SQL query statement below */
select Id
from
(
select Id,
RecordDate,
Temperature,
lag(Temperature,1) over(order by RecordDate) as Temperature_2
from Weather
)
where Temperature > Temperature_2;

测试用例不通过,因为有一个测试用例是中间间隔了1天,并非前后2天,所以这道题不能通过偏移函数来进行求解,还是得通过前后2天进行连接,如果找不到对应的时间差则关联不上,这样的答案才是正确的。

MySQL 环境中,使用 joindatediff 函数进行求解。

---- MySQL ----
select a.Id as Id
from Weather a
left join Weather b
on datediff(a.RecordDate, b.RecordDate) = 1
where a.Temperature > b.Temperature; ---- 274ms
-- 第一次提交的时候把最后温度的过滤条件写成了and,怪不得提交不通过,改为where之后便可以了。
---- MySQL ----
# Write your MySQL query statement below
select a.Id
from Weather a,
Weather b
where a.Temperature > b.Temperature
and datediff(a.RecordDate, b.RecordDate) = 1; ---- 275ms

这样子就通过?得好好考虑一下。。

---- oracle ----
/* Write your PL/SQL query statement below */
select a.Id as Id
from Weather a
left join Weather b
on a.RecordDate = b.RecordDate - 1
where a.Temperature > b.Temperature;
---- 没通过

其实本身,这样子的解法是没有问题的,只是测试样例中的数据不够规范,所以测试才不通过。

另外,果然看到一种通过偏移函数解答的,再进行尝试一番,修改一下。

---- oracle ----
/* Write your PL/SQL query statement below */
select t.Id as Id
from
(
select Id,
RecordDate,
Temperature,
lag(Temperature,1) over(order by RecordDate) as Temperature_2,
lag(RecordDate,1) over(order by RecordDate) as RecordDate_2
from Weather
) t
where t.Temperature > t.Temperature_2
and round(to_number(t.RecordDate - t.RecordDate_2)) = 1; ---- 537ms

证明,偏移函数还可以可以的!!!

思考

复习一下 MySQLdatediff 函数。

第一个参数减掉第二个参数。

datediff('2007-12-31','2007-12-30');   # 1
datediff('2010-12-30','2010-12-31'); # -1

另外,也可以通过 date_add 函数进行时间加减。

date_add('2019-10-26', interval 1 day)

使用 oracle 中的偏移函数进行求解。

LeetCode:197.上升的温度的更多相关文章

  1. LeetCode 197. Rising Temperature (上升的温度)

    题目标签: 题目给了我们一个 温度表格,让我们找到 所有温度比之前一天高的,返回id. 建立 Weather w1, Weather w2,找到当w1 的温度 大于 w2 的时候,而且 w1 的日期是 ...

  2. [LeetCode] Daily Temperatures 日常温度

    Given a list of daily temperatures, produce a list that, for each day in the input, tells you how ma ...

  3. leetcode 197. Rising Temperature sql_Date用法

    https://leetcode.com/problems/rising-temperature/description/ 题目需要选出今天比昨天气温高的ID 用join,默认是inner join需 ...

  4. LeetCode 739:每日温度 Daily Temperatures

    题目: 根据每日 气温 列表,请重新生成一个列表,对应位置的输入是你需要再等待多久温度才会升高超过该日的天数.如果之后都不会升高,请在该位置用 0 来代替. 例如,给定一个列表 temperature ...

  5. Leetcode 197. Rising Temperature

    Given a Weather table, write a SQL query to find all dates' Ids with higher temperature compared to ...

  6. [LeetCode] 197. Rising Temperature_Easy tag: SQL

    Given a Weather table, write a SQL query to find all dates' Ids with higher temperature compared to ...

  7. leetcode题库

    leetcode题库 #题名题解通过率难度出现频率  1 两数之和     46.5%简单2 两数相加     35.5%中等3 无重复字符的最长子串     31.1%中等4 寻找两个有序数组的中位 ...

  8. 【MySQL 基础】MySQ LeetCode

    MySQL LeetCode 175. 组合两个表 题目描述 表1: Person +-------------+---------+ | 列名 | 类型 | +-------------+----- ...

  9. mysql学习 | LeetCode数据库简单查询练习

    力扣:https://leetcode-cn.com/ 力扣网数据库练习:https://leetcode-cn.com/problemset/database/ 文章目录 175. 组合两个表 题解 ...

随机推荐

  1. app微信支付的集成步骤

    1.引用地址 //微信支付 compile 'com.tencent.mm.opensdk:wechat-sdk-android-with-mta:+' 2.注册 private IWXAPI api ...

  2. C之静态内存和动态内存

    静态内存: * 自动申请,自动释放* 大小固定,内存空间连续* 从栈上分配的内存叫静态内存 动态内存: * 程序员自己申请 * new/malloc* 大小取决于虚拟内存的大小,内存空间不连续* ja ...

  3. SVM算法总结

    svm算法通俗的理解在二维上,就是找一分割线把两类分开,问题是如下图三条颜色都可以把点和星划开,但哪条线是最优的呢,这就是我们要考虑的问题: 首先我们先假设一条直线为 W•X+b =0 为最优的分割线 ...

  4. 基于文件系统(及MySQL)使用Java实现MapReduce

    实现这个代码的原因是: 我会MapReduce,但是之前都是在AWS EMR上,自己搭过伪分布式的,但是感觉运维起来比较困难: 我就MySQL会一点(本来想用mongoDB的但是不太会啊) 数据量不是 ...

  5. Pytorch Tensor, Variable, 自动求导

    2018.4.25,Facebook 推出了 PyTorch 0.4.0 版本,在该版本及之后的版本中,torch.autograd.Variable 和 torch.Tensor 同属一类.更确切地 ...

  6. C语言tips_2 关于scanf 读取规则小结以及与getchar 的区别

    第一点:scanf默认回车和空格是输入不同组之间的间隔和结束符号. 也就是说他不会读取 空格 和 换行符.而是把他们当作一个 数据被读取完成的标志!他的停止标志则为,当%d之类的数据输入结束之后,自动 ...

  7. souce and bash 的区别

    对于一些环境变量的配置文件,如想使更改后立即生效,多用 souce +file 执行后即可.如/etc/profile 里加了配置, source 和  bash 的区别: source filena ...

  8. 集群架构03·MySQL初识,mysql8.0环境安装,mysql多实例

    官方网址 https://dev.mysql.com/downloads/mysql/社区版本分析 MySQL5.5:默认存储引擎改为InnoDB,提高性能和可扩展性,增加半同步复制 MySQL5.6 ...

  9. upload上传通关游戏

    第一关:后缀名限制,抓包改一下后缀. 前端脚本检测文件扩展名.当客户端选择文件点击上传的时候,客户端还没有向服务器发送任何消 息,前端的 js 脚本就对文件的扩展名进行检测来判断是否是可以上传的类型 ...

  10. 不同Json工具对空串和NULL的序列号处理:net.sf.json 和 fastjson

    目录 1.测试代码 2.测试结果: 3.总结: 4.注:Maven中引入net.sf.json的方式 net.sf.json 和 fastjson 对于空串和NULL的处理: 1.测试代码 packa ...