Account Options

  1. Sign in
Google Groups Home
« Groups Home
Reduce To Ranges aggregate function
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  11 messages - Collapse all  -  Translate all to Translated (View all originals)
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
 
Mike King  
View profile  
 More options Oct 19 2006, 1:18 pm
Newsgroups: comp.databases.oracle.misc
From: "Mike King" <emai...@excite.com>
Date: Thu, 19 Oct 2006 13:18:25 -0400
Local: Thurs, Oct 19 2006 1:18 pm
Subject: Reduce To Ranges aggregate function
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


 
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.
Michel Cadot  
View profile  
 More options Oct 19 2006, 1:53 pm
Newsgroups: comp.databases.oracle.misc
From: "Michel Cadot" <micadot{at}altern{dot}org>
Date: Thu, 19 Oct 2006 19:53:22 +0200
Local: Thurs, Oct 19 2006 1:53 pm
Subject: Re: Reduce To Ranges aggregate function

"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;
        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


 
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.
Frank van Bortel  
View profile  
 More options Oct 19 2006, 2:11 pm
Newsgroups: comp.databases.oracle.misc
From: Frank van Bortel <frank.van.bor...@gmail.com>
Date: Thu, 19 Oct 2006 20:11:01 +0200
Local: Thurs, Oct 19 2006 2:11 pm
Subject: Re: Reduce To Ranges aggregate function
Michel Cadot schreef:

Love it! Beautiful!

--
Regards,
Frank van Bortel

Top-posting is one way to shut me up...


 
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.
Mike King  
View profile  
 More options Oct 19 2006, 2:11 pm
Newsgroups: comp.databases.oracle.misc
From: "Mike King" <emai...@excite.com>
Date: Thu, 19 Oct 2006 14:11:44 -0400
Local: Thurs, Oct 19 2006 2:11 pm
Subject: Re: Reduce To Ranges aggregate function

I agree with Frank.  Wow!  Thanks!

 
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.
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.
Frank van Bortel  
View profile  
 More options Oct 19 2006, 2:56 pm
Newsgroups: comp.databases.oracle.misc
From: Frank van Bortel <frank.van.bor...@gmail.com>
Date: Thu, 19 Oct 2006 20:56:09 +0200
Local: Thurs, Oct 19 2006 2:56 pm
Subject: Re: Reduce To Ranges aggregate function
Michel Cadot schreef:

> One step further ;-)

You get a kick out of this, don't you?
I'm flabbergasted. (Nice word, huh?)
--
Regards,
Frank van Bortel

Top-posting is one way to shut me up...


 
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.
Michel Cadot  
View profile  
 More options Oct 19 2006, 3:09 pm
Newsgroups: comp.databases.oracle.misc
From: "Michel Cadot" <micadot{at}altern{dot}org>
Date: Thu, 19 Oct 2006 21:09:37 +0200
Local: Thurs, Oct 19 2006 3:09 pm
Subject: Re: Reduce To Ranges aggregate function

"Frank van Bortel" <frank.van.bor...@gmail.com> a écrit dans le message de news: eh8hc5$cc...@news3.zwoll1.ov.home.nl...
| Michel Cadot schreef:
| >
| > One step further ;-)
|
| You get a kick out of this, don't you?

Sure, I do.

| I'm flabbergasted. (Nice word, huh?)

Nice and hopefully I have my Harrap's at hand!

| --
| Regards,
| Frank van Bortel
|
| Top-posting is one way to shut me up...

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.
Charles Hooper  
View profile  
 More options Oct 21 2006, 8:53 am
Newsgroups: comp.databases.oracle.misc
From: "Charles Hooper" <hooperc2...@yahoo.com>
Date: 21 Oct 2006 05:53:18 -0700
Local: Sat, Oct 21 2006 8:53 am
Subject: Re: Reduce To Ranges aggregate function

I guess that I have a bit of difficulty understanding what the OP was
wanting.  That is an interesting solution that you posted Michel.  I am
not sure that I would have tried to use analytical functions to solve
the problem, but your example is clever.

If we can make the assumption that the OP was looking for a way to
group the numbers 1-10, 11-20, 21-30, 31-40, 41-50, a much simplier
approach is as follows:

CREATE TABLE T (ID NUMBER(22));
INSERT INTO T VALUES (1);
INSERT INTO T VALUES (2);
INSERT INTO T VALUES (3);
INSERT INTO T VALUES (4);
INSERT INTO T VALUES (5);
INSERT INTO T VALUES (11);
INSERT INTO T VALUES (12);
INSERT INTO T VALUES (45);

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);

RANGE
======
1-5
11-12
45

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
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)) T;

REDUCED_FORM
============
1-5,11-12,45

Looking at the OP's suggested SQL statement:

> | select reduced_to_ranges(id)
> | from some_large_table
> | where some_field = some_value

SELECT
  ID,
  TRUNC((ID-1)/10) RANGE
FROM
  T;

        ID      RANGE
========== ==========
         1          0
         2          0
         3          0
         4          0
         5          0
        11          1
        12          1
        45          4

INSERT INTO T VALUES (100);

SELECT
  TRUNC((ID-1)/10) RANGE
FROM
  T
WHERE
  ID=100;

RANGE
==========
 9

Charles Hooper
PC Support Specialist
K&M Machine-Fabricating, Inc.


 
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.
Charles Hooper  
View profile  
 More options Oct 21 2006, 9:09 am
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

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
============
1-5,11-12,45,100

Charles Hooper
PC Support Specialist
K&M Machine-Fabricating, Inc.


 
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.
Dieter Noeth  
View profile  
 More options Oct 21 2006, 12:19 pm
Newsgroups: comp.databases.oracle.misc
From: Dieter Noeth <dno...@gmx.de>
Date: Sat, 21 Oct 2006 18:19:35 +0200
Local: Sat, Oct 21 2006 12:19 pm
Subject: Re: Reduce To Ranges aggregate function

Mike King wrote:
>  ID
> ----
> 1
> 2
> 3
> 4
> 5
> 11
> 12
> 45

> Reduced form: 1-5,11-12,45

Consecutive values are easy to find using OLAP-functions:

select
   min(id), max(id)
from
  (
   select
     id,
     id - row_number() over (order by id) grp
   from t
  ) dt
group by grp

If id is not unique use dense_rank instead of row_number...

Dieter


 
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.
Michel Cadot  
View profile  
 More options Oct 21 2006, 12:44 pm
Newsgroups: comp.databases.oracle.misc
From: "Michel Cadot" <micadot{at}altern{dot}org>
Date: Sat, 21 Oct 2006 18:44:19 +0200
Local: Sat, Oct 21 2006 12:44 pm
Subject: Re: Reduce To Ranges aggregate function

"Dieter Noeth" <dno...@gmx.de> a écrit dans le message de news: ehdhat$sc...@online.de...

| Mike King wrote:

|
| >  ID
| > ----
| > 1
| > 2
| > 3
| > 4
| > 5
| > 11
| > 12
| > 45
| >
| > Reduced form: 1-5,11-12,45
|
| Consecutive values are easy to find using OLAP-functions:
|
| select
|   min(id), max(id)
| from
|  (
|   select
|     id,
|     id - row_number() over (order by id) grp
|   from t
|  ) dt
| group by grp
|
| If id is not unique use dense_rank instead of row_number...
|
| Dieter

Neat solution!

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.
End of messages
« Back to Discussions « Newer topic     Older topic »