Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

finding the gaps in between the sequences

user8858890Nov 12 2014 — edited Nov 12 2014

Hello All,

I have a query with finding the gap in between the sequences.

Below is the structure of my table

user_number    | seq_number

1            |   101

1            |      102

1            |    109

1            |    110

1            |    111

2            |    201

2            |    202

2            |    203

2            |    207

2            |    208

.            |    .

.            |    .

.            |    .

.            |    .

Now I have to find the gap in between the sequences, what I mean by gap is for user_number '1' there is gap of 6 between seq_number '102' and '109'.

Below is the query which I am trying to write, but not giving the exact answer, it always gives the gap of 2:

select gap_start - gap_end gap

from(

select a.seq_number+1 as gap_start, b.seq_number-1 as gap_end

from my_table a, my_table b

where a.seq_number = b.seq_number

and a.seq_number - (select c.seq_number

from my_table c

where c.seq_number = a.seq_number-1) is null

and a.user_number = '1'

order by a.seq_number

);

Thanks.

This post has been answered by Frank Kulash on Nov 12 2014
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 10 2014
Added on Nov 12 2014
8 comments
2,468 views