Solved

Find Overlap date range

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

744 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

8 Experts available now in Live!

Get 1:1 Help Now