Sử dụng 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;