Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
doran_doran
Posting Yak Master
179 Posts |
Posted - 2008-07-16 : 12:30:02
|
How do i determine records from a table "tblCustomers" when I know for fact the id column has the next increment number. for example,id customer 1 abc company2 xyz company4 rrr companyin this scenario i can see i am missing the id 3. I downloaded a table and it has missing records like this and I need to be able to determine those.Thanks |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2008-07-16 : 12:56:36
|
Maybe someone else has a better idea, but it works:-- your downloaded tablecreate table test_it(id int,customer varchar(10))-- your example-datainsert test_it (id,customer)select 1,'abc'union select 2,'xyz'union select 4,'rrr'-- show itselect * from test_it-- table for the needed valuescreate table without_gap(good_id int identity(1,1),something int)-- insert needed valuesinsert without_gap (something)select id from test_it-- find missing idsselect good_id as id_is_missingfrom without_gap wgleft join test_it ti on wg.good_id = ti.idwhere ti.id is nullGreetingsWebfredThere are 10 types of people in the world: Those who understand binary, and those who don't... |
 |
|
doran_doran
Posting Yak Master
179 Posts |
Posted - 2008-07-16 : 13:51:23
|
I will try this. Thanks |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-07-16 : 14:07:10
|
or use not inselect * from without_gap where id not in (select id from with_gap) |
 |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2008-07-16 : 14:41:17
|
Hi visakh16,I've detected that, at latest in SQL Server 2005, the join-method is much faster than the not-in-method.Probably it's depending on how much rows are the tables.In our company, we speak about the "bad not in".GreetingsWebfredThere are 10 types of people in the world: Those who understand binary, and those who don't... |
 |
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2008-07-16 : 15:06:02
|
If you have a Number table you could just join to that and determine which are missing:SELECT Num.NumFROM NumLEFT OUTER JOIN CustomerON Num.Num = Customer.IDWHERE Num.Num <= (SELECT MAX(ID) FROM Customer) |
 |
|
doran_doran
Posting Yak Master
179 Posts |
Posted - 2008-08-06 : 13:39:00
|
Lamprey,I modified your statement a little bit and now it seems to be working.SELECT t.idFROM test_id AS t LEFT JOIN slip ON t.id = slip.IDWHERE (((t.id)<=(SELECT MAX(ID) FROM slip)) AND ((slip.id) Is Null));ThanksMoe |
 |
|
|
|
|
|
|