LeetCode:197.上升的温度
题目
给定一个 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 中的分析函数,偏移函数 lag 、lead,不过不知道这里支不支持窗口函数,测试一番再说。
---- 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 环境中,使用 join 和 datediff 函数进行求解。
---- 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
证明,偏移函数还可以可以的!!!
思考
复习一下 MySQL 的 datediff 函数。
第一个参数减掉第二个参数。
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.上升的温度的更多相关文章
- LeetCode 197. Rising Temperature (上升的温度)
题目标签: 题目给了我们一个 温度表格,让我们找到 所有温度比之前一天高的,返回id. 建立 Weather w1, Weather w2,找到当w1 的温度 大于 w2 的时候,而且 w1 的日期是 ...
- [LeetCode] Daily Temperatures 日常温度
Given a list of daily temperatures, produce a list that, for each day in the input, tells you how ma ...
- leetcode 197. Rising Temperature sql_Date用法
https://leetcode.com/problems/rising-temperature/description/ 题目需要选出今天比昨天气温高的ID 用join,默认是inner join需 ...
- LeetCode 739:每日温度 Daily Temperatures
题目: 根据每日 气温 列表,请重新生成一个列表,对应位置的输入是你需要再等待多久温度才会升高超过该日的天数.如果之后都不会升高,请在该位置用 0 来代替. 例如,给定一个列表 temperature ...
- Leetcode 197. Rising Temperature
Given a Weather table, write a SQL query to find all dates' Ids with higher temperature compared to ...
- [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 ...
- leetcode题库
leetcode题库 #题名题解通过率难度出现频率 1 两数之和 46.5%简单2 两数相加 35.5%中等3 无重复字符的最长子串 31.1%中等4 寻找两个有序数组的中位 ...
- 【MySQL 基础】MySQ LeetCode
MySQL LeetCode 175. 组合两个表 题目描述 表1: Person +-------------+---------+ | 列名 | 类型 | +-------------+----- ...
- mysql学习 | LeetCode数据库简单查询练习
力扣:https://leetcode-cn.com/ 力扣网数据库练习:https://leetcode-cn.com/problemset/database/ 文章目录 175. 组合两个表 题解 ...
随机推荐
- 《你不知道的JavaScript(上)》笔记——动态作用域
动态作用域让作用域作为一个在运行时就被动态确定的形式, 而不是在写代码时进行静态确定的形式.动态作用域并不关心函数和作用域是如何声明以及在何处声明的, 只关心它们从何处调用. 换句话说, 作用域链是基 ...
- 设置django 时间
使用Django的DateTimeField(auro_now_add=True)设置当前时间为创建时间时,时间往往与当前时间对应不上,这是由于Django默认使用的是[UTC](世界标准时间)时区, ...
- hadoop1.2.1安装配置
原文地址 环境:ubuntu13 使用的用户为普通用户.如:用户ru jdk安装略 1.安装ssh (1) sudo apt-get install openssh-server (2)配置ssh面密 ...
- Firefox Chrome Http请求插件
Firefox:HttpRequester Chrome:Advanced Rest Client
- List的remove方法里的坑
今天遇到一件怪事,用一个ArrayList添加了一个对象,再调用ArrayList的remove方法删除该对象,当然这时对象是数据库里查出来的,但内容绝对是一样,却发现remove失败了.演示一下,这 ...
- springboot动态定时任务
SpringBoot设置动态定时任务 一.说明 1.在我们日常的开发中,很多时候,定时任务都不是写死的,而是写到数据库中,从而实现定时任务的动态配置. 2.定时任务执行时间可根据数据库中某个设置字段动 ...
- Hystrix-基本概念(设计原则和两种隔离技术)
一.Hystrix是什么在微服务的架构系统中,每个服务都可能会调用很多其他服务,被调用的那些服务就是依赖服务.有的时候某些依赖服务出现故障也是很正常的.Hystrix可以让我们在对服务间的调用进行控制 ...
- nginx使用vhost子目录
在主配置文件http模块最后添加如下一句话 [root@host---- ~]# vi /etc/nginx/nginx.conf include /etc/nginx/conf.d/*.conf; ...
- 思科S系列220系列交换机多个漏洞预警
补天漏洞响应平台近期监测思科官方发布了关于思科 S 系列 220 系列交换机的3个漏洞修复通告,其中包含2个高危漏洞,最高CVSS 3.0评分9.8. 更新时间 2019年 08月 09日 威胁目标 ...
- 多路径IO
多路径IO(MPIO)是一个微软架构,通过在存储设备和windows操作系统之间提供一个备用数据路径,来减轻主机总线适配器(HBA)故障的影响,MPIO能够支持多达32个备用路径,来增加Windows ...