197.上升的温度


197.上升的温度

表: Weather

+—————+———+
| Column Name | Type |
+—————+———+
| id | int |
| recordDate | date |
| temperature | int |
+—————+———+
id 是该表具有唯一值的列。
没有具有相同 recordDate 的不同行。
该表包含特定日期的温度信息

编写解决方案,找出与之前(昨天的)日期相比温度更高的所有日期的 id

返回结果 无顺序要求

结果格式如下例子所示。

示例 1:

输入:
Weather 表:
+—-+————+————-+
| id | recordDate | Temperature |
+—-+————+————-+
| 1 | 2015-01-01 | 10 |
| 2 | 2015-01-02 | 25 |
| 3 | 2015-01-03 | 20 |
| 4 | 2015-01-04 | 30 |
+—-+————+————-+
输出:
+—-+
| id |
+—-+
| 2 |
| 4 |
+—-+
解释:
2015-01-02 的温度比前一天高(10 -> 25)
2015-01-04 的温度比前一天高(20 -> 30)

题解:

select
    w2.id
from
    Weather as w1, Weather as w2
where
    w1.temperature < w2.temperature
and
    -- datediff:计算两个日期的差值
    datediff(w2.recordDate, w1.recordDate) = 1

文章作者: Feliks
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Feliks !
评论
  目录