Solved

Dates between date ranges - oracle 10

Posted on 2010-11-20
3
673 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 32

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 74

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

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

Suggested Solutions

Title # Comments Views Activity
SQL Query 34 115
Help on model clause 5 37
oracle DR - data guard failover. 18 44
Help with Oracle IF statment 5 22
Article by: Swadhin
From the Oracle SQL Reference (http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/queries006.htm) we are told that a join is a query that combines rows from two or more tables, views, or materialized views. This article provides a glimps…
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…
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.
This video shows how to recover a database from a user managed backup

765 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