Solved

Find Overlap date range

Posted on 2004-04-16
1
2,607 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
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
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.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

821 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