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

思路:SELF JOIN 表,然后设置条件查询,语法:

TO_DAYS(wt1.DATE) return the number of days between from year 0 to date DATE

SELECT w1.ID AS ID
FROM Weather AS w1, Weather AS w2
WHERE (w1.Temperature > w2.Temperature) 
AND (TO_DAYS(w1.RecordDate)-TO_DAYS(w2.RecordDate)=1)

猜你喜欢

转载自blog.csdn.net/Scarlett_Guan/article/details/82946587