【LeetCode】Rising Temperature

Rising Temperature 
Total Accepted: 2437 Total Submissions: 8879 My Submissions Question Solution 
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) | Date(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:

1、1399ms

# Write your MySQL query statement below
select w1.Id from Weather w1 join Weather w2 on
date_sub(w1.Date,interval 1 day) = w2.Date 
and w1.Temperature > w2.Temperature;

2、1615ms

# Write your MySQL query statement below
select Id from Weather w1 where Temperature > 
(select Temperature from Weather where Date = date_sub(w1.Date,interval 1 day));



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章