how do I group by by hour in postgresql with a “time” field?

前端 未结 2 626
無奈伤痛
無奈伤痛 2021-02-03 18:34

I have a table with a column ctime of type time without time zone.

  |   cdate    |  ctime   
  +------------+----------
  | 2016-12-2         


        
相关标签:
2条回答
  • 2021-02-03 18:59

    use date_trunc:

    group by cdate, date_trunc('hour', ctime)

    0 讨论(0)
  • 2021-02-03 19:06

    I think date_part('hour', ctime) is a better choice if you just want to group based on the hour.

    date_part('hour', timestamp '2001-02-16 20:38:40')    ---> 20
    date_trunc('hour', interval '2 days 3 hours 40 minutes')    ---> 2 days 03:00:00
    
    0 讨论(0)
提交回复
热议问题