197. 上升的温度
给定一个 Weather 表,编写一个 SQL 查询,来查找与之前(昨天的)日期相比温度更高的所有日期的 Id。
例如,根据上述给定的 Weather 表格,返回如下 Id:
解答:
DATEDIFF 函数,可以计算两者的日期差
DATEDIFF(‘2007-12-31’,‘2007-12-30’); # 1
DATEDIFF(‘2010-12-30’,‘2010-12-31’); # -1
所以查询的条件有两个:
与之前的日期相差为 1
比之前的温度高
select b.Id from Weather as a, Weather as b
where a.Temperature<b.Temperature and Datediff(a.RecordDate,b.RecordDate)=-1;