按间隔时间分组-PostgreSQL

按间隔时间分组-PostgreSQL,postgresql,group-by,timestamp,Postgresql,Group By,Timestamp,我需要在PostgreSQL中对在不到40分钟的时间间隔内重复的记录进行计数,如下例所示: Table 2019-01-27 09:55:18 2019-01-27 10:03:20 2019-01-27 10:12:14 2019-01-27 11:05:37 2019-01-27 11:27:52 2019-01-27 13:35:28 2019-01-27 15:36:41 Result Count 3 2 1 1 您可以尝试以下方法: select round(extra

我需要在PostgreSQL中对在不到40分钟的时间间隔内重复的记录进行计数,如下例所示:

Table

2019-01-27 09:55:18
2019-01-27 10:03:20
2019-01-27 10:12:14
2019-01-27 11:05:37
2019-01-27 11:27:52
2019-01-27 13:35:28
2019-01-27 15:36:41

Result

Count
3
2
1
1
您可以尝试以下方法:

select 
   round(extract(epoch from timestamp ts_column)/(60*40)),
   count(1)
  from Table
 group by round(extract(epoch from timestamp ts_column)/(60*40))
您可以尝试以下方法:

select 
   round(extract(epoch from timestamp ts_column)/(60*40)),
   count(1)
  from Table
 group by round(extract(epoch from timestamp ts_column)/(60*40))