1

干杯,在 Postgres 表上工作

CREATE TABLE my_table (
  "id" serial,
  "sensorid" integer,
  "actorid" integer,
  "timestamp" timestamp without time zone,
)

带有示例数据

id, sensorid, actorid, timestamp
1; 2267; 3023; "2013-07-09 12:20:06.446"
2; 2267; 3023; "2013-07-09 12:20:16.421"
3; 2267; 3023; "2013-07-09 12:20:30.661"
4; 2267; 3023; "2013-07-09 12:20:36.958"
5; 2267; 3023; "2013-07-09 12:20:49.508"
6; 2267; 3023; "2013-07-09 12:20:57.683"
7; 3301; 3023; "2013-08-15 06:03:03.428"
8; 2267; 3024; "2013-07-09 12:19:52.196"
9; 2267; 3024; "2013-07-09 12:20:16.515"
10; 2267; 3024; "2013-07-09 12:20:42.341"
11; 2267; 3025; "2013-07-09 12:21:05.98"
12; 2268; 3026; "2013-07-09 12:22:35.03"
13; 2268; 3026; "2013-07-09 12:22:45.066"
14; 3192; 3026; "2013-08-09 07:41:31.206"

我想按照以下条件对记录进行分组

  1. 他们有相同的sensorid
  2. 他们有相同的actorid
  3. (问题:)他们每个人之间的时间跨度小于(比如说)5分钟。也就是说,可能有一个组跨越一个多小时,但组中的两条记录之间的间隔永远不会超过 5 分钟。时间跨度可以是平均的聚合。
  4. 此外,必须给出每个组的聚合记录数,因为必须识别太大的组。

所以,输出应该看起来像

id; sensorid, actorid, avg, count
1; 2267; 3023; "2013-07-09 12:20:30.000"; 7;
2; 3301; 3023; "2013-08-15 06:03:03.428"; 1;
3; 2267; 3024; "2013-07-09 12:20:06.415"; 3;
5; 2267; 3025; "2013-07-09 12:21:05.98"; 1;
6; 2268; 3026; "2013-07-09 12:22:40.626"; 2;
7; 3192; 3026; "2013-08-09 07:41:31.206"; 1;

谢谢你的帮助!丹尼斯

4

1 回答 1

2

首先,您要使用lag()来确定上一个时间是以及它是否开始了一个新的时期。然后对于每个 sensorid/actorid 组合,您可以进行累积总和isStart以识别每对的组。

然后在结果中包含这个新组进行聚合:

select sensorid, actorid, min(timestamp), max(timestamp), count(*) as numInGroup
from (select t.*,
             sum(isStart) over (partition by sensorid, actorid order by timestamp) as grp
      from (select t.*,
                   (case when prevts is null or prevts < timestamp - interval '5 minutes'
                         then 1 else 0
                    end) as isStart
            from (select t.*,
                         lag(timestamp) over (partition by sensorid, actorid
                                              order by timestamp) as prevts
                  from my_table t
                 ) t
           ) t
     ) t
group by sensorid, actorid, grp 
于 2013-08-19T21:06:55.313 回答