我遇到了一些SQL查询,它检查第一行的日期是否不是>比起相同ID中的第二行的datefrom.Maybe最简单的方法是在示例中显示它:
ID DATEFROM DATETO PK
1234 20150512 20150518 1
1234 20150514 20150520 2
1234 20150519 null 3
2313 20150512 20150518 4
44341 20150512 null 5
现在,在ID 1234中,流程是:
1.2015-05-12 -> 2015-05-18
2.2015-05-13 -> 2015-05-20 WRONG
3.2015-05-19 -> null
在该示例中,第一行的结束日期是>比第二行的开始日期。 作为输出我想显示PK id(在这个例子中:2)。
有人能给我一些提示如何解决这个问题吗?我不想要SQL,只是提示。提前致谢
答案 0 :(得分:2)
您可以使用join
或相关子查询。我怀疑你想要一个完整的重叠比较:
select t.*
from t
where exists (select 1
from t t2
where t.id = t2.id and
(t2.start_date < t.start_date and
t2.end_date > t.start_date or t2.end_date is null)
);