Solved

Dates between date ranges - oracle 10

Posted on 2010-11-20
3
675 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Oracle dataguard 5 61
Error querying database link from Oracle 10g to Postgresql 7 103
SYS password changed. Now can't log in as SYS 27 43
oracle query 3 26
Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
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 set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

726 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