December 5, 2007 at 7:34 am
I store a table that contains a bunch of numbers which are "used IDs". These aren't sequentially used ID values. I'm looking for SQL that will get me the ranges of numbers that are used. For example, let's say my table contains:
100
104
105
106
110
111
115
I'd like the SQL query to return the following records with START and END fields:
100, NULL
104, 106
110, 111
115, NULL
I'm sure this is probably easy but for the life of me it's escaping me. Thanks in advance.
December 6, 2007 at 12:26 am
It's easier to find missing ID's... this test code shows you how... last paragraph of code is the code that does the work... everything else is just test setup...
--===== Setup for speed and to prevent blocking
SET NOCOUNT ON
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
--=============================================================================
-- Create an experimental table to simulate the table being examined
--=============================================================================
--===== If the experimental temp table exists, drop it so we can do repeat runs
-- if we want to "play".
IF OBJECT_ID('TempDB..#yourtable') IS NOT NULL
DROP TABLE #yourtable
--===== Create the experimental temp table and populate with IDs on the fly
SELECT TOP 1000000 ID = IDENTITY(INT, 1, 1)
INTO #yourtable
FROM Master.dbo.SYSCOLUMNS sc1,
dbo.SYSCOLUMNS sc2
--===== Like any good table, our experimental table needs a Primary Key
ALTER TABLE #yourtable
ADD PRIMARY KEY CLUSTERED (ID)
--===== Ok, we have an experimental table with a million IDs from 1 to 1,000,000.
-- Let's remove some of the rows and see if we can find if they're missing
-- This deletes a handful of individual rows and 3 very small ranges
DELETE #yourtable
WHERE ID IN (2,3,250,251,2000,4000,4002,4004,900001,900002,900003,999999)
-- This deletes a "monster" range just to see how it's handled.
DELETE #yourtable
WHERE ID BETWEEN 500000 AND 700000
or (ID BETWEEN 700000 AND 800000
AND ID%2 = 0)
--===== This short little ditty is what does the actual work
SELECT MinRange = (SELECT ISNULL(MAX(suba.ID),0)+1
FROM #yourtable suba
WHERE suba.ID < a.ID),
MaxRange = ID - 1
FROM #yourtable a
WHERE a.ID - 1 NOT IN (SELECT ID FROM #yourtable)
AND a.ID - 1 > 0
--Jeff Moden
Change is inevitable... Change for the better is not.
December 6, 2007 at 6:14 am
Jeff... thanks for the reply. That code works like a champ, but unfortunately I need the IDs that are present, not the ones missing. My ID range will be from 1 to around 4 billion (I kid you not) and the user requirements don't specify how the IDs should be used, so that can be assigned any way the user wants. This is a government project, so that might explain a lot! 😉 Basically the IDs are assigned to four different object types and the IDs can't be reused among them. So if instances of object A are assigned IDs 1, 2, and 3, then instances object B thru D can't use 1, 2, 3. However, they could use ID 4 for an object B, resuming with ID 5 for the next object A. Confusing enough? Yeah, gotta love it. Anyway, I need to be able to show what ID ranges are already used so the users can pick from the unused ones. I'm only storing the used IDs in my table -- don't want to store 4 billion entries if I can help it!
Steve
December 6, 2007 at 2:51 pm
to show what ID ranges are already used so the users can pick from the unused ones.
So, users actually need unused ID's, not used ones?
:hehe:
Why don't you want to display what users actually need?
_____________
Code for TallyGenerator
December 6, 2007 at 2:58 pm
I need to generate a report listing what ID ranges have been used.
December 7, 2007 at 9:45 pm
Then, try this...
--Jeff Moden
Change is inevitable... Change for the better is not.
December 10, 2007 at 6:31 am
Sweet! Thanks, I think that will do the trick.
December 28, 2007 at 2:35 am
Jeff Moden (12/7/2007)
Then, try this...
It would be more readable if the title of the atricle is included in the url replacing hyphen for space instead of removing it
ex
http://sqlblogcasts.com/blogs/madhivanan/archive/2007/12/18/extract-only-numbers-from-a-string.aspxYou can clearly understand what that article is about
If you are moderating this site, suggest this to your admin 🙂
Failing to plan is Planning to fail
December 28, 2007 at 11:43 am
Heh... agreed... but I'm not a moderator... usually, I'm just part of the problem 😛
--Jeff Moden
Change is inevitable... Change for the better is not.
December 28, 2007 at 10:59 pm
Jeff Moden (12/28/2007)
Heh... agreed... but I'm not a moderator... usually, I'm just part of the problem 😛
Well. Shouldnt it be "I'm just part of the problem Solving"? [:)]
In fact you are eligible to be a moderator of the forum where you are highly active 🙂
Failing to plan is Planning to fail
December 29, 2007 at 8:16 am
Thanks for the compliment, but I don't actually have the time to be a moderator... would take away from problem-solving time. 😀
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply