Newsgroups: comp.databases.oracle.misc
From: "Charles Hooper" <hooperc2...@yahoo.com>
Date: 21 Oct 2006 06:09:17 -0700
Local: Sat, Oct 21 2006 9:09 am
Subject: Re: Reduce To Ranges aggregate function
Charles Hooper wrote: Borrowing Michel's excellent suggestion to use SYS_CONNECT_BY_PATH, > Michel Cadot wrote: > > "Mike King" <emai...@excite.com> a écrit dans le message de news: 12jfcsjsafvm...@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; > > 8 rows selected. > > SQL> col Range format a20 > > 3 rows selected. > > Regards > I guess that I have a bit of difficulty understanding what the OP was > If we can make the assumption that the OP was looking for a way to > CREATE TABLE T (ID NUMBER(22)); > SELECT > RANGE > SELECT > MAX(DECODE(ROWNUM,1,RANGE,''))||MAX(DECODE(ROWNUM,2,','||RANGE,''))||MAX(DE CODE(ROWNUM,3,','||RANGE,''))||MAX(DECODE(ROWNUM,4,','||RANGE,''))||MAX(DEC ODE(ROWNUM,5,','||RANGE,''))||MAX(DECODE(ROWNUM,6,','||RANGE,'')) > REDUCED_FORM > Looking at the OP's suggested SQL statement: > SELECT > ID RANGE > INSERT INTO T VALUES (100); > SELECT > RANGE > Charles Hooper 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 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.
| ||||||||||||||