Reputation: 360
I've datasets in a table with a timestamp value and an event string. How can I get all events that are min. 5 seconds apart of other events with the same event value.
Data given:
TS EVENT
-------------------------- --------------------------------
2012-03-15 13:09:27.486000 foo
2012-03-15 13:09:37.253000 bar
2012-03-15 13:09:31.243000 foo
2012-03-15 13:09:26.243000 foo
2012-03-15 13:09:47.841000 foo
The result should be
TS EVENT
-------------------------- --------------------------------
2012-03-15 13:09:26.243000 foo
2012-03-15 13:09:37.253000 bar
2012-03-15 13:09:47.841000 foo
Two of the "foo" events are only 3 or 4 seconds before or after another "foo" event and should therefore not be selected. If multiple event exists in a certain range of seconds, only the first one should be returned.
Any hint appreciated! Thanks!
Upvotes: 1
Views: 134
Reputation:
Try:
with cte as
(select event, ts, row_number() over (partition by event order by ts) rn
from myTable)
select t1.event, t1.ts
from cte t1
left join cte t2
on t2.event = t1.event and
t2.rn=t1.rn+1 and
t2.ts <= t1.ts + interval '5' second
where t2.event is null
Upvotes: 2