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 3350926
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:50:47+00:00 2026-05-18T01:50:47+00:00

I got a table with 2 columns (both INT), and there are 400 000

  • 0

I got a table with 2 columns (both INT), and there are 400 000 records (a lot).
The first column is random numbers ordered ASC. The second column has a rule on it (which is not important right now)
In the table there are 1000 records, that are exceptions. So, instead of the “rule”, there is only “-1” – valued cells.

How can I delete ~399 000 records, so i want to have in my table left only the ones with -1 and their “neighbors” (the records before and after the ones with -1)

UPDATE
sql server 2k5
first column values – yes unique, but not ID-s (it’s not ++ :D)

example:

before:

 20022518   13
 20022882   364
 20022885   -1
 20022887   5
 20022905   18
 20023200   295
 20023412   212
 20023696   284
 20024112   416
 20025015   903
 20025400   385
 20025401   -1
 20025683   283
 20025981   298
 20025989   8
 20026752   763
 20027779   1027
 20028344   565
 20028350   6
 20028896   546
 20028921   25
 20028924   -1
 20028998   77
 20029031   33
 20029051   20
 20029492   441
 20029530   38
 20029890   360

after:

 20022882   364
 20022885   -1
 20022887   5
 20025400   385
 20025401   -1
 20025683   283
 20028921   25
 20028924   -1
 20028998   77
  • 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-05-18T01:50:48+00:00Added an answer on May 18, 2026 at 1:50 am

    If I understand correctly you want to keep all records with col2 = -1 and the records with the closest col1 to the records with -1. Assuming no duplicates in col1 I would do something like this

    delete from table where not col1 in 
    (
        (select col1 from table where col2 = -1)
    union
        (select (select max(t2.col1) from table t2 where t2.col1 < t1.col1) from table t1 where t1.col2 = -1)
    union
        (select (select min(t4.col1) from table t4 where t4.col1 > t3.col1) from table t3 where t3.col2 = -1)
    )
    

    Edit:
    t4.col1 < t3.col1 should be t4.col1 > t3.col1
    I created a test-table with col1 and col2, both int, col1 is PK, but not autonumber

    SELECT * from adjacent
    

    Gives

    col1    col2
    1   5
    3   4
    4   2
    7   -1
    11  8
    12  2
    

    With the above subselects:

    SELECT * from adjacent
    where
    col1 in 
    (
        (select col1 from adjacent where col2 = -1)
    union
        (select (select max(t2.col1) from adjacent t2 where t2.col1 < t1.col1) from adjacent t1 where t1.col2 = -1)
    union
        (select (select min(t4.col1) from adjacent t4 where t4.col1 > t3.col1) from adjacent t3 where t3.col2 = -1)
    )
    

    gives

    col1    col2
    4   2
    7   -1
    11  8
    

    With the not also

    SELECT * from adjacent
    where
    col1 not in 
    (
        (select col1 from adjacent where col2 = -1)
    union
        (select (select max(t2.col1) from adjacent t2 where t2.col1 < t1.col1) from adjacent t1 where t1.col2 = -1)
    union
        (select (select min(t4.col1) from adjacent t4 where t4.col1 > t3.col1) from adjacent t3 where t3.col2 = -1)
    )
    

    gives

    col1    col2
    1   5
    3   4
    12  2
    

    Finally a delete and select

    delete from adjacent
    where
    col1 not in 
    (
        (select col1 from adjacent where col2 = -1)
    union
        (select (select max(t2.col1) from adjacent t2 where t2.col1 < t1.col1) from adjacent t1 where t1.col2 = -1)
    union
        (select (select min(t4.col1) from adjacent t4 where t4.col1 > t3.col1) from adjacent t3 where t3.col2 = -1)
    )
    
    select * from adjacent
    

    gives

    col1    col2
    4   2
    7   -1
    11  8
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a column in a table (eg. UserName) which I want to make
I got a table with a custom TableCellEditor (extending DefaultCellEditor) with a JFormattedTextField as
If I've got a table containing Field1 and Field2 can I generate a new
I've got a table of URLs and I don't want any duplicate URLs. How
I've got a table that is supposed to track days and costs for shipping
I've got a table of hardware and a table of incidents. Each hardware has
I've got a table with a header, a row with input fields, rows with
I've got a table structure I'm not really certain of how to create the
I've got a table with checkbox-style cells, and I can't figure out how to
I have got a table in MS Access 2007 with 4 fields. Labour Cost

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.