Home » SQL & PL/SQL » SQL & PL/SQL » Generate range with given set of list (10.2.0.1.0)
Generate range with given set of list [message #636814] Sun, 03 May 2015 17:10 Go to next message
bluetooth420
Messages: 146
Registered: November 2011
Senior Member
Hi

Here is sample data

create table t (n number(3));

insert into t values (1);
insert into t values (2);
insert into t values (3);
insert into t values (4);
insert into t values (7);

insert into t values (9);
insert into t values (10);
 



I require the following output (In 2 columns)
Start   end
1        4
7        7
9        10



The required output is basically range of the "continuous data sets"

How can i achieve it?

Thanks





Re: Generate range with given set of list [message #636815 is a reply to message #636814] Sun, 03 May 2015 18:35 Go to previous messageGo to next message
Barbara Boehmer
Messages: 9106
Registered: November 2002
Location: California, USA
Senior Member
SCOTT@orcl> SELECT * FROM t ORDER BY n
  2  /

         N
----------
         1
         2
         3
         4
         7
         9
        10

7 rows selected.

SCOTT@orcl> SELECT s.n "Start", e.n "end"
  2  FROM   (SELECT n,
  3  		    ROW_NUMBER () OVER (ORDER BY n) rn
  4  	     FROM   t t1
  5  	     WHERE  NOT EXISTS
  6  		    (SELECT n
  7  		     FROM   t t2
  8  		     WHERE  t1.n = t2.n + 1)) s,
  9  	    (SELECT n,
 10  		    ROW_NUMBER () OVER (ORDER BY n) rn
 11  	     FROM   t t1
 12  	     WHERE  NOT EXISTS
 13  		    (SELECT n
 14  		     FROM   t t2
 15  		     WHERE  t1.n = t2.n - 1)) e
 16  WHERE  s.rn = e.rn
 17  ORDER  BY s.n
 18  /

     Start        end
---------- ----------
         1          4
         7          7
         9         10

3 rows selected.

Re: Generate range with given set of list [message #636816 is a reply to message #636814] Sun, 03 May 2015 18:44 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3312
Registered: January 2010
Location: Connecticut, USA
Senior Member
with a as (
           select  n - dense_rank() over(order by n) grp,
                   n
             from  t
          )
select  min(n) start_n,
        max(n) end_n
  from  a
  group by grp
  order by grp
/

   START_N      END_N
---------- ----------
         1          4
         7          7
         9         10

SQL>


SY.
Re: Generate range with given set of list [message #636821 is a reply to message #636814] Mon, 04 May 2015 00:29 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
I like the Tabibitosan method demonstrated by Aketi Jyuuzou.

SQL> WITH DATA AS(
  2  SELECT n,
  3        n-Row_Number() OVER(ORDER BY n)
  4        AS grp
  5        FROM t
  6        )
  7  SELECT MIN(n),MAX(n)
  8  FROM DATA
  9  GROUP BY grp
 10  ORDER BY MIN(n);

    MIN(N)     MAX(N)
---------- ----------
         1          4
         7          7
         9         10

SQL>



Regards,
Lalit
Re: Generate range with given set of list [message #636823 is a reply to message #636821] Mon, 04 May 2015 01:36 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

What is the difference with Solomon's solution?
Oh yes, you used row_number instead of dense_rank; but if you've thought a little bit before compulsively posting your solution you'd see Solomon's one is better as it takes into account duplicates.

In the end, your post is useful in the way it gives a weaker solution and allows me to explain why it should not be used instead of solomon's one.

SQL> select * from t order by n;
         N
----------
         1
         2
         3
         3
         4
         7
         9
        10

8 rows selected.

SQL> with a as (
  2             select  n - dense_rank() over(order by n) grp,
  3                     n
  4               from  t
  5            )
  6  select  min(n) start_n,
  7          max(n) end_n
  8    from  a
  9    group by grp
 10    order by grp
 11  /
   START_N      END_N
---------- ----------
         1          4
         7          7
         9         10

3 rows selected.

SQL> WITH DATA AS(
  2  SELECT n,
  3        n-Row_Number() OVER(ORDER BY n)
  4        AS grp
  5        FROM t
  6        )
  7  SELECT MIN(n),MAX(n)
  8  FROM DATA
  9  GROUP BY grp
 10  ORDER BY MIN(n);
    MIN(N)     MAX(N)
---------- ----------
         1          3
         3          4
         7          7
         9         10

4 rows selected.

Re: Generate range with given set of list [message #636825 is a reply to message #636821] Mon, 04 May 2015 01:40 Go to previous messageGo to next message
bluetooth420
Messages: 146
Registered: November 2011
Senior Member
Thanks all,

I wish i have such knowledge.

I started to work on excel but could not have have the concept of "group by ?" in order to pick min and max value per group of the scattered data.

God bless you all
Re: Generate range with given set of list [message #636828 is a reply to message #636823] Mon, 04 May 2015 02:45 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
Michel Cadot wrote on Mon, 04 May 2015 12:06
but if you've thought a little bit before compulsively posting your solution you'd see Solomon's one is better as it takes into account duplicates.


I very well knew the fact about the duplicates and I saw Solomon's answer using DENSE_RANK before posting my reply. I posted a link to the OTN thread which has various examples with detailed explanation. A fancy term to easily remember the method and usage.

Quote:
In the end, your post is useful in the way it gives a weaker solution and allows me to explain why it should not be used instead of solomon's one.


Well, having duplicates might not always be true. In the end, your explanation is worth a read. Thanks.
Re: Generate range with given set of list [message #636829 is a reply to message #636828] Mon, 04 May 2015 02:59 Go to previous message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Quote:
having duplicates might not always be true.


So Solomon's solution is the right one without having to think if there are or are not duplicates. Smile

Previous Topic: How to convert a table of CLOBS to VARCHAR & How to come up with a magic number for any table that returns more than 32KB & ... ? (merged 3)
Next Topic: PIVOTING: one row to multiple columns
Goto Forum:
  


Current Time: Fri Aug 07 06:04:56 CDT 2026