I have tried many ways, but unsuccessfully, to combine Start dates and end dates where the record Id is the same and combine the where there is no break in the Date
CREATE TABLE #t (
A_ID VARCHAR(100),
BDate VARCHAR(100),
CDate VARCHAR(100)
)
INSERT INTO #T
(A_ID, BDate, CDate)
VALUES
('1000','2017/12/01','2017/12/31'),
('1000','2018/01/01','2018/03/31'),
('1000','2018/05/01','2018/05/31')
Select A_ID, bDate,cDate from
(
select BDate,A_ID,Cdate,lead(Bdate) over (order by Bdate) next_BDate from #T as t2
where exists ( select null from #T as t1
where t1.A_ID = t2.A_ID and t1.Bdate <= t2.Bdate and t1.CDate <=t2.CDate )
) as combine
where bDate < Cdate
order by BDate;
I would like to see:
1000 2017/12/01 2018/03/31 (no break in first two dates)
1000 2018/05/01 2018/05/31 (Break between 4-1-18 and 5-1-18)