Solved

Dates between date ranges - oracle 10

Posted on 2010-11-20
3
668 Views
Last Modified: 2013-12-18
Please see the attached displaying:
fig 1. current output from o_assessments table
fig 2. desired output

the requirements:
the output needs to list all days which the assessments cover, (i.e. the days between the start and end date).

I’m only interested in assessments which cover a set time period (so in this instance just Jan-09), although I’d like to be able to change the to and from dates to cover a wider range of dates if necessary)

If no assessments took place on a certain date I’d still like that date to be listed. (Eg 21-Jan- 24-Jan in this instance)

An assessment needs to be given an order number (asm_ordering) this is ordered by the start date of the assessment (in this instance REF would be 1 as started in December, then CORE (01/01/2009) as 2, IA (03/01/2009) as 3 etc

Any help is appreciated.

example-table-2.xls
0
Comment
Question by:tonMachine100
3 Comments
 
LVL 31

Expert Comment

by:awking00
ID: 34179961
See attached. Note - your example shows 1/16/2009,CORE,A198035,2 twice, which I think is a copy and paste error.

select asm_date

      ,asm_type

      ,asm_id

      ,decode(asm_type,'REF',1

                     ,'CORE',2

                     ,'IA',3

                     ,'SECT47',4) asm_ordering

from

((select trunc(to_date('20081231','yyyymmdd')) + level as asm_date

  from dual

  connect by level <= 31) x

 left join

 o_assessments a

 on x.asm_date >= a.asm_start_date

 and x.asm_date <= a.asm_end_date)

order by x.asm_date,decode(asm_type,'REF',1,'CORE',2,'IA',3,'SECT47',4)

;

Open in new window

0
 
LVL 73

Accepted Solution

by:
sdstuber earned 500 total points
ID: 34180058
try this...

simply change the january 1 date to whatever month you want, it will determine the correct number of days on its own.
The ordering is based on the asm_start_date and will be dynamic as other rows are added.


SELECT asm_date,
       asm_type,
       asm_id,
       CASE
           WHEN asm_type IS NOT NULL
           THEN
               DENSE_RANK() OVER (ORDER BY asm_start_date)
       END
           asm_ordering
  FROM     o_assessments
       RIGHT OUTER JOIN
           (SELECT TO_DATE('2009-01-01', 'yyyy-mm-dd') + LEVEL - 1 asm_date
              FROM DUAL
            CONNECT BY TO_DATE('2009-01-01', 'yyyy-mm-dd') + LEVEL - 1 <=
                           LAST_DAY(TO_DATE('2009-01-01', 'yyyy-mm-dd')))
       ON asm_date BETWEEN asm_start_date AND asm_end_date
ORDER BY asm_date, asm_ordering
0
 
LVL 3

Expert Comment

by:mpaladugu
ID: 34180255
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
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 shows how to recover a database from a user managed backup
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

762 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

21 Experts available now in Live!

Get 1:1 Help Now