Postgres group by timestamp into 6 hourly buckets

前端 未结 1 1339
死守一世寂寞
死守一世寂寞 2021-02-07 22:17

I have the following simple table:

ID      TIMESTAMP               VALUE
4   2011-05-27 15:50:04 1253
5   2011-05-27 15:55:02 1304
6   2011-05-27 16:00:02 1322
7         


        
1条回答
  •  长发绾君心
    2021-02-07 23:09

    I think grouping the integer value of the quotient of the (Hour of your timestamp / 6) should help. Try it and see if it helps. Your group by should be something like

    group by year, month, day, trunc(EXTRACT(hour from TIMESTAMP) / 6)
    

    The logic behind this is that when the hour part of the date is divided by 6, the int values can only be

        0 - 0:00 - 5:59:59
        1 - 6:00 - 11:59:59
        2 - 12:00 - 17:59:59
        3 - 18:00 - 23:59:59
    

    Grouping using this should put your data into 4 groups per day, which is what you need.

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