Home > Ask the Oracle Experts > SQL Questions & Answers > The integers table
Ask The Oracle Expert: Questions & Answers
EMAIL THIS

The integers table

Rudy Limeback EXPERT RESPONSE FROM: Rudy Limeback

Pose a Question
Other Oracle Categories
Meet all Oracle Experts
Become an Expert for this site


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


>
QUESTION POSED ON: 28 January 2004
I want to write one query to get serial dates, like 1/1/2004, 1/2/2004... I know repeating "select dateadd(d,n,'1/1/2004')" can get the result. But how about a simple method?

>
EXPERT RESPONSE

My favourite solution for this type of problem uses an integers table. Here's the code to create it:

create table integers (i integer)
insert into integers (i) values (0)
insert into integers (i) values (1)
insert into integers (i) values (2)
insert into integers (i) values (3)
insert into integers (i) values (4)
insert into integers (i) values (5)
insert into integers (i) values (6)
insert into integers (i) values (7)
insert into integers (i) values (8)
insert into integers (i) values (9)

Those 10 rows are sufficient to create, with one or more cross joins, whatever range of integers you need. For example, you could generate the numbers 0 through 999 with a triple cross join like this:

select 100*hundreds.i + 10*tens.i + units.i as iii 
  from integers hundreds
     , integers tens
     , integers units

Why iii? Because that reminds me that it's a number up to 999. Note that the query uses table list syntax without join conditions, thus joining every row from all three "copies" of the integers table with every other row. This table list syntax for a cross join will work in any database. In effect, it is the same query as the following one using explicit JOIN syntax:

select 100*hundreds.i + 10*tens.i + units.i as iii 
  from integers hundreds
cross
  join integers tens
cross
  join integers units

The words hundreds, tens, and units are table aliases, required to distinguish the rows from the three different "copies" of the table that are present in the query. Usually I write h, t, and u, to make typing easier. Even more convenient is a view for this query:

create view integers999 (iii)
as
select 100*h.i+10*t.i+u.i 
  from integers h
cross
  join integers t
cross
  join integers u

Now you can use the iii column in an expression to produce all the dates for this year, using a WHERE clause to restrict the upper end of the range:

select dateadd(d,iii,'2004-01-01') as thedate
  from integers999
 where iii <= 365

Simple, eh? Note that there are 366 days in 2004, so '2004-01-01' + 0 days will be the first date, and '2004-01-01' + 365 days will be the last.


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


RELATED CONTENT
SQL
IN list or series of OR conditions?
Connecting tables in a database
SQL query for co-authored books
Querying complex derived tables
SQL string functions
Changing a NULL column to NOT NULL
SQL for hourly totals for the last 48 hours
LEFT OUTER JOIN to a MIN/MAX row
Normalizing a crosstab table
Querying metadata and data at the same time

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary



Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice

HomeNewsTopicsTipsAsk the ExpertsWebcastsWhite PapersProductsBlogs
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Reprints  |  Site Map




All Rights Reserved, Copyright 2003 - 2008, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts