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 |
|
Tekno_Guru
Starting Member
2 Posts |
Posted - 2007-07-25 : 15:44:08
|
| I would like to create a query that shows numbers that are not in a identity column. So if there are 500,000 records you would hope the values in the identity column would be 1 to 500,000. But if some rows have been removed they identity column might be above 500,000. I would like to figure out what values were removed. I have tried the following queries, but am sure I am not close to what I need. Thank you for any assistance provided.select identity_column from example_table where identity_column between (1) and(500000) and identity_column not in( select identity_column from example_table where identity_column between (1) and (500000) )select identity_column from example_table where identity_column between (1) and(500000) and not exists( select identity_column from example_table where identity_column between (1) and (500000) ) |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-07-25 : 15:59:18
|
[code]SELECT n.NumberFROM F_TABLE_NUMBER_RANGE(1, 500000) AS nLEFT JOIN YourTableNameHere AS x ON x.PrimaryColHere = n.NumberWHERE x.PrimaryColHere IS NULL[/code] E 12°55'05.76"N 56°04'39.42" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-07-25 : 16:02:17
|
[code]SELECT s1.RowNum + 1 AS MissingIDFROM @Sample AS s1LEFT JOIN @Sample AS s2 ON s2.RowNum - 1 = s1.RowNumWHERE s2.RowNum IS NULL AND s1.RowNum < (SELECT MAX(RowNum) FROM @Sample)[/code]This will only work if the "gaps" are maximum 1 missing id (3, 4, 5, 6) E 12°55'05.76"N 56°04'39.42" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-07-25 : 16:02:43
|
Moderator, move that post! E 12°55'05.76"N 56°04'39.42" |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2007-07-25 : 16:37:25
|
quote: Originally posted by Peso Moderator, move that post! E 12°55'05.76"N 56°04'39.42"
Not sure if the question is 2000 or 2005, so I chose 2005 for the forum.Tara Kizerhttp://weblogs.sqlteam.com/tarad/ |
 |
|
|
Tekno_Guru
Starting Member
2 Posts |
Posted - 2007-07-27 : 00:02:09
|
| Hello Peso, I tried your second query and it eventually just timed out. The first one calls a function that I don't have on my server/database. Any other ideas? Is my intent clear? Thanks again for your help. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
|
|
|
|
|
|
|