Solved

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

Posted on 2013-06-27
7
557 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

895 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now