Solved

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

Posted on 2013-06-27
7
551 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Viewers will learn how the fundamental information of how to create a table.
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.

706 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

17 Experts available now in Live!

Get 1:1 Help Now