GROUP BY时间戳记间隔10分钟PostgreSQL [关闭]


10

我需要按一段时间划分一组,每组划分10分钟。

我在下面的查询中返回一个结果集:

SELECT timestamp
    FROM table_1, table_2_shape_polygon
    WHERE ST_Within(earthnetworks_geometry_raios, poligono_geometry)
      AND timestamp 
      BETWEEN '2013-04-07 15:30:00' AND '2013-04-07 16:50:00'
      AND type = 0
      AND id_polygon = 2

我的结果集是这样的:

"2013-04-07 15:30:55"
"2013-04-07 15:32:52"
"2013-04-07 15:32:52"
"2013-04-07 15:34:21"
"2013-04-07 15:39:09"
"2013-04-07 16:24:25"
"2013-04-07 16:29:58"
"2013-04-07 16:33:22"
"2013-04-07 16:34:30"
"2013-04-07 16:35:09"
"2013-04-07 16:36:54"
"2013-04-07 16:38:40"
"2013-04-07 16:39:37"
"2013-04-07 16:39:37"
"2013-04-07 16:39:38"
"2013-04-07 16:39:38"
"2013-04-07 16:39:44"
"2013-04-07 16:39:56"
"2013-04-07 16:40:03"
"2013-04-07 16:40:04"
"2013-04-07 16:41:22"
"2013-04-07 16:41:27"
"2013-04-07 16:41:38"
"2013-04-07 16:41:38"
"2013-04-07 16:42:24"
"2013-04-07 16:42:39"
"2013-04-07 16:45:00"
"2013-04-07 16:45:40"
"2013-04-07 16:49:43"

我在一段时间之间拥有所有时间戳,但是我需要每10分钟将其分组一次,但我不知道如何。我尝试date_trunc了没有我需要的精度。

例如:之间2013-04-07 15:30:00, 2013-04-07 15:40:00 5 results

我怎样才能做到这一点?


我试过了,它没有按预期工作。

SELECT
    timestamp 'epoch' +
    INTERVAL '1 second' * round((extract('epoch' from earthnetworks_dt_horario) / 600) * 600) as horario,
    count(earthnetworks_dt_horario)
FROM raios.earthnetworks, raios.earthnetworks_teste_poligono
WHERE ST_Within(earthnetworks_geometry_raios, poligono_geometry)
      AND earthnetworks_dt_horario 
      BETWEEN '2013-04-07 15:30:00' AND '2013-04-07 16:50:00'
      AND earthnetworks_num_tipo = 0
      AND id_poligono = 2
GROUP BY 
round(extract('epoch' from earthnetworks_dt_horario) / 600), earthnetworks_dt_horario

这是未分组的结果集:

"2013-04-07 16:29:58";1
"2013-04-07 16:34:30";1
"2013-04-07 16:33:22";1
"2013-04-07 16:39:44";1
"2013-04-07 16:39:56";1
"2013-04-07 16:42:24";1
"2013-04-07 16:41:38";2
"2013-04-07 16:24:25";1
"2013-04-07 16:39:38";2
"2013-04-07 16:42:39";1
"2013-04-07 16:41:27";1
"2013-04-07 16:36:54";1
"2013-04-07 16:45:00";1
"2013-04-07 16:40:04";1
"2013-04-07 16:40:03";1
"2013-04-07 15:34:21";1
"2013-04-07 15:30:55";1
"2013-04-07 15:39:09";1
"2013-04-07 16:49:43";1
"2013-04-07 16:45:40";1
"2013-04-07 16:35:09";1
"2013-04-07 16:38:40";1
"2013-04-07 16:39:37";2
"2013-04-07 16:41:22";1
"2013-04-07 15:32:52";2

那没起效。

Answers:


14

您可以将其用于PostgreSQL。600是10分钟以秒为单位。这个想法是将时间戳转换为纪元,除以所需的间隔(以分钟为单位),然后取整以获得所需的间隔

SELECT COUNT(*) cnt, 
to_timestamp(floor((extract('epoch' from timestamp_column) / 600 )) * 600) 
AT TIME ZONE 'UTC' as interval_alias
FROM TABLE_NAME GROUP BY interval_alias

2

您可以通过将300替换为600(10分钟内的秒数)来使用此答案


2

有一些方便的日期时间函数。首先,将时间戳解构date_trunc为每个小时,然后对分钟进行整数除法date_part以形成一个间隔,然后将其重新组合。

SELECT COUNT(*), 
    date_trunc('hour', timestamp) +
    (((date_part('minute', ts)::integer / 10::integer) * 10::integer)
     || ' minutes')::interval AS ten_min_timestamp
FROM sometable_or_joins_or_whatever
GROUP BY ten_min_timestamp;

这在所有条件下都适用,必须已被批准。但我不确定为什么|| ' minutes'在中使用过date_part
维沙尔·谢蒂

integer || 'minutes'用于创建间隔类型,例如“ 8分钟”,该间隔类型将添加到时间戳中。
Mike T

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.