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
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
Edit:
t4.col1 < t3.col1should bet4.col1 > t3.col1I created a test-table with col1 and col2, both int, col1 is PK, but not autonumber
Gives
With the above subselects:
gives
With the
notalsogives
Finally a delete and select
gives