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

前端 未结 1 1586
孤街浪徒
孤街浪徒 2021-01-22 22:47

I am using Presto and Zeppelin. There are a lot of raw datas. I have to summarize those datas.

I wanna group time every 5 seconds.

serviceType        log         


        
1条回答
  •  爱一瞬间的悲伤
    2021-01-22 23:01

    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)
    

    0 讨论(0)
提交回复
热议问题