I have posted a similar question some days ago, but unfortunately I misunderstood our customer's statement of the problem I have to solve (most precisely I forgot about a part of it). That's why this post may look as duplicated, but it is not.
This is my original post: Oracle sql: filtering repeated rows that only differ by a tiny amount of time
So lets go again:
I have an Oracle table with event alarms fired by parquimeters. Alarms have a state of Open/Close and when an alarm is Open (PKN_EVENTSTATUS='Open') and Close (PKN_EVENTSTATUS='Close') between a tiny amount of time -RECEIVEDDATE- (let's say for example Open RECEIVEDDATE = x | Close RECEIVEDDATE = x + 30 seconds and -of course- PKN_EVENTNAME is the same) this both event rows (Open/Close) are considered as a "fake" alarm (a "failure alarm" that was fired by the parquimeter by "mistake") so both should be deleted.
I would need to create an Oracle SQL query that will select all that "fake" alarms so I can delete them. Again, alarms that has a tiny difference in time (RECEIVEDDATE) between "Open" and "Close" states.
I started creating a query that might work, but it's extremely slow, so I can not even test it because it takes too long. I'm quite sure it can be optimized, but cannot find how right now so I hope anyone can help me.
My current very slow query:
select t1.ID, t1.PKN_EVENTNAME, t1.PKN_EVENTSTATUS, t1.RECEIVEDDATE
from PARQUIMETERS_ALARMS t1
where
exists
(select 'x'
from PARQUIMETERS_ALARMS t2
where t1.PKN_EVENTNAME = t2.PKN_EVENTNAME
and ((t1.PKN_EVENTSTATUS = 'Open' and t2.PKN_EVENTSTATUS = 'Close'
and abs(t1.RECEIVEDDATE - t2.RECEIVEDDATE) * 24 * 60 * 60 < 30) -- < 30 sec
or (t1.PKN_EVENTSTATUS = 'Close' and t2.PKN_EVENTSTATUS = 'Open'
and abs(t2.RECEIVEDDATE - t1.RECEIVEDDATE) * 24 * 60 * 60 < 30))) -- < 30 sec