Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
![]() |
![]() |
Home -> Community -> Usenet -> c.d.o.misc -> Re: Reduce To Ranges aggregate function
Charles Hooper wrote:
> Michel Cadot wrote:
> > "Mike King" <emailMK_at_excite.com> a écrit dans le message de news: 12jfcsjsafvmtc1_at_corp.supernews.com...
> > | Has someone ready created an aggregate function that reduces a set of
> > | numbers to a reduced form. I'm writing some middleware software that needs
> > | to know the IDs of some tuples but the query could return tens or thousands
> > | of tuples. I'm thinking if there were an aggregate function that would
> > | reduce the network traffic.
> > |
> > | select reduced_to_ranges(id)
> > | from some_large_table
> > | where some_field = some_value
> > |
> > | ID
> > | ----
> > | 1
> > | 2
> > | 3
> > | 4
> > | 5
> > | 11
> > | 12
> > | 45
> > |
> > | Reduced form: 1-5,11-12,45
> > |
> > |
> >
> > SQL> select id from t order by id;
> > ID
> > ----------
> > 1
> > 2
> > 3
> > 4
> > 5
> > 11
> > 12
> > 45
> >
> > 8 rows selected.
> >
> > 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
>
>
>
>
>
>
>
>
>
>
>
> ID RANGE
> ========== ==========
> 1 0
> 2 0
> 3 0
> 4 0
> 5 0
> 11 1
> 12 1
> 45 4
>
>
>
>
Borrowing Michel's excellent suggestion to use SYS_CONNECT_BY_PATH,
since we do not know how many groups of numbers will be retrieved, my
suggestion SQL statement then looks like this:
SELECT
MAX(SUBSTR(SYS_CONNECT_BY_PATH(RANGE,','),2)) REDUCED_FORM
FROM
(SELECT
RANGE,
ROWNUM POSITION
FROM
(SELECT
MIN(ID)||DECODE(MAX(ID),MIN(ID),'','-'||MAX(ID)) RANGE
FROM
T
GROUP BY
TRUNC((ID-1)/10)
ORDER BY
TRUNC((ID-1)/10)))
CONNECT BY PRIOR
POSITION=POSITION-1
START WITH
POSITION=1;
REDUCED_FORM
Charles Hooper
PC Support Specialist
K&M Machine-Fabricating, Inc.
Received on Sat Oct 21 2006 - 08:09:17 CDT
![]() |
![]() |