sql - Row aggregation by time based distance -
cheers, working on postgres table
create table my_table ( "id" serial, "sensorid" integer, "actorid" integer, "timestamp" timestamp without time zone, )
with example data
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"
i want group records following criteria
- they have same sensorid
- they have same actorid
- (the problem:) timespan between each of them less (say) 5 minutes. is, there might group spans on more hour there never more 5 minutes between 2 records in group. timespan may aggregates average.
- additionally, number of aggregated records each group must given, since large groups must identified.
so, output should like
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;
thanks help! dennis
first, want use lag()
determine previous time , whether starts new period. each sensorid/actorid combination, can cumulative sum of isstart
identify groups each pair.
then aggregation including new group in result:
select sensorid, actorid, min(timestamp), max(timestamp), count(*) numingroup (select t.*, sum(isstart) on (partition sensorid, actorid order timestamp) grp (select t.*, (case when prevts null or prevts < timestamp - interval '5 minutes' 1 else 0 end) isstart (select t.*, lag(timestamp) on (partition sensorid, actorid order timestamp) prevts my_table t ) t ) t ) t group sensorid, actorid, grp
Comments
Post a Comment