2011-01-24 7 views

답변

1

이 ... 나는이 SQL 당신을 위해 그것을 할 것입니다 단지 SQL의 문제를 생각한다 :

registrations = Registration.find_by_sql(sql) 

는 SQL이 동일 장소 :

이 레일에서
select r1.* 
from registrations r1, registrations r2 
where 
    r1.start_date < r2.start_date 
    and r1.end_date > r2.start_date 

이 같은 할 거라고 위의 SQL 코드. 당신은 예를 들어, 중복, 같은 "감동"날짜하다고 판단하는 경우 <= 여기 http://c2.com/cgi/wiki?TestIfDateRangesOverlap

WHERE A.start < B.end 
    AND B.start < A.end 

변경 <에게 설명

+0

r2가 r1에 완전히 포함되어있을 때만 보이는 것처럼 보입니다. 나는 겹치는 부분을 찾아야한다고 생각합니다. –

2

는 중복 시험 정답은, Other.End 대 회 This.Start입니다

Jan1 - Jan13 
Jan13 - Jan15 

반환 <= 겹치면 아니라 오버랩 <

0

위해이 하나 완전히 다른에 포함되지 않은 경우에도, 두 개의 범위 사이 어떤 오버랩이있는 범위 (1)를 찾는다.

select r1.* 
from registrations r1 
JOIN registrations r2 ON r1.start_date 
        between r2.start_date 
         AND r2.end_date 
0

표준 SQL에는 OVERLAPS 연산자가 있습니다.

CREATE TABLE over_laps 
(
    start_date date NOT NULL, 
    end_date date NOT NULL, 
    CONSTRAINT over_laps_pkey PRIMARY KEY (start_date, end_date), 
    CONSTRAINT over_laps_check CHECK (start_date < end_date) 
) 


insert into over_laps values 
('2011-01-10', '2011-01-15'), 
('2011-01-08', '2011-01-09'), 
('2011-01-09', '2011-01-10'), 
('2011-01-09', '2011-01-11'), 
('2011-01-10', '2011-01-12'), 
('2011-01-11', '2011-01-13'), 
('2011-01-13', '2011-01-15'), 
('2011-01-14', '2011-01-16'), 
('2011-01-15', '2011-01-17'), 
('2011-01-17', '2011-01-19'); 

select t1.start_date start_1, t1.end_date end_1, 
     t2.start_date start_2, t2.end_date end_2 
from over_laps t1 
inner join over_laps t2 
    on ((t1.start_date, t1.end_date) overlaps (t2.start_date, t2.end_date)) 
-- Exclude rows that overlap themselves, a trivial case in a self-joined table. 
where t1.start_date <> t2.start_date and 
     t1.end_date <> t2.end_date 
관련 문제