Looking to compare today’s temperature to yesterday’s. If today’s is higher than I want to return that row’s id.
Table has 3 columns: id
, RecordDate
, and temperature
.
Code works on small data sets but looking at a data set of 1400 seeing a 1% failure rate.
SELECT DISTINCT
id
FROM
(
SELECT DISTINCT
id,
recordDate,
recordDate - LAG(recordDate, 1) OVER (ORDER BY recordDate) AS diff,
temperature - LAG(temperature, 1) OVER (ORDER BY recordDate) AS dif
FROM
Weather
) AS t
WHERE
dif > 0
AND
diff = 1
ORDER BY
id ASC;
I expect this to sort the data by recordDate
.
There are 2 conditions that must be met for the ID to be captured
- Today’s temperature is greather than yesterday (
dif > 0
) - and yesterday is in fact yesterday – there can only be a 1 day difference between rows (
diff = 1
)
Let me know how you’d improve this! Have been grinding for a while and could use the help. Feel like there might be an easier way to do this even.
2
Answers
I’m not sure about performance, but what do you think of this?