Home LeetCode - 197. Rising Temperature
Post
Cancel

LeetCode - 197. Rising Temperature

197. Rising Temperature - Easy

문제

1
2
3
4
5
6
7
8
9
10
Table: Weather
+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| id            | int     |
| recordDate    | date    |
| temperature   | int     |
+---------------+---------+
id is the primary key for this table.
This table contains information about the temperature on a certain day.

Write an SQL query to find all dates’ Id with higher temperatures compared to its previous dates (yesterday).

Return the result table in any order.

The query result format is in the following example.

입출력 예

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Input: 
Weather table:
+----+------------+-------------+
| id | recordDate | temperature |
+----+------------+-------------+
| 1  | 2015-01-01 | 10          |
| 2  | 2015-01-02 | 25          |
| 3  | 2015-01-03 | 20          |
| 4  | 2015-01-04 | 30          |
+----+------------+-------------+

Output: 
+----+
| id |
+----+
| 2  |
| 4  |
+----+

Explanation: 
In 2015-01-02, the temperature was higher than the previous day (10 -> 25).
In 2015-01-04, the temperature was higher than the previous day (20 -> 30).

풀이

1
2
3
4
5
SELECT next.id
FROM Weather AS prev
  JOIN Weather AS next
    ON (prev.recordDate = SUBDATE(next.recordDate, 1)) AND
       (prev.temperature < next.temperature);
1
2
3
4
SELECT *
FROM Weather AS prev
  JOIN Weather AS next ON DATEDIFF(next.recordDate, prev.recordDate) = 1
WHERE prev.temperature < next.temperature;
This post is licensed under CC BY 4.0 by the author.