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!

Need a query to report contiguous runs of sequential numbers across rows

445476Sep 5 2011 — edited Sep 5 2011
suppose I have a block of data:
select 0 as n from dual
union all
select 1 as n from dual
union all
select 2 as n from dual
union all
select 3 as n from dual
union all
select 4 as n from dual
union all
select 7 as n from dual
union all
select 8 as n from dual
union all
select 10 as n from dual
union all
select 11 as n from dual
union all
select 12 as n from dual
I'd like a query output like:
first_n, last_n, count
0, 4, 5
7, 8, 2
10, 12, 3

So the first block of 5 numbers: 0,1,2,3,4 is grouped up and min/max/counted (the block has 5 numbers, first is 0 and last is 4 hence result 0,4,5)

Just struggling with the logic I'd need.. Thanks in advance for any pointers

Edited by: charred on Sep 5, 2011 5:35 AM
This post has been answered by Frank Kulash on Sep 5 2011
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 3 2011
Added on Sep 5 2011
7 comments
256 views