What is the fastest way to truncate timestamps to 5 minutes in Postgres?

后端 未结 3 447
故里飘歌
故里飘歌 2020-12-07 17:40

Postgres can round (truncate) timestamps using the date_trunc function, like this:

date_trunc(\'hour\', val)
date_trunc(\'minute\', val)

I\

3条回答
  •  没有蜡笔的小新
    2020-12-07 18:02

    Full query for those wondering (based on @DNS question):

    Assuming you have orders and you want to count them by slices of 5min and shop_id:

    SELECT date_trunc('hour', created_at) + date_part('minute', created_at)::int / 5 * interval '5 min' AS minute
          , shop_id, count(id) as orders_count
    FROM orders
    GROUP BY 1, shop_id
    ORDER BY 1 ASC
    

提交回复
热议问题