我有一个包含start_date和end_date列的表,我想删除start_date和end_date都在现有日期范围内的记录
源数据:
start_date end_date
2019-03-18 00:00:00.000 2019-04-08 00:00:00.000
2019-04-01 00:00:00.000 2019-05-31 00:00:00.000
2019-04-03 00:00:00.000 2019-04-24 00:00:00.000
2019-04-24 00:00:00.000 2019-05-05 00:00:00.000
2019-05-06 00:00:00.000 2019-05-16 00:00:00.000
2019-05-06 00:00:00.000 2019-05-20 00:00:00.000
2019-05-06 00:00:00.000 2019-06-17 00:00:00.000
2019-05-10 00:00:00.000 2019-05-14 00:00:00.000
预期结果:
start_date end_date
2019-03-18 00:00:00.000 2019-04-08 00:00:00.000
2019-04-01 00:00:00.000 2019-05-31 00:00:00.000
2019-05-06 00:00:00.000 2019-06-17 00:00:00.000
答案 0 :(得分:0)
实际上并不难,您只需检查一下要检查的东西即可。只需验证没有任何记录会包含您自己的开始日期和结束日期之间的开始日期和结束日期。
类似的事情会起作用:
select *
from so_58088216 wrapping
where not exists (select *
from so_58088216 wrapped
where wrapping.start_date between wrapped.start_date and wrapped.end_date
and wrapping.end_date between wrapped.start_date and wrapped.end_date
-- don't check against yourself, this would be easier if had an ID or something
and wrapping.start_date != wrapped.start_date
and wrapping.end_date != wrapped.end_date)