Home » SQL & PL/SQL » SQL & PL/SQL » Generate Sequence and then find missing Sequence no (Oracle 10.2.0.1)
Generate Sequence and then find missing Sequence no [message #614787] Tue, 27 May 2014 06:41 Go to next message
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 Go to previous messageGo to next message
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

Re: Generate Sequence and then find missing Sequence no [message #614794 is a reply to message #614791] Tue, 27 May 2014 07:41 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
Michel Cadot wrote on Tue, 27 May 2014 18:06

connect by level < (select max(start1+total -1) from t)


Just a small tweak at line 11 to get exact output.

EDIT : Just saw, you already updated by removing "=" operator

[Updated on: Tue, 27 May 2014 07:44]

Report message to a moderator

Re: Generate Sequence and then find missing Sequence no [message #614798 is a reply to message #614794] Tue, 27 May 2014 07:47 Go to previous messageGo to next message
Michel Cadot
Messages: 68647
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

No. However with "-1" you will get the correct result because it will remove from "all_numbers" a number that actually is in "t_numbers" (the last one) and so the result does not change.

(There was an error in my first post in "<=" fix to "<" in "all_numbers".)
Re: Generate Sequence and then find missing Sequence no [message #614799 is a reply to message #614794] Tue, 27 May 2014 07:48 Go to previous messageGo to next message
Michel Cadot
Messages: 68647
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Quote:
EDIT : Just saw, you already updated by removing "=" operator


Which is not in your quote. Razz

Re: Generate Sequence and then find missing Sequence no [message #614802 is a reply to message #614799] Tue, 27 May 2014 08:02 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
Michel Cadot wrote on Tue, 27 May 2014 18:18
Which is not in your quote. Razz


Yes, took a minute to understand why it incremented to 1 more till 13 Smile

SQL> SELECT LEVEL num_list
  2  FROM   dual
  3  CONNECT BY LEVEL < (SELECT Max(start1 + total)
  4                      FROM   t)
  5  MINUS
  6  SELECT start1 + LEVEL - 1 myno
  7  FROM   (SELECT start1,
  8                 total,
  9                 start1 + total - 1 endno
 10          FROM   t)
 11  CONNECT BY ( start1 + LEVEL - 1 ) <= endno
 12  /
  NUM_LIST
----------
         1
         2
         3
         7
         8
Re: Generate Sequence and then find missing Sequence no [message #614803 is a reply to message #614799] Tue, 27 May 2014 08:03 Go to previous messageGo to next message
bluetooth420
Messages: 146
Registered: November 2011
Senior Member
cast, multiset, sys.odciNumberList all these things are new to me.

Thanks anyhow for the help and showing me new horizons.
Re: Generate Sequence and then find missing Sequence no [message #614804 is a reply to message #614787] Tue, 27 May 2014 08:05 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
Assuming Oracle version supports recursive subquery factoring:

with r(n,l) as (
                 select  start1 + total - 1 n,
                         total n
                   from  t
                union all
                 select n - 1 n,
                        l - 1 l
                   from  r
                   where l > 1
               )
select  n
  from  r
  order by n
/

         N
----------
         4
         5
         6
         9
        10
        11
        12

7 rows selected.

SQL> 


SY.
Re: Generate Sequence and then find missing Sequence no [message #614805 is a reply to message #614802] Tue, 27 May 2014 08:13 Go to previous messageGo to next message
Michel Cadot
Messages: 68647
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Quote:
  6  SELECT start1 + LEVEL - 1 myno
  7  FROM   (SELECT start1,
  8                 total,
  9                 start1 + total - 1 endno
 10          FROM   t)
 11  CONNECT BY ( start1 + LEVEL - 1 ) <= endno


This row generator does not work when "t" has more than one row.
Just execute it alone and you will see (and this is the reason of the "Distinct" in OP's query).

Re: Generate Sequence and then find missing Sequence no [message #614806 is a reply to message #614803] Tue, 27 May 2014 08:14 Go to previous message
Michel Cadot
Messages: 68647
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
bluetooth420 wrote on Tue, 27 May 2014 15:03
cast, multiset, sys.odciNumberList all these things are new to me.

Thanks anyhow for the help and showing me new horizons.


Have a look at Row generator, there are a couple of examples of their usage.

Previous Topic: Read Binary Format File
Next Topic: Insert into Partition Tables After Column Addition
Goto Forum:
  


Current Time: Fri Apr 26 22:50:16 CDT 2024