例如,我目前有一个返回如下内容的查询:(您可以假设表结构是这样的)
customer_id | start_date | end_date
1 | 20120101 | 20120401
2 | 20120402 | 20121231
1 | 20130101 | 20130401
1 | 20130101 | 20130330
2 | 20130331 | 99991231
2 | 20130402 | 99991231有两点需要考虑:
因此,考虑到上述情况,我想要一个查询,它将返回第1、2、3和5行。
我的想法和方法是:
我可以编写一个查询来执行上面的操作,但我不知道如何同时处理这两个问题。或者一种完全不同的方法会更好。
Server 2008
谢谢!
发布于 2013-05-20 21:01:15
我想你可以在not exists条件下-
以下查询可用于此输出-
select customer_id , start_date , end_date
from table_name t_1
where not exists(
select 1 from table_name t_2
where t_2.customer_id = t_1.customer_id
and t_2.start_date = t_1.start_date
and t_2.end_date > t_1.end_date)
and not exists (
select 1 from table_name t_3
where t_3.customer_id = t_1.customer_id
and t_3.end_date = t_1.end_date
and t_3.start_date<t_1.start_date)https://stackoverflow.com/questions/16658093
复制相似问题