Solved

See if a datetime is between another records datetime

Posted on 2012-12-28
3
170 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

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

Suggested Solutions

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…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

738 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