Generate Sequence and then find missing Sequence no [message #614787] |
Tue, 27 May 2014 06:41 |
|
bluetooth420
Messages: 146 Registered: November 2011
|
Senior Member |
|
|
Hi
The sample table and data is as follow
create table t (start1 number(3), total number(3));
insert into t values (4, 3);
insert into t values (9, 4);
MY requirement: 1st generate sequences for EVERY row present in the above table. i.e
row 1 will create sequence 4, 5, 6 (as total is 3)
row 2 will create sequence 9, 10, 11, 12 (as total is 4)
Then i want to know the missing numbers starting from one till maximum number (12 in this case). Hence the required result must be
1,2,3,7,8
I have made the sequence by using the following query but it slows down/doesnot work when more rows are present in the table t.
SELECT Distinct start1+LEVEL-1 myno
FROM (select start1, total, start1+total-1 endno
from t)
CONNECT BY (start1+LEVEL-1) <= endno
order by 1;
Please guide me to redesign my query.
Thanks
|
|
|
Re: Generate Sequence and then find missing Sequence no [message #614791 is a reply to message #614787] |
Tue, 27 May 2014 07:36 |
|
Michel Cadot
Messages: 68647 Registered: March 2007 Location: Nanterre, France, http://...
|
Senior Member Account Moderator |
|
|
SQL> with
2 t_numbers as (
3 select start1+column_value-1 nb
4 from t,
5 table(cast(multiset (select level from dual connect by level <= total)
6 as sys.odciNumberList))
7 ),
8 all_numbers as (
9 select level
10 from dual
11 connect by level < (select max(start1+total) from t)
12 )
13 select * from all_numbers
14 minus
15 select * from t_numbers
16 /
LEVEL
----------
1
2
3
7
8
[Updated on: Tue, 27 May 2014 07:37] Report message to a moderator
|
|
|
|
|
|
|
|
|
|
|