Solved

Find Overlap date range

Posted on 2004-04-16
1
2,611 Views
Last Modified: 2007-12-19
I have a data structure like this:

ShipId      StartDate      EndDate
------------------------------------
Ship1    SDate1         SDate2
Ship1    SDate3         SDate4
Ship1    SDate5         SDate6
Ship2    SDate7         Sdate8
Ship2    SDate9         SDate10
......

I need to create a sql script to find out if any ship got Overlaped ship schedule. For example, if SDate1..SDate2 Overlap with SDate5..SDate6, the script should return the Shipid (Ship1 in this case) and Overlapped rows (Row1 and Row3 in this case)

Please Help!
0
Comment
Question by:3b56
[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
1 Comment
 
LVL 7

Accepted Solution

by:
ChrisFretwell earned 100 total points
ID: 10843846
--do you have another record thats a unique id field? This code assumes there is - comment below

select t1.shipid, t1.startdate, t1.enddate, t2.startdate, t2.enddate
from shiptable t1
join shiptable t2 on t1.shipid = t2.shipid and (t2.startdate between t1.startdate and t1.enddate or t2.enddate between t1.startdate and t1.enddate)
and t1.id <> t2.id ---- use this if there is an id field - if there isnt, you'll need to change this so that you dont match the record to itself (t1.start and end dates not exactly the same as t2.start and end dates)
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

691 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