all groups > sql server programming > november 2006 >
You're in the

sql server programming

group:

Selecting missing sequence numbers


Selecting missing sequence numbers Jami
11/26/2006 11:52:33 PM
sql server programming:

Hi!
i am maintaining a distributed database. data comes in shape of batches.
i want to determine which no is missing.
sample data and table steucture are like this

Main_table
s-no batch_no region_id
1 1 AA1
2 1 AA1
3 1 AA1
4 1 AA1
17 4 AA1
18 4 AA1
19 4 AA1
23 5 AA2
35 8 AA2
..
..
..


i have made a table with sequnce no's to compare the batch no sequence.

and i m using following query

select distinct b.region_name,batch_no from main_talbe a join Regions b
on a.Region_id = b.region_id
where a.batch_no in (select seq_no from list)


this query gives me complete list of batches centers by each region. but
i want break in the sequnce batches not sent by the regions. if i use
'not in' it returns me nothing as batch no is in the list as list is
quite long 1..1000. how can i change my query to get the desired result.

thanx
Jami

Re: Selecting missing sequence numbers Uri Dimant
11/27/2006 12:00:00 AM
Jami
CREATE TABLE X (C INT NOT NULL PRIMARY KEY)
insert X select 1 union all
select 3 union all
select 4 union all
select 5 union all
select 7 union all
select 10


SELECT
c+n FROM x, (
SELECT 0 n UNION ALL SELECT 1
) T
GROUP BY c+n
HAVING MIN(n) = 1 and c+n < (select max(c) from x)


[quoted text, click to view]

Re: Selecting missing sequence numbers Uri Dimant
11/27/2006 12:00:00 AM
Hi

T is an alias of derived table which has one column called "n" . SELECT c+n
and this table (group by) makes use list of sequential numbers



[quoted text, click to view]

Re: Selecting missing sequence numbers Arnie Rowland
11/27/2006 1:04:31 AM
You should RIGHT JOIN (or LEFT JOIN) as is appropriate with a Numbers table.
See:

Numbers Table
http://www.sqlservercentral.com/columnists/mcoles/2547.asp


--
Arnie Rowland, Ph.D.
Westwood Consulting, Inc

Most good judgment comes from experience.
Most experience comes from bad judgment.
- Anonymous

You can't help someone get up a hill without getting a little closer to the
top yourself.
- H. Norman Schwarzkopf


[quoted text, click to view]

Re: Selecting missing sequence numbers Jami
11/27/2006 3:20:54 AM


thanx Uri but can u explain it to me a bit as i m a bit confused
especial from that 'n'

Re: Selecting missing sequence numbers Jami
11/27/2006 3:51:45 AM


thanx Uri for ur help

Re: Selecting missing sequence numbers Anith Sen
11/27/2006 8:46:36 AM
See if this helps:
http://groups.google.com/group/microsoft.public.sqlserver.programming/browse_frm/thread/3818cf8298611f47/

--
Anith

AddThis Social Bookmark Button