sql - 查找重叠日期并在 SQL Server 中排除它们?
问题描述
这是我的表“地图”的一部分:例如,比较 A 和 D 行。A 日期范围完全在 D 日期范围内,因此我们要删除该行。(我不想删除这一行,但我的查询结果不应该显示该行)。
sed id code startdate enddate
101 1019 A 2002-12-02 2009-11-17
101 1019 B 1986-01-02 2009-11-04
101 1019 C 2009-07-01 2009-11-17
101 1019 C 2002-12-02 2009-06-30
101 1019 D 1986-01-03 2009-11-17
101 1019 E 2007-10-15 2009-11-17
101 1019 E 1992-01-31 1999-08-30
101 1019 F 2007-11-26 2009-11-05
101 1019 F 2007-09-05 2007-11-22
101 1019 F 2007-07-06 2007-09-03
解决方案
这似乎可以回答您的问题:
select t.*
from t
where not exists (select 1
from t t2
where t2.startdate < t.startdate and t2.enddate >= t.enddate and
t2.sed = t.sed and t2.id = t.id and t2.code <> t.code
);
推荐阅读
- openshift - 使用 openshift 和 unix 管道
- android - 如何从 RecyclerView ListAdapter 按位置获取项目?
- python - 有没有更好的方法来删除搁置中键的特定值?
- c# - 在c#中取消后如何重新启动CancellationTokenSource?
- javascript - 为什么这个 DOM 代码没有出现在页面上?
- java - Java:无法在 Windows 10 上打开 COM 端口
- swift - Swift 编译器说的关于 Bool("") 的奇怪消息是什么意思?
- c# - 如何在 ASP.Net Core 中使用 Google.Protobuf.WellknownTypes.Struct
- r - 在选择列 x 中的特定行后如何从列 y 中提取信息
- javascript - 在javascript结构中查找最近的最低日期