联表查询:使用SQL进行数据去重[穷举法]

有一个表A,请使用SQL找出所有根据user_code重复,且open_time与close_time存在重复的数据,如果close_time为空则默认为关闭时间无穷大。

Id

user_code

open_time

close_time

1

A

2019-01-01

2019-12-31

2

B

2019-02-01

3

C

2019-02-03

2019-12-31

100

A

2020-01-01

101

C

2018-01-01

2020-12-31

200

A

2020-06-01

201

B

2019-06-01

2020-12-31

例如上表中的数据,第1行与100、200行虽然user_code重复,但时间不重叠,不应该被查出来;100行与200行user_code重复,且open_time与close_time存在重叠;3行与101行的存在重叠应该被查出来;2行与201行时间存在重叠应该查出来。

select distinct
a.*
from
A as a inner join A as b
on a.user_code=b.user_code
where
(a.open_time<b.open_time and b.close_time<a.close_time)
or
(b.open_time<a.open_time and b.close_time<a.close_time)
or
(b.open_time<a.open_time and a.close_time<b.close_time)
or
(a.open_time<b.open_time and b.close_time<a.close_time)
or
(a.open_time<b.open_time and a.close_time is null)
or
(b.open_time<a.open_time and b.close_time is null)
order by a.user_code;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值