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