Mysql
 sql >> Database >  >> RDS >> Mysql

Come selezionare l'ultimo record di ogni ora in un giorno

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;

Demo:http://sqlfiddle.com/#!2/49a69/14