编写一个SQL查询来查找所有日期的id,与它之前的日期(昨天)相比具有更高的温度。
参考:https://leetcode.com/problems/rising-temperature/
下面是我的代码:
SELECT L.id AS id
FROM weather AS p
INNER JOIN weather AS L ON p.recordDate = L.recorddate - 1
WHERE p.temperature < l.temperature我不知道我的答案出了什么问题,有人能帮我吗?
发布于 2021-03-13 17:30:47
注意sql的dateadd函数。如果L.recorddate是正确的日期或日期时间,则在进行比较时需要使用sql的DateAdd函数,而不是像p.recordDate=dateadd( L.recorddate,-1,l.recorddate)那样使用天-1。
发布于 2021-03-13 17:34:50
LAG函数为您完成了这项工作:
WITH a AS(
SELECT *, LAG(temperature) OVER(ORDER BY recordDate) PrevTmp FROM weather
)
SELECT * FROM a
WHERE a.PrevTmp<temperature发布于 2021-03-13 17:40:14
使用Lag()
select id from (
select id, temperature, Lag(temperature,1) over (order by recorddate) lastTemp
from weather
)w
where temperature>LastTemphttps://stackoverflow.com/questions/66611699
复制相似问题