Mysql
 sql >> Datenbank >  >> RDS >> Mysql

So finden Sie den Durchschnitt der Unterschiede der letzten N Messwerte einer Spalte PHP MySQL

SELECT AVG(difference)
FROM (
    SELECT @next_reading - reading AS difference, @next_reading := reading
    FROM (SELECT reading
          FROM level_records
          ORDER BY date DESC
          LIMIT 20) AS recent20
    CROSS JOIN (SELECT @next_reading := NULL) AS var
    ) AS recent_diffs

DEMO