mysql – Get the id where the max(value) was found

Question:

I tried like this, but the correct value is not coming

SELECT id, data, max(valor) 
FROM teste 
WHERE data BETWEEN '2017-03-01 00:00:00' AND '2017-04-01 00:00:00'

The maximum value is perfect, but it takes the first id within that date. How to get the id referring to the max value? If possible the date as well.

Answer:

I think it works that way, but maybe there's a better solution:

SELECT id, data, max(valor) 
FROM teste 
WHERE 
data BETWEEN '2017-03-01 00:00:00' AND '2017-04-01 00:00:00'
AND
valor in (select max(valor) FROM teste)
Scroll to Top
AllEscort