LeetCode 数据库面试题 – 上升的温度

本页内容

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

Id(INT)RecordDate(DATE)Temperature(INT)
12015-01-0110
22015-01-0225
32015-01-0320
42015-01-0430

例如,根据上述给定的 Weather 表格,返回如下 Id:

Id
2
4

解答

1SELECT
2    w1.Id
3FROM
4    Weather w1
5LEFT JOIN
6    Weather w2
7        ON DATEDIFF(w1.RecordDate, w2.RecordDate) = 1
8WHERE w2.Id IS NOT NULL 
9    AND w1.Temperature > w2.Temperature

原题

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/rising-temperature