Solved

See if a datetime is between another records datetime

Posted on 2012-12-28
3
171 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
[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 Comments
 
LVL 45

Assisted Solution

by:Kent Olsen
Kent Olsen 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:
Scott Pletcher 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

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
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…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

628 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