我有一些时间间隔,可能会重叠。如果它们重叠,则需要折叠它们。仅供参考,entryDate不为null,但ExitDate可以为null。如果重叠,则需要min(entryDate)和maxium(没有Null exitdate)。
用于创建示例数据集的代码(仅包含一个SSN):
create table #test (
SSN int,
EntryDate datetime,
ExitDate datetime,
)
;
insert into #test (SSN, EntryDate, ExitDate)
values(1, '2001-01-01', '2001-01-09'),
(1, '2001-01-09', null),
(1, '2001-01-09', '2001-01-12'),
(1, '2000-12-19', '2001-01-12'),
(1, '2001-01-21', '2001-10-01'),
(1, '2001-11-01', null),
(1, '2001-02-01', null),
(1, '2001-12-01', null),
(1, '2001-12-01', '2001-12-16')
;
想要的数据集:
SSN EntryDate ExitDate
1 2000-12-19 2001-01-12
1 2001-01-21 2001-10-01
1 2001-11-01 NULL
1 2001-12-01 2001-12-16