PostgreSQL
 sql >> Base de Dados >  >> RDS >> PostgreSQL

Tire as últimas 3 horas e agrupe por 5 minutos


Use 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;