I have a table with multi-year data of temperature measurement.
Looking for the highest measured temperatures including the date of recording from the current year only.
Tried following:
SELECT timestamp, temperature AS max_temp
FROM table
WHERE
temperature = (SELECT max(temperature) FROM table
WHERE timestamp >= '2023-01-01');
But now records from complete table are returned with higest measured temperature in 2023.
Who has the right way to go?
3
Answers
Solved:
SELECT timestamp, temperature AS max_temp FROM table WHERE timestamp >= '2023-01-01' AND temperature = (SELECT max(temperature) FROM table WHERE timestamp >= '2023-01-01');
You can use Year() function to extract the year portion of the timestamp:
So, the modified query would be :
SELECT timestamp, temperature AS max_temp
FROM table
WHERE
temperature = (SELECT max(temperature) FROM table
WHERE Year(timestamp) = YEAR(CURDATE()));
Hope it works!
this may solve your problem: