Rising Temperature


description:
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 |
+----+

这道题不难,但是要借助MySQL的相关时间函数才能解决,所以记录一下。
题意是让在Weather表当中找出当前那天比前一天温度高的那天的Id。
我刚开始没想那么多,直接用日期相减,这样写的:

# Write your MySQL query statement below
select w2.id from Weather w1 join Weather w2 on w1.recorddate=w2.recorddate-1
where w2.temperature > w1.temperature;

这样写果然就WA了,date类型的数据应该是不能直接相加减这样写,后面查了个函数DATEDIFF(a, b):计算a和b两个日期的差值a-b。用了这个函数后就过了。
Mycode:

# Write your MySQL query statement below
select w2.id from Weather w1 join Weather w2 on DATEDIFF(w2.recorddate, w1.recorddate) = 1
where w2.temperature > w1.temperature;

另外还有两个函数也介绍一下:
TO_DAYS(a): 用来将日期a换算成天数
SubDate(a,b): 用来将日期a减掉b天,基本和Date_Sub(a, b)一样用
因此,这道题也可以这样写:

# Write your MySQL query statement below
select w2.id from Weather w1 join Weather w2 on To_DAYS(w2.recorddate)=To_DAYS(w1.recorddate)+1
where w2.temperature > w1.temperature;
# Write your MySQL query statement below
select w2.id from Weather w1 join Weather w2 on SubDate(w2.recorddate, 1) = w1.recorddate
where w2.temperature > w1.temperature;

谢谢你的观看!

Rising Temperature相关推荐

  1. 【sql】统计温度比前一天高的id Rising Temperature

    为什么80%的码农都做不了架构师?>>>    问题: Given a Weather table, write a SQL query to find all dates' Ids ...

  2. 【sql】leetcode习题 (共 42 题)

    [175]Combine Two Tables (2018年11月23日,开始集中review基础) Table: Person +-------------+---------+ | Column ...

  3. Leetcode之Database篇

    早晨和陈John一起来实验室的路上听他说起leetcode上也有数据库和shell的练习.于是拿来练练手,发现数据库的题只有几道而且做得也很快AC率也蛮高,权当复习了一下数据库的基本语法了吧. 1:E ...

  4. python如何统计累计每日的人数‘’_每日一练 | Data Scientist amp; Business Analyst amp; Leetcode 面试题 902...

    点击上方蓝字 会变美 " 每 日 一 练 " Jun. 30 Data Application Lab 自2017年6月15日起,每天和你分享讨论一道数据科学(DS)和商业分析(B ...

  5. LeetCode 简单算法题

    使用Nodejs 抓取的LeetCode 简单算法题  一步一步来,先攻破所有简单的题目,有些题目不适合使用JS解决,请自行斟酌 Letcode 简单题汇总 104. Maximum Depth of ...

  6. 【航线运输驾驶员理论考试】气象学

    1.TAF is the weather forcast information applied to an area within ( ) from the center of an airport ...

  7. LeetCode 从零单刷个人笔记整理(持续更新)

    更新至2020.2.23 github:https://github.com/ChopinXBP/LeetCode-Babel 本人博客用于个人对知识点的记录和巩固. 用几乎所有可行的方法进行了实现和 ...

  8. 【LeetCode 简单题】50-上升的温度

    声明: 今天是第50道题.给定一个 Weather 表,编写一个 SQL 查询,来查找与之前(昨天的)日期相比温度更高的所有日期的 Id.以下所有代码经过楼主验证都能在LeetCode上执行成功,代码 ...

  9. LeetCode All in One 题目讲解汇总(持续更新中...)

    原文地址:https://www.cnblogs.com/grandyang/p/4606334.html 终于将LeetCode的大部分题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开 ...

最新文章

  1. python中print又可将数据写入文件_Python 通过print_lol将数据保存到文件中
  2. php设置背景图片的代码,css设置背景图片如何实现?(代码实例)
  3. echo -n 和echo -e 参数意义
  4. jdbc和mysql面试题_JDBC数据访问技术面试题(附答案)
  5. 图片上传时即时生成多个缩略图
  6. 面试官:为什么SpringBoot的 jar 可以直接运行?
  7. makefile文件的书写规则(make和makefile)
  8. 基本的SVG样式属性
  9. 为你的Web程序加个启动画面
  10. linux移植简介[MS2]
  11. excel数据处理_如何用excel做仓库管理软件?这样做很好用,有权限和流程
  12. latex中文简历,硕博士找工作实习用,顶级简约简历
  13. GIS数据在哪里下载
  14. Python.习题七 函数(上)
  15. B站云E办Vue+SpringBoot前后端分离项目——MVC三层架构搭建后台项目
  16. python round_Python round() 函数
  17. php下单声音提醒,拼多多商家怎么设置下单的声音?开启方法是什么?
  18. Gvim,Vim编辑器快速学习介绍
  19. FENeRF: Face Editing in Neural Radiance Fields
  20. SSM框架+WebSocket实现网页聊天(Spring+SpringMVC+MyBatis+WebSocket)

热门文章

  1. 感受HTML5 Audio API带来的视听盛宴
  2. 200 行代码实现一个简单的区块链
  3. P1072新年趣事之债务解题报告
  4. URAL - 1721 Two Sides of the Same Coin
  5. 玩家类pk{游戏}测试类
  6. 08年刘翔广告收入近亿 冠军身份令身价飙升
  7. 时间的换算(时、分、秒、毫秒、微秒、纳秒)
  8. null pointer exception解决方法
  9. 推荐几个美团系大佬的公众号
  10. Dota2国际冠军赛如火如荼,可以邀请朋友一同观看VR直播