简体   繁体   English

如何在postgresql中用“时间”字段按小时分组?

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

I have a table with a column ctime of type time without time zone . 我有一个表格,列time without time zone类型为ctimetime without time zone

  |   cdate    |  ctime   
  +------------+----------
  | 2016-12-24 | 12:02:17
  | 2016-12-24 | 12:02:32
  | 2016-12-24 | 12:03:00
  | 2016-12-24 | 12:02:10

I would like to group by both cdate and ctime but would like for ctime to count only hours. 我想将cdatectime cdate ,但希望ctime只计算几个小时。

使用date_trunc

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

I think date_part('hour', ctime) is a better choice if you just want to group based on the hour. 我认为如果你只想根据小时进行分组date_part('hour', ctime)是一个更好的选择。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM