Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8792487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:59:07+00:00 2026-06-13T22:59:07+00:00

I have a table that looks like the following: eventId activity timestamp 1 0

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T22:59:08+00:00Added an answer on June 13, 2026 at 10:59 pm

    Here is how you can select all the records you want to keep:

    select m.eventId
    from (
      select t1.eventId, 
          max(t2.timestamp) as previousTime,
          min(t3.timestamp) as nextTime
      from mytable t1
      left outer join mytable t2 on t1.eventId <> t2.eventId and t2.timestamp < t1.timestamp
      left outer join mytable t3 on t1.eventId <> t3.eventId and t3.timestamp > t1.timestamp
      group by t1.eventId
    ) m
    left outer join mytable tb on m.previousTime = tb.timestamp
    left outer join mytable ta on m.nextTime = ta.timestamp
    where tb.activity = 1
        or ta.activity = 1
    

    SQL Fiddle Example

    You can then do something like:

    delete from mytable
    where mytable.eventId not in ( ... ) <--above query
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table that looks like the following: <table class=theClass> <tr> <td class=anotherClass><strong>Label1:</strong></td>
I have a table in MySQL That looks like the following: date |storenum |views
I have a scenario outline table that looks like the following: Scenario Outline: Verify
I have some data in a table that looks roughly like the following: table
Greetings, I have a table that looks like the following: date value 2007-11-05 134
I have data in a table that looks like the following sample data: varchar(20)
I have a table that looks like the following: id, Name, LinkBackId, Param1, Param2,
I have a table that looks like the following where what should be a
I have a table relationship that looks like the following: barn ------ PK barn_id
Suppose I have a table that looks like the following id | location |

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.