Solved

Dates between date ranges - oracle 10

Posted on 2010-11-20
3
671 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 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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.Net - CSV to Oracle table 4 71
Migrate Oracle Database from ASM to Non-ASM on a Windows server. 1 45
Queries 15 41
Oracle function to insert records? 15 42
Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…

831 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