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!

Calculating days between overlapping period

dianapJul 27 2016 — edited Jul 29 2016

All,

I have a table with periods and a value for an ID.

An ID can have multiple periods. For example:

IDbegin_dateend_datevalue
101-Jul-1614-Jul-1640
115-Jul-1631-jul-1650
212-aug-1623-aug-1640
301-jul-1631-jul-1690

For a given date range, I want to calculate the date difference between the periods and the given date range for the ID.

Say for example date range is 01-jul-16 untill 29-jul-16

Then the given output should be:

IDbegin_dateend_datevaluedate_diff
101-Jul-1614-Jul-164014
115-Jul-1631-jul-165015
212-aug-1623-aug-16400
301-jul-1631-jul-169029

How can I achieve this?, Is it possible?

Create table t_period (id number

                  ,begin_date date

                  ,end_date date

                  ,value number

                 );

insert into t_period(id,begin_date,end_date,value)

values(1,'01-jul-16','14-jul-16',40);

insert into t_period(id,begin_date,end_date,value)

values(1,'15-jul-16','31-jul-16',50);

insert into t_period(id,begin_date,end_date,value)

values(3,'01-jul-16','31-jul-16',90);

insert into t_period(id,begin_date,end_date,value)

values(2,'12-aug-16','23-aug-16',40);

commit;

Thanks,

Diana

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 26 2016
Added on Jul 27 2016
8 comments
2,353 views