?
Solved

Find Overlap date range

Posted on 2004-04-16
1
Medium Priority
?
2,614 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 400 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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Suggested Courses

801 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