Rising Temperature详解程序员

问题描述:

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

代码实现:

select w2.Id  
from Weather w1 ,Weather w2  
where w1.Temperature < w2.Temperature  
and datediff(w2.date,w1.date)=1 

原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/7198.html

(0)
上一篇 2021年7月17日
下一篇 2021年7月17日

相关推荐

发表回复

登录后才能评论