SQL日期重叠

时间:2017-06-07 12:03:04

标签: sql sql-server sql-server-2014

我在sql中有一个包含数据的表。我想知道重叠的日期。

@AutoConfigureMockMvc(secure = false)

在上表中,前三个记录重叠。如何编写查询以查找?我需要以下输出

ARN NO      From Date   To Date
1   106739  1/1/2015    3/31/2015
2   106739  1/1/2015    2/28/2015
3   106739  3/1/2015    3/31/2015
4   106739  5/1/2015    6/30/2015
5   106739  7/1/2015    9/30/2015
6   106739  9/1/2015    9/30/2015
7   106739  10/1/2015   12/31/2015
8   106739  10/1/2015   12/31/2015

由于

1 个答案:

答案 0 :(得分:2)

您可以使用exists

select t.*
from t
where exists (select 1
              from t t2
              where t2.arnno = t.arrno and
                    t2.fromdate < t.todate and
                    t2.todate > t.fromdate
             );

如果第一个在第二个结束之前开始并在第二个结束之后结束,则两个时间段重叠。