Unix Technical Forum

CONSECUTIVE Values

This is a discussion on CONSECUTIVE Values within the Oracle Database forums, part of the Database Server Software category; --> SQL> select MIN(c_year), MAX(c_year) 2 from T 3 ; MIN(C_YEAR) MAX(C_YEAR) --------------------------- ----------------------------------------------- - 1996 2005 There may be ...


Go Back   Unix Technical Forum > Database Server Software > Oracle Database

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-23-2008, 04:35 AM
Robert C
 
Posts: n/a
Default CONSECUTIVE Values

SQL> select MIN(c_year), MAX(c_year)
2 from T
3 ;

MIN(C_YEAR) MAX(C_YEAR)
--------------------------- -----------------------------------------------
-
1996 2005

There may be gaps in this column value...
How can I ensure to return a result of CONSECUTIVE values...like:

1996
1997
1998
1999
2000
2001
2002
2003
2004
2005

Thanks


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-23-2008, 04:35 AM
Pizza Goldberg
 
Posts: n/a
Default Re: CONSECUTIVE Values


"Robert C" <rchin@panix.com> wrote in message
news:bpdvhc$2fu$1@reader2.panix.com...
> SQL> select MIN(c_year), MAX(c_year)
> 2 from T
> 3 ;
>
> MIN(C_YEAR) MAX(C_YEAR)
> --------------------------- ---------------------------------------------

--
> -
> 1996 2005
>
> There may be gaps in this column value...
> How can I ensure to return a result of CONSECUTIVE values...like:
>
> 1996
> 1997
> 1998
> 1999
> 2000
> 2001
> 2002
> 2003
> 2004
> 2005
>
> Thanks
>
>


1 select 1995 + rn
2 from (select rownum rn from dba_objects)
3* where rn between 1 and 10
sql>/

1995+RN
----------
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-23-2008, 04:35 AM
mcstock
 
Posts: n/a
Default Re: CONSECUTIVE Values

robert,

your post is a little unclear (or maybe my reading of it is)... if there are
gaps in the column values, the literal answer to your questions is you can't
ensure returning a set of consecutive values, because by definition you
don't have one

however, if you're asking how to determine if there are gaps in the sequence
then you can do something like this

select t1.c_year
from t t1, t t2
where t1.c_year + 1 = t2.c_year(+)
and t2.c_year is null
and t1.c_year <> (select max(c_year) from t)

this is basically an outer self join that tries to match each record with
one for the following year -- outer join includes years that do not have a
matching 'next' year, and the IS NULL predicate filters the output to just
those years
the subquery eliminates the max year, as it will never have a 'next' year

this will return a list of years that precede a gap, but won't identify how
big the gap is

--mcs


"Robert C" <rchin@panix.com> wrote in message
news:bpdvhc$2fu$1@reader2.panix.com...
| SQL> select MIN(c_year), MAX(c_year)
| 2 from T
| 3 ;
|
| MIN(C_YEAR) MAX(C_YEAR)
| --------------------------- ---------------------------------------------
--
| -
| 1996 2005
|
| There may be gaps in this column value...
| How can I ensure to return a result of CONSECUTIVE values...like:
|
| 1996
| 1997
| 1998
| 1999
| 2000
| 2001
| 2002
| 2003
| 2004
| 2005
|
| Thanks
|
|


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-23-2008, 04:35 AM
Jonathan Gennick
 
Posts: n/a
Default Re: CONSECUTIVE Values

On Tue, 18 Nov 2003 15:30:36 -0500, "Robert C" <rchin@panix.com>
wrote:

>There may be gaps in this column value...
>How can I ensure to return a result of CONSECUTIVE values...like:


I've written about this problem. Have a look at the following two
articles:

http://otn.oracle.com/oramag/oracle/02-sep/o52sql.html

http://four.pairlist.net/pipermail/o...03/000006.html

The first article should help. The second talks about a new 10g
feature that bears on this problem.

Jonathan Gennick --- Brighten the corner where you are
http://Gennick.com

Join the Oracle-article list and receive one article on Oracle
technologies per month by email. To join, visit
http://four.pairlist.net/mailman/lis...oracle-article, or send
email to Oracle-article-request@gennick.com and include the word
"subscribe" in either the subject or body.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-23-2008, 04:35 AM
Billy Verreynne
 
Posts: n/a
Default Re: CONSECUTIVE Values

"Robert C" <rchin@panix.com> wrote

> SQL> select MIN(c_year), MAX(c_year)
> 2 from T
> 3 ;
>
> MIN(C_YEAR) MAX(C_YEAR)
> --------------------------- -----------------------------------------------
> -
> 1996 2005
>
> There may be gaps in this column value...
> How can I ensure to return a result of CONSECUTIVE values...like:
>
> 1996
> 1997
> 1998
> 1999
> 2000
> 2001
> 2002
> 2003
> 2004
> 2005


You create a lookup dimension/table. You populate it with the serie
you want. You then join from it to the applicable table using an outer
join.

In Data Warehousing you will often find a date dimension/table that
contains a row per day for years X to Z. Or a time dimension that
contains a row per second for a 24h period.

--
Billy
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 10:48 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com