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

fenêtre coulissante sql - recherche de la valeur maximale sur l'intervalle

SELECT  *,
        (
        SELECT  SUM(value)
        FROM    mytable mi
        WHERE   mi.tstamp BETWEEN m.tstamp - '5 minute'::INTERVAL AND m.tstamp
        ) AS maxvalue
FROM    mytable m
ORDER BY
        maxvalue DESC
LIMIT   1

Dans PostgreSQL 11 et supérieur :

SELECT  SUM(value) OVER (ORDER BY tstamp RANGE '5 minute' PRECEDING) AS maxvalue,
        *
FROM    mytable m
ORDER BY
        maxvalue DESC
LIMIT   1