197. Rising Temperature


Posted by ikl258794613 on 2024-02-22

Column Name Type
id int
recordDate date
temperature int

id is the column with unique values for this table.
There are no different rows with the same recordDate.
This table contains information about the temperature on a certain day.

Write a solution to find all dates' Id with higher temperatures compared to its previous dates (yesterday).

Return the result table in any order.

The result format is in the following example.

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).

解答:

SELECT w1.id FROM Weather w1, Weather w2 
WHERE DATEDIFF(w1.recordDate,w2.recordDate) = 1 
AND w1.temperature > w2.temperature

學習點:
1.自己跟自己比較時SELECT自己兩次。
2.DATEDIFF ( datepart , startdate , enddate ) datepart不傳時返回兩個日期之間的差異。


#SQL







Related Posts

AI輔導室|製作飄逸緞帶

AI輔導室|製作飄逸緞帶

Day 14-higher-lower game

Day 14-higher-lower game

[Day 02] - Vault 的啟動及 Unseal 概念

[Day 02] - Vault 的啟動及 Unseal 概念


Comments