Link to home
Start Free TrialLog in
Avatar of Mahmoud Al Jundi
Mahmoud Al JundiFlag for Jordan

asked on

Find missing serials

Hello,

How to find a missing sequence Using oracle 8i from a table ?  the purpose I need to assign available sequences automatically for customer accounts, as below example :

Account_no
1
2
4
5
8

results:
1) The result of the query is 3 , when (3) inserted in the table, query should get 6..etc.
2) Also how can I find all missing sequences , result : 3,6,7 using one select?

Thank You
Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

Customer Accounts, Serial Numbers once assigned have an infinite lifetime. They never can be reused. If you delete them from the current database they should still exist in your data warehouse
ASKER CERTIFIED SOLUTION
Avatar of Mahmoud Al Jundi
Mahmoud Al Jundi
Flag of Jordan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mahmoud Al Jundi

ASKER

thank u
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

If your sample data has a 3, your query returns 7 not 6.

This one only hits the table once:
select min(rn) from (
	select num, row_number() over(order by num) rn from tab1
)
where num!=rn
;

Open in new window


To show all missing numbers, this is what I came up with:
with max_num as (
	select max(num) max_num from tab1
),
all_numbers as (
	select rownum rn from max_num connect by level <= max_num
),
combined as (
	select rn, num
	from all_numbers a left join tab1 t1 on a.rn = t1.num
)
select rn from combined where num is null order by rn
;

Open in new window


There are many other options on the web.