Solved

Find gaps in datetime columns (where starttime of next <> endtime of current)

Posted on 2010-11-17
6
412 Views
Last Modified: 2012-05-10
I need to identify holes in a schedule, basically I am looking for missing data.   Using the following data as a test case I need to identify any timeslots between the variables @daystart and @dayend where there isn't data.

LocID	StartTime		EndTime
 1	11/15/2010 1:00pm	11/15/2010 2:00pm
 1	11/15/2010 2:00pm	11/15/2010 3:00pm
 1	11/15/2010 4:00pm	11/15/2010 5:00pm
 1	11/15/2010 7:00pm	11/15/2010 8:00pm

Declare @daystart datetime
Declare @dayend datetime

Set @daystart = '12:00pm'  --First appointment of the day
Set @dayend = '9:00pm'  --Last appointment of the day

Open in new window


The results would look like this.
 1	11/15/2010 12:00pm	11/15/2010 1:00pm
 1	11/15/2010 3:00pm	11/15/2010 4:00pm
 1	11/15/2010 5:00pm	11/15/2010 7:00pm
 1	11/15/2010 8:00pm	11/15/2010 9:00pm

Open in new window


Any advise or help is greatly appreciated.

Thank you...
0
Comment
Question by:clintnash
  • 3
  • 3
6 Comments
 
LVL 14

Expert Comment

by:Christopher Gordon
ID: 34158114
I probably know what you're going to say but I'll ask anyways.  Are the "time slots" always in hour increments?  If so, you could always join to a Time_Dimension type table.
0
 
LVL 1

Author Comment

by:clintnash
ID: 34158727
Unfortunately no. The time increments range from 15 minutes to 2 hours.
0
 
LVL 14

Expert Comment

by:Christopher Gordon
ID: 34159436
Something like this maybe?  You can past the whole thing into SSMS and run it.  Note:  The table var I created is just for test and would be replaced by actual table data.

The logic behind it would be to use the RowNumber function to create a unique ID per location ID and use that RowNumber as part of a join to get the previous records value.

declare @myTable table (LocID int,      StartTime datetime,       EndTime datetime)

declare @startDateTime datetime = '11/15/2010 12:00 PM'
declare @endDateTime datetime = '11/15/2010 9:00 PM'

insert into @myTable values ( 1, '11/15/2010 1:00pm', '11/15/2010 2:00pm')
insert into @myTable values ( 1, '11/15/2010 2:00pm', '11/15/2010 3:00pm')
insert into @myTable values ( 1, '11/15/2010 4:00pm', '11/15/2010 5:00pm')
insert into @myTable values ( 1, '11/15/2010 7:00pm', '11/15/2010 8:00pm');

with MyTableWithStartAndEnd as
(
      select 1 as LocId, @startDateTime as StartTime, @startDateTime as EndTime
      union
      select * from @myTable
      union
      select 1, @endDateTime as StartTime, @endDateTime as EndTime

)

, MyTableWithCounter as
(
      select ROW_NUMBER() over (partition by LocId order by endtime) as Instance_Counter
            , *
                  
      from      MyTableWithStartAndEnd
)

, MyTableWithRanges as
(
      select
             a.[LocId]
            
            ,a.[StartTime]
            
            ,a.EndTime
      
            ,a.Instance_Counter
      
            ,abs((select datediff(minute, a.EndTime, b.StartTime) from MyTableWithCounter b where a.locId = b.LocID and b.[Instance_Counter] = a.[Instance_Counter] + 1)) as GapRangeInMinutes

      from      MyTableWithCounter a
)

 select      
 
            StartTime
      ,      DATEADD(MINUTE, GapRangeInMinutes, StartTime) as EndTime
      ,      case
                  when GapRangeInMinutes < 60 then 0
                  else GapRangeInMinutes/60                  
            end      as      GapInHours
      ,      GapRangeInMinutes                        as      GapInMinutes
 
 from      MyTableWithRanges
 where ISNULL(GapRangeInMinutes,0) > 0
 
 
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 1

Author Comment

by:clintnash
ID: 34159877
I think this is almost there.  The number of results are correct but the times in the results are not.  I cut and pasted the code and am getting the following for output.

Start Time                        End Time                       GinH  GinMin
Nov 15 2010 12:00PM	Nov 15 2010  1:00PM	1	60
Nov 15 2010  2:00PM	Nov 15 2010  3:00PM	1	60
Nov 15 2010  4:00PM	Nov 15 2010  6:00PM	2	120
Nov 15 2010  7:00PM	Nov 15 2010  8:00PM	1	60

Open in new window


The results should be

Start Time                        End Time                       GinH  GinMin
Nov 15 2010 12:00PM	Nov 15 2010  1:00PM	1	60
Nov 15 2010 3:00PM	Nov 15 2010  4:00PM	1	60
Nov 15 2010 5:00PM	Nov 15 2010  7:00PM	2	120
Nov 15 2010 8:00PM	Nov 15 2010  9:00PM	1	60

Open in new window


It seems like after the first row that the rest of the timeslots are off by an hour?  Thank you very much for your help.

0
 
LVL 14

Accepted Solution

by:
Christopher Gordon earned 500 total points
ID: 34161022
--try this logic

declare @myTable table (LocID int,      StartTime datetime,       EndTime datetime)

declare @startDateTime datetime = '11/15/2010 12:00 PM'
declare @endDateTime datetime = '11/15/2010 9:00 PM'

insert into @myTable values ( 1, '11/15/2010 1:00pm', '11/15/2010 2:00pm')
insert into @myTable values ( 1, '11/15/2010 2:00pm', '11/15/2010 3:00pm')
insert into @myTable values ( 1, '11/15/2010 4:00pm', '11/15/2010 5:00pm')
insert into @myTable values ( 1, '11/15/2010 7:00pm', '11/15/2010 8:00pm');

with MyTableWithStartAndEnd as
(
      select 1 as LocId, @startDateTime as StartTime, @startDateTime as EndTime
      union
      select * from @myTable
      union
      select 1, @endDateTime as StartTime, @endDateTime as EndTime

)

, MyTableWithCounter as
(
      select ROW_NUMBER() over (partition by LocId order by endtime) as Instance_Counter
            , *
                 
      from      MyTableWithStartAndEnd
)

, MyTableWithRanges as
(
      select      
            a.LocId
      ,      a.Instance_Counter
      ,      a.StartTime
      ,      A.EndTime
      ,      b.StartTime            as      NextStartTime
      ,      DATEDIFF(minute, a.EndTime, b.StartTime) as TimeSpanMinutes

      from      MyTableWithCounter a
      
      left outer join MyTableWithCounter b on
            (a.Instance_Counter + 1) = b.Instance_Counter
)

select      
            LocId
      ,      Instance_Counter
      ,      convert(nvarchar(30), EndTime, 109)                  as      StarTimeOfHiatus
      ,      TimeSpanMinutes                                                as      SpanofHiatusMinuts
      
      ,   case
                    when TimeSpanMinutes < 60 then 0
                    else TimeSpanMinutes / 60                  
            end                                                                  as      SpanofHiatusHours
      
      ,      convert(nvarchar(30), dateadd(minute, timespanminutes, EndTime), 109)      as      EndTimeHiatus

from      MyTableWithRanges

where ISNULL(TimeSpanMinutes,0) > 0
0
 
LVL 1

Author Closing Comment

by:clintnash
ID: 34166977
Brilliant!  Thank you so much for your time.  This was another opportunity to not just get an answer to a problem but learn something in the process.  Thanks again...
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Join & Write a Comment

I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

743 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

13 Experts available now in Live!

Get 1:1 Help Now