Click here to Skip to main content
15,917,320 members
Home / Discussions / Database
   

Database

 
GeneralRe: Insert in Excel & Get in SQL table Pin
notes4we19-Sep-08 7:09
notes4we19-Sep-08 7:09 
GeneralRe: Insert in Excel & Get in SQL table Pin
ChandraRam19-Sep-08 8:01
ChandraRam19-Sep-08 8:01 
GeneralRe: Insert in Excel & Get in SQL table Pin
notes4we19-Sep-08 10:09
notes4we19-Sep-08 10:09 
GeneralRe: Insert in Excel & Get in SQL table Pin
ChandraRam19-Sep-08 23:27
ChandraRam19-Sep-08 23:27 
GeneralRe: Insert in Excel & Get in SQL table Pin
notes4we19-Sep-08 10:06
notes4we19-Sep-08 10:06 
GeneralRe: Insert in Excel & Get in SQL table Pin
Ashfield21-Sep-08 7:59
Ashfield21-Sep-08 7:59 
GeneralRe: Insert in Excel & Get in SQL table Pin
Paul Conrad21-Sep-08 8:26
professionalPaul Conrad21-Sep-08 8:26 
QuestionPair Date Overlaps [modified] Pin
Lash2018-Sep-08 5:10
Lash2018-Sep-08 5:10 
Hi there,

I need to write a query that gets overlapping date ranges from a table and also generate a random unique number for each group of overlaps. I was able to write the code below in SQL 2005 to get the overlaps for each employee in my table but I'm stuck on pairing up the overlaps.

My code currently generates the following:

Payment_Key Emp_No Start_date End_date
55 JDOE 2008-01-13 2008-01-26
56 JDOE 2008-01-16 2008-01-31
100 MPOE 2007-01-01 2007-01-15
104 MPOE 2007-01-10 2007-01-13
145 MPOE 2007-12-16 2007-12-31
150 MPOE 2007-12-16 2007-12-31
151 MPOE 2007-12-31 2007-12-31

But I need to generate the following:

Payment_Key Emp_No Start_date End_date Dup_Pair
55 JDOE 2008-01-13 2008-01-26 1
56 JDOE 2008-01-16 2008-01-31 1
100 MPOE 2007-01-01 2007-01-15 2
104 MPOE 2007-01-10 2007-01-13 2
145 MPOE 2007-12-16 2007-12-31 3
150 MPOE 2007-12-16 2007-12-31 3
151 MPOE 2007-12-31 2007-12-31 3

Here's my current code:

select distinct
t1.payment_key,
t1.emp_no,
t1.start_date,
t1.end_date
from tbl_empl_pymt_rate t1 join tbl_empl_pymt_rate t2
on t1.emp_no = t2.emp_no and
(t2.start_date between t1.start_date and t1.end_date or
t2.end_date between t1.start_date and t1.end_date or
((t1.start_date between t2.start_date and t2.end_date) and (t1.end_date between t2.start_date and t2.end_date)))
and t1.payment_key <> t2.payment_key
order by t1.emp_no, t1.start_date

Any help would be greatly appreciated.

modified on Thursday, September 18, 2008 12:07 PM

AnswerRe: Pair Date Overlaps Pin
Wendelius21-Sep-08 0:35
mentorWendelius21-Sep-08 0:35 
QuestionDatabase Pin
WebMaster18-Sep-08 2:01
WebMaster18-Sep-08 2:01 
AnswerRe: Database Pin
J4amieC18-Sep-08 2:16
J4amieC18-Sep-08 2:16 
AnswerRe: Database Pin
Paul Conrad18-Sep-08 6:42
professionalPaul Conrad18-Sep-08 6:42 
AnswerRe: Database Pin
nelsonpaixao18-Sep-08 13:03
nelsonpaixao18-Sep-08 13:03 
AnswerGet out now Pin
leckey18-Sep-08 14:44
leckey18-Sep-08 14:44 
QuestionHelp!! Pin
WebMaster18-Sep-08 1:56
WebMaster18-Sep-08 1:56 
AnswerRe: Help!! Pin
J4amieC18-Sep-08 2:15
J4amieC18-Sep-08 2:15 
AnswerRe: Help!! Pin
Mycroft Holmes18-Sep-08 2:44
professionalMycroft Holmes18-Sep-08 2:44 
GeneralRe: Help!! Pin
J4amieC18-Sep-08 2:59
J4amieC18-Sep-08 2:59 
GeneralRe: Help!! Pin
Ashfield18-Sep-08 8:52
Ashfield18-Sep-08 8:52 
AnswerRe: Help!! Pin
Tim Carmichael18-Sep-08 5:11
Tim Carmichael18-Sep-08 5:11 
AnswerRe: Help!! Pin
AlexeiXX319-Sep-08 10:43
AlexeiXX319-Sep-08 10:43 
Questioncalculate sql Pin
foryou18-Sep-08 1:24
foryou18-Sep-08 1:24 
GeneralRe: calculate sql Pin
nelsonpaixao18-Sep-08 13:15
nelsonpaixao18-Sep-08 13:15 
AnswerRe: calculate sql Pin
foryou19-Sep-08 4:20
foryou19-Sep-08 4:20 
GeneralRe: calculate sql Pin
nelsonpaixao21-Sep-08 15:00
nelsonpaixao21-Sep-08 15:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.