sql >> Base de Datos >  >> RDS >> Mysql

Cómo seleccionar el último registro de cada hora en un día

SELECT 
   HOUR(a.reading_on) As hr, max(a.id),a.reading_on
FROM 
   InverterReadings a
LEFT JOIN
   InverterReadings b
ON
        YEAR(a.reading_on)=YEAR(b.reading_on)
        AND MONTH(a.reading_on)=MONTH(b.reading_on)
        AND day(a.reading_on)=day(b.reading_on)
        AND hour(a.reading_on)=hour(b.reading_on)
AND 
    a.reading_on < b.reading_on
WHERE 
    b.reading_on is null
group by a.reading_on;

Demostración:http://sqlfiddle.com/#!2/49a69/14