PostgreSQL
 sql >> Database >  >> RDS >> PostgreSQL

Prendi le ultime 3 ore e raggruppa per 5 minuti

Usa generate_series() :

SELECT gs.t, COUNT(t.createdts)
FROM GENERATE_SERIES(now()::date - interval '3 hour', now()::date, interval '5 minute') gs(t)
     mytable t
     ON t.createdts >= gs.t AND
        t.createdts < gs.t + interval '5 minute'
GROUP BY gs.t;