Solved

See if a datetime is between another records datetime

Posted on 2012-12-28
3
160 Views
Last Modified: 2013-01-03
Hopefully I can explain myself on this one.

I work for a trucking company and we trying to see if we have overlapping times.

I have a query that tells me the StartTime and EndTime for trips based on a specific vehiicle that performs the services.   These are two columns and look something like so:


Order             StartTime                                          EndTime
123                 2012-12-28 11:29:26.000                2012-12-28 13:41:02.000
344                 2012-12-28 08:35:15.000                2012-12-28 11:10:53.000
xyx                  2012-12-28 11:45:26.000                2012-12-28 11:59:26.000

I would like to add a column that checks for overlapping times.  So lets say I have an Order xyx and the start time falls within that of the times already indicated, then have a column that basically flags it using a SELECT Query.

An example would be lets say Order 'xyz' above has the start time of '2012-12-28 11:45:26.000' well that would be flagged, because using the example I have above it can't be because during that time, Order 123 was being performed.  Same would go for the End Time.

Just trying to figure out an easy way of identify those that overlap because my trucks can't be at two different places at the same time if that makes sense?
0
Comment
Question by:chrisryhal
3 Comments
 
LVL 45

Assisted Solution

by:Kdo
Kdo earned 250 total points
ID: 38728115
Hi Chris,

A pretty good starting point is to just query where the start or end date appears in another record's range.

SELECT * FROM orders o
INNER JOIN orders o1
  ON o.startdate between o1.startdate and o1.enddate
UNION ALL
SELECT * FROM orders o
INNER JOIN orders o1
  ON o.enddate between o1.startdate and o1.enddate;

similary:

SELECT * FROM orders o
INNER JOIN orders o1
  ON o.startdate between o1.startdate and o1.enddate
  OR o.enddate between o1.startdate and o1.enddate;


Either of those should be a great start!


Good Luck,
Kent
0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 250 total points
ID: 38728207
First, it will really help performance to have an index on: ( StartTime, EndTime, [Order] )

Then:

SELECT o1.*, o2.*
FROM dbo.Orders o1
INNER JOIN dbo.Orders o2 ON
    o2.[Order] > o1.[Order] AND NOT (o2.StartTime > o1.EndTime OR o2.EndTime < o1.StartTime)


Using the sample test data:

;with orders as (
    select '123' as [order],      '2012-12-28 11:29:26.000' as starttime, '2012-12-28 13:41:02.000' as endtime union all
    select '344',                 '2012-12-28 08:35:15.000'             , '2012-12-28 11:10:53.000' union all
    select 'xyx',                 '2012-12-28 11:45:26.000'             , '2012-12-28 11:59:26.000'
)
SELECT o1.*, o2.*
FROM Orders o1
INNER JOIN orders o2 ON
    o2.[Order] > o1.[Order] AND NOT (o2.StartTime > o1.EndTime OR o2.EndTime < o1.StartTime)
0
 
LVL 2

Author Closing Comment

by:chrisryhal
ID: 38740685
Thanks for both of your help and sorry for my delay.

I was able to get it to work with the help of both of you :)
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how the fundamental information of how to create a table.

758 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

20 Experts available now in Live!

Get 1:1 Help Now