I have a table that looks like the following:
eventId activity timestamp
1 0 2012-10-22 20:10:00
2 0 2012-10-22 20:10:20
3 1 2012-10-22 20:11:25
4 1 2012-10-22 20:12:20
5 1 2012-10-22 20:12:22
6 0 2012-10-22 20:12:30 <--
7 1 2012-10-22 20:12:25 <--
8 0 2012-10-22 20:14:46
9 0 2012-10-22 20:14:48
10 1 2012-10-22 20:15:45
11 0 2012-10-22 20:16:00
12 0 2012-10-22 20:17:00
13 0 2012-10-22 20:17:13
I would like to delete every row that has an activity of 0 and is not chronologically next to a row with activity 1. So from this example I would delete rows with eventID 1, 8, 12, and 13. It is possible for events to be inserted into the table asynchronously, as shown by row 6 and 7.
I know I can do this in a loop by checking each individual row and issuing a query to delete it if it matches my criteria, but that is very inefficient. I was wondering if it is possible to do this all in one query.
It seems like if I could do something similar to
delete from mytable
where activity = 0
and (rownumber()+1).activity = 0
and (rownumber()-1).activity = 0
order by timestamp
this would be simple, but I don’t think such functionality is possible.
Here is how you can select all the records you want to keep:
SQL Fiddle Example
You can then do something like: