Solved

list of days between dates - oracle sql

Posted on 2010-11-19
5
945 Views
Last Modified: 2013-12-18
I have a number of assessments which cover periods of time.

the attachment shows -

fig 1. the current output of o_assessments table
fig 2. desired output.

I need to be able to list all days which the assessments cover, (ie the days between the start and end date). Hopefully my requirments are clear from the sample data.

example-table.xls
0
Comment
Question by:tonMachine100
  • 4
5 Comments
 
LVL 73

Expert Comment

by:sdstuber
ID: 34174531
try this

SELECT     asm_start_date + n asm_date, asm_type, asm_id
      FROM o_assessments,(select level - 1 n from dual connect by level < 100)
      where asm_start_date + n <= asm_end_date
  ORDER BY asm_date, asm_type, asm_id  
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 34174545
adjust the 100 to some number big enough to cover your largest date span (but not grossly over sized)

you could generate with a sub query so it would be an exact fit but that would require an extra hit on your table to generate,  so probably not worth it if you can determine an upper limit on your own.
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 34175323
alternate method,  requires a couple more internal sort operations (use autotrace to confirm) but doesn't require extra IO to the table or need you to guess at a maximum range


alternate method,  requires a couple more sort operations but doesn't require a guess of how many rows you might need


SELECT   asm_start_date + COLUMN_VALUE asm_date, asm_type, asm_id
    FROM (SELECT o_assessments.*,
                 (SELECT     COLLECT(LEVEL - 1)
                        FROM DUAL
                  CONNECT BY asm_start_date + LEVEL - 1 <= asm_end_date)
                     n
            FROM o_assessments) x,
         TABLE(n)
ORDER BY asm_date, asm_type
0
 
LVL 73

Accepted Solution

by:
sdstuber earned 500 total points
ID: 34176455
yet another method, should be equivalent to previous post, but slightly more compact

SELECT   asm_start_date + COLUMN_VALUE asm_date, asm_type, asm_id
    FROM (SELECT *
            FROM o_assessments,
                 TABLE(SELECT     COLLECT(LEVEL - 1)
                             FROM DUAL
                       CONNECT BY asm_start_date + LEVEL - 1 <= asm_end_date))
ORDER BY asm_date, asm_type

0
 

Author Closing Comment

by:tonMachine100
ID: 34179663
This is great thanks.

(I'm just about to ask another question which is related to this one - so if you could check this one out too that would be appreciated)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now