Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Reduce To Ranges aggregate function
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Michel Cadot  
View profile  
 More options Oct 19 2006, 2:29 pm
Newsgroups: comp.databases.oracle.misc
From: "Michel Cadot" <micadot{at}altern{dot}org>
Date: Thu, 19 Oct 2006 20:29:09 +0200
Local: Thurs, Oct 19 2006 2:29 pm
Subject: Re: Reduce To Ranges aggregate function

"Mike King" <emai...@excite.com> a écrit dans le message de news: 12jfg0la73vv...@corp.supernews.com...
|> SQL> col Range format a20
| > SQL> with
| >  2    step1 as (
| >  3      select id,
| >  4             case
| >  5             when nvl(lag(id) over (order by id),-1) != id-1 then id
| >  6             end grp
| >  7      from t),
| >  8    step2 as (
| >  9      select id,
| > 10             max(grp) over (order by id) grp
| > 11      from step1
| > 12    )
| > 13  select min(id)||decode(min(id),max(id),'','-'||max(id)) "Range"
| > 14  from step2
| > 15  group by grp
| > 16  /
| > Range
| > --------------------
| > 1-5
| > 11-12
| > 45
| >
| > 3 rows selected.
| >
| > Regards
| > Michel Cadot
|
|
| I agree with Frank.  Wow!  Thanks!
|
|

One step further ;-)

SQL> with
  2    step1 as (
  3      select id,
  4             case
  5             when nvl(lag(id) over (order by id),-1) != id-1 then id
  6             end grp
  7      from t),
  8    step2 as (
  9      select id,
 10             max(grp) over (order by id) grp
 11      from step1
 12    ),
 13    step3 as (
 14      select min(id)||decode(min(id),max(id),'','-'||max(id)) rg,
 15             row_number () over (order by min(id)) rn
 16      from step2
 17      group by grp
 18    )
 19  select max(substr(sys_connect_by_path(rg,','),2)) "Reduced"
 20  from step3
 21  connect by prior rn = rn-1
 22  start with rn-1 = 0
 23  /
Reduced
--------------------
1-5,11-12,45

Regards
Michel Cadot


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.