Solved

Find Overlap date range

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

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

734 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