Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Count by date range

ysriDec 31 2013 — edited Jan 2 2014

Hi, I have data containing a task name and it's start and end date.

Now, I want new data based on above that will tell how many tasks were running at any given date.

Assuming that time stamp for each date is 00:00:00, and tasks start at Start_Dt but end just before End_dt (lower bound included, upper bound not)

Input:

Task     Start_Dt     End_D

A    2013-12-01    2013-12-05

B    2013-12-03    2013-12-04

C    2013-12-03    2013-12-04

D    2013-12-03    2013-12-06

E    2013-12-04    2013-12-05

F    2013-12-06    2013-12-07

Output:

Start    End      Count(Jobs)
2013-12-012013-12-031(A)
2013-12-032013-12-044(A,B,C,D)
2013-12-042013-12-053(A,D,E)
2013-12-052013-12-061(D)
2013-12-062013-12-071(F)

In the above, Task B should not be in 3rd row since it ended on 12/04 and hence not to be included in the 12/04 - 12/05 range (and B was not running in this period)

alter session set nls_date_format = 'YYYY-MM-DD';

select tmp.* from

(

select 'A' TASK, to_date('2013-12-01','YYYY-MM-DD') START_DT, to_date('2013-12-05','YYYY-MM-DD') END_DT from dual

union

select 'B' TASK, to_date('2013-12-03','YYYY-MM-DD') START_DT, to_date('2013-12-04','YYYY-MM-DD') END_DT from dual

union

select 'C' TASK, to_date('2013-12-03','YYYY-MM-DD') START_DT, to_date('2013-12-04','YYYY-MM-DD') END_DT from dual

union

select 'D' TASK, to_date('2013-12-03','YYYY-MM-DD') START_DT, to_date('2013-12-06','YYYY-MM-DD') END_DT from dual

union

select 'E' TASK, to_date('2013-12-04','YYYY-MM-DD') START_DT, to_date('2013-12-05','YYYY-MM-DD') END_DT from dual

union

select 'F' TASK, to_date('2013-12-06','YYYY-MM-DD') START_DT, to_date('2013-12-07','YYYY-MM-DD') END_DT from dual

)tmp

order by 2,3

;

How to write the sql on the input ('tmp' table)?

Thanks,

-srinivas y.

Message was edited by: ysri

Message was edited by: ysri. Updated tasks and dates.

This post has been answered by Partha Sarathy S on Dec 31 2013
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 30 2014
Added on Dec 31 2013
22 comments
1,049 views