Try   HackMD

197. Rising Temperature

Fix Names in a Table

透過leetcode 197Rising Temperature來練習

使用table

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

id 是該表的主鍵。
此表包含有關某一天的溫度的信息。

題目說明:

編寫 SQL 查詢以查找與之前日期(昨天)相比溫度較高的所有日期。order by Id

按任意順序返回結果表。

查詢結果格式位於以下範例中。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

解題:

以下圖解為第一個例題

使用datediff來解題
datediff函數可以算出輸入的日期相隔天數

select T.id from Weather T, Weather Y where datediff(day, T.recordDate, Y.recordDate) = -1 and T.temperature > Y.temperature;

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

以下圖解為第二個例題(失敗)

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