--- tags: sql, LeetCode --- # 197. Rising Temperature `Fix Names in a Table` 透過`leetcode 197`[Rising Temperature](https://leetcode.com/problems/rising-temperature/)來練習 #### 使用table ![](https://i.imgur.com/3FCGg5z.png) id 是該表的主鍵。 此表包含有關某一天的溫度的信息。 ## 題目說明: 編寫 SQL 查詢以查找與之前日期(昨天)相比溫度較高的所有日期。order by Id 按任意順序返回結果表。 查詢結果格式位於以下範例中。 ![](https://i.imgur.com/QhjpE7L.png) ## 解題: 以下圖解為第一個例題 使用[datediff](https://www.fooish.com/sql/sql-server-datediff-function.html)來解題 datediff函數可以算出輸入的日期相隔天數 ```sql= select T.id from Weather T, Weather Y where datediff(day, T.recordDate, Y.recordDate) = -1 and T.temperature > Y.temperature; ``` ![](https://i.imgur.com/EQLyrsU.png) 以下圖解為第二個例題(失敗) ```sql= SELECT M.id FROM Table_2 M inner join Table_2 A on cast(convert(varchar(10),A.recordDate,112) as int) + 1 = cast(convert(varchar(10),M.recordDate,112) as int) where M.temperature > A.temperature ``` By. @UEW2WaUHTqSmPOSfnfXrNw