Mysql
 sql >> Base de données >  >> RDS >> Mysql

Comment sélectionner le dernier enregistrement de chaque heure d'une journée

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;

Démo :http://sqlfiddle.com/#!2/49a69/14