How to group time column into 5 second intervals and count rows using Presto?

泄露秘密 提交于 2019-12-02 07:04:08

You can

  1. discard millisecond part of a timestamp with date_trunc
  2. you can round a timestamp without millisecond part to 5 seconds with ts - interval '1' second * (second(ts) % 5)

Example putting this together:

presto> SELECT ts_rounded, count(*)
     -> FROM (
     ->     SELECT date_trunc('second', ts) - interval '1' second * (second(ts) % 5) AS ts_rounded
     ->     FROM (VALUES timestamp '2017-10-24 23:01:20.206',
     ->         timestamp '2017-10-24 23:01:23.206',
     ->         timestamp '2017-10-24 23:01:23.207',
     ->         timestamp '2017-10-24 23:01:26.206') AS t(ts)
     -> )
     -> GROUP BY ts_rounded ORDER BY ts_rounded;
       ts_rounded        | _col1
-------------------------+-------
 2017-10-24 23:01:20.000 |     3
 2017-10-24 23:01:25.000 |     1
(2 rows)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!