?
Solved

How do we count the number of months in overlapping periods?

Posted on 2010-01-11
10
Medium Priority
?
454 Views
Last Modified: 2013-12-19
Hi,

Suppose we have a table:
Employer | Begin_Date | End_Date
A             | Jan 2010     | Mar 2010
A             | Feb 2010     | Apr 2010
A             | Oct 2010     | Dec 2010
B             | Jan 2010     | May 2010
C             | Aug 2010    | Sep 2010
...

Where the begin date and end date are the date when a project begins and ends.
I want a script to find out how many months in year 2010 employer A will spend on projects. Number of projects can change each year. Difficulty is how we would know that there is an overlap month between Jan2010-Mar2010 and Feb2010-Apr2010.

Thanks in advance
0
Comment
Question by:shepp_it
[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
  • 3
  • 2
  • +2
10 Comments
 
LVL 26

Expert Comment

by:tigin44
ID: 26287548
simply
select Employer,  MONTHS_BETWEEN(End_Date,  Begin_Date)
from yourTable

Open in new window

0
 

Author Comment

by:shepp_it
ID: 26287631
I want the total number of month in 2010 for employer A, which should be 7 (Jan, Feb, Mar, Apr, Oct, Nov, Dec).
0
 
LVL 30

Expert Comment

by:Reza Rad
ID: 26287655
select Employer,  sum(MONTHS_BETWEEN(End_Date,  Begin_Date))
from yourTable
group by Employer
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 26

Expert Comment

by:tigin44
ID: 26287737
so..
SELECT Employer, SUM(duration)
FROM	(select Employer,  MONTHS_BETWEEN(End_Date,  Begin_Date) AS duration
		from yourTable) A
group by Employer

Open in new window

0
 
LVL 57

Expert Comment

by:HainKurt
ID: 26287818
lets work on something like this

say M = to_char(sysdate,'mm')

select Employer,  
case when 1 between M1 and M2 then 1 else 0 end E1,
case when 1 between M1 and M2 then 1 else 0 end E2,
..
case when 12 between M1 and M2 then 1 else 0 end E12
from yourTable

say this is Q. Then this will give you

select employer, max(e1) me1, max(e2) me2,..., max(e12) me12 from yourTable group by employer

A 110110000001
B 000111010101

then the count of 1's here will give you the result that you are looking for, right?

say above is R

select employer, (me1 + me2 + me12) MonthsWorked from R
0
 

Author Comment

by:shepp_it
ID: 26287821
reza and tigin, both of you are  incorrect. Duplicate month shouldn't be included.
For example, if I insert another row (A, Jan 2010, Apr 2010), the number of months should not change because this period overlaps the existing first two rows.
0
 
LVL 57

Accepted Solution

by:
HainKurt earned 2000 total points
ID: 26287843
@ above solution

M1 = to_char(Begin_Date,'mm')
M2 = to_char(Begin_Date,'mm')

Q :

select Employer,  
case when 1 between M1 and M2 then 1 else 0 end E1,
case when 1 between M1 and M2 then 1 else 0 end E2,
..
case when 12 between M1 and M2 then 1 else 0 end E12
from yourTable

R :
select employer, max(e1) me1, max(e2) me2,..., max(e12) me12 from (Q) group by employer

solution :
select employer, (me1 + me2 + .. + me12) MonthsWorked from (R)
0
 
LVL 26

Expert Comment

by:tigin44
ID: 26288099
in order to achive your desired result set you should find out the overlapping date intervals and merge them to produce a single record. After that you may get the results by using the commands that I SUPPL0ED
0
 
LVL 57

Expert Comment

by:HainKurt
ID: 26288257
did you try 26287818? it should give what you want...
0
 
LVL 15

Expert Comment

by:Shaju Kumbalath
ID: 26289989
I don't have a database right now to test it down try something like this
select distinct count(months) from
(select trunc(begin_dt,'mm')+level-1 as months from table where employer'='A' connect by trunc(begin_dt,'mm')+level-1<=trunc(end_date,'mm'));
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.
Suggested Courses

752 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