?
Solved

list of days between dates - oracle sql

Posted on 2010-11-19
5
Medium Priority
?
979 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 74

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 74

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 74

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 74

Accepted Solution

by:
sdstuber earned 2000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
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.
Suggested Courses

809 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