Solved

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

Posted on 2013-06-27
7
586 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
[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
  • 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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

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