Solved

Calculating hours and minutes between dates, and if the span of time overlaps two months

Posted on 2013-06-27
7
563 Views
Last Modified: 2013-06-27
I have a query with a begin and end date parameter.  However, I find I also need to select people who have time in a house that overlaps from one month to the next.  They don't show because of the end date.

Example:  Parameters - 01/01/2013 to 01/31/2013
 Person1 - At a facility during the month
Person2 - At a facility 01/25/2013 to 02/05/2013
Person3 - At a facility 12/28/2012 to 01/04/2013

Person1 will show up, person2 won't because the end date isn't <= @enddate
Person3 won't show because of the begindate.

I also calculate how many days and hours they are at a facility.  I want to capture the overlap people and only show the days and hours for the time during the begin and end dates.  I've attached the part of my code with the calculations and selections.
SQLQuery6.sql
0
Comment
Question by:Sherry
  • 3
  • 2
  • 2
7 Comments
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39281911
sample data & expected results?
this will certainly help provide a speedy result
0
 
LVL 8

Accepted Solution

by:
didnthaveaname earned 500 total points
ID: 39281917
Why don't you check to see if:

( m1.prmvt_mvmt_ts >= @beginDate or m2.prmvt_mvmt_ts <= @endDate ) and
datediff( mm, m1.prmvt_mvmt_ts, m2.prmvt_mvmt_ts ) >= 1

Edit: congrats on getting 1mil points, Paul!  I am going to hold off on the case statement until you're able to post some sample data/examples because there are a lot of different ways that can go, whereas, I think the previous was what you were looking for for that part...
0
 

Author Closing Comment

by:Sherry
ID: 39281932
Thank you.  That was about the only one I hadn't tried.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 48

Expert Comment

by:PortletPaul
ID: 39281991
@didnthaveaname - :) thanks, seemed like those last few points would never arrive
Cheers, Paul.
0
 

Author Comment

by:Sherry
ID: 39281995
I'll get some posted.  I see that I'm getting most of what I want, but I'm also getting some that I shouldn't.  I'm wondering if maybe an IF statement in my where clause might work better.
0
 

Author Comment

by:Sherry
ID: 39282020
Full Query and data results.  I removed the columns that are irrelevant.
SQLQuery1.sql
SampleData.xlsx
0
 
LVL 8

Expert Comment

by:didnthaveaname
ID: 39282068
I think this should do it:

   case
      when M.RELEASE_DATE > @end_Date then ( dateDiff( hour, M.confinement_date, @endDate ) / 24 )
      when M.CONFINEMENT_DATE < @begin_date then ( dateDiff( hour, @begin_date, M.release_date ) / 24 ) as TotalDays
      else null
   end as TotalDays,
   case
      when M.RELEASE_DATE > @end_Date then ( dateDiff( hour, M.confinement_date, @endDate ) % 24 )
      when M.CONFINEMENT_DATE < @begin_date then ( dateDiff( hour, @begin_date, M.release_date ) % 24 ) as TotalDays
      else null
   end as LeftOverHours,

Open in new window

0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how the fundamental information of how to create a table.

776 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