Solved

list of days between dates - oracle sql

Posted on 2010-11-19
5
951 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 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Working with Network Access Control Lists in Oracle 11g (part 1) Part 2: http://www.e-e.com/A_9074.html So, you upgraded to a shiny new 11g database and all of a sudden every program that used UTL_MAIL, UTL_SMTP, UTL_TCP, UTL_HTTP or any oth…
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…

749 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