2016-08-30 6 views
0

다른 행과 날짜가 겹치는 행만 찾으려고합니다.중복되는 날짜가있는 행을 반환하십시오.

예시 DATA :

여기 내 현재 쿼리
Period: 13, Start Date: 9/1/2015, End Date: 9/30/2015 

Period: 14, Start Date: 9/15/2015, End Date: 10/30/2015 

Period: 15, Start Date: 11/1/2015, End Date: 11/30/2015 

:

select pd1.periodNumber AS 'Period Seq A', pd1.startDate, pd1.endDate, 
pd2.periodNumber AS 'Period Seq B', pd2.startDate, pd2.endDate,  
pd2.adjustment 
from dbo.PeriodDefinition pd1 
inner join dbo.PeriodDefinition pd2 on pd2.startDate > pd1.startDate and 
pd2.startDate < pd2.endDate 
where (pd1.adjustment =1 AND pd2.adjustment = 1) 

그것만 1 개 행을 리턴하고, 기간 (13) (기간 A)과주기 (14)로부터 데이터를 표시한다 (기간 B) 지금 당장 3 행을 반환하고 결과에 13과 15를 표시합니다. 내가 뭘 놓치고 있니? 고맙습니다!

답변

1
and pd2.startDate < pd2.endDate 

당신도 아마

and pd1.periodNumber != pd2.periodNumber 

(periodNumber 가정하면 고유 한 행 식별자입니다.)

원하는

on pd2.startDate between pd1.startDate and pd1.endDate 

작성합니다

and pd2.startDate < pd1.endDate 

해야한다

+0

* Headdesk * 증거를 읽어 주셔서 감사합니다! :디 – Michelle

관련 문제