问题是这样的:
1.表结构
表Carriers(运营商) 有以下字段:字段名 类型
carrier_cd(运营商代码) varchar,
eff_time(生效时间) date,
exprie_time(失效时间) date,
operation_time(操作时间) date
............2.表记录
如表中有这样的记录:
----------- ----------------- ----------------------
|carrier_cd | eff_time | expire_time |
----------- ------------------ ----------------------
| CHNCT |1992/02/01 00:00:00| 2007/02/01 00:00:00 |
----------- ------------------ ----------------------
| CHNCT |1997/02/01 00:00:00| 2008/09/01 00:00:00 |
----------- ------------------ ----------------------
| CHNCT |2007/02/01 00:00:00| 2010/09/01 00:00:00 |
----------- ------------------ ----------------------3.要求:
取出carrier_cd 重复的记录,重复的条件是:eff_time和expire_time在同一区间内如上记录两个时间就是在同一个区间内,也就是说第一条记录的expire_time+1秒必须大于第二条记录的生效时间,而且这个重复有可能不止一条,如上记录应该怎么取呢或者是用存储过程来实现呢?本人的SQL和DB方面的知识实现有限,望各位高手帮帮忙.............
1.表结构
表Carriers(运营商) 有以下字段:字段名 类型
carrier_cd(运营商代码) varchar,
eff_time(生效时间) date,
exprie_time(失效时间) date,
operation_time(操作时间) date
............2.表记录
如表中有这样的记录:
----------- ----------------- ----------------------
|carrier_cd | eff_time | expire_time |
----------- ------------------ ----------------------
| CHNCT |1992/02/01 00:00:00| 2007/02/01 00:00:00 |
----------- ------------------ ----------------------
| CHNCT |1997/02/01 00:00:00| 2008/09/01 00:00:00 |
----------- ------------------ ----------------------
| CHNCT |2007/02/01 00:00:00| 2010/09/01 00:00:00 |
----------- ------------------ ----------------------3.要求:
取出carrier_cd 重复的记录,重复的条件是:eff_time和expire_time在同一区间内如上记录两个时间就是在同一个区间内,也就是说第一条记录的expire_time+1秒必须大于第二条记录的生效时间,而且这个重复有可能不止一条,如上记录应该怎么取呢或者是用存储过程来实现呢?本人的SQL和DB方面的知识实现有限,望各位高手帮帮忙.............
select distinct * from (
select * from a where exists (select 1 from b where b.rowid=a.rowid and
to_date(b.expire_time)+1/86400>=to_date(a.eff_time) or
to_date(a.expire_time)+/86400>=to_date(b.eff_time)
)
)
select * from carriers where carrier_cd in
(select carrier_cd from carriers group by carrier_cd having count(*) > 1)
测试了一下都没有数据返回,而用你的SQL测试了一下就返回很多条记录.但是还是谢谢你了
select * from a where exists (select 1 from b where b.carrier_cd=b.carrier_cd and
to_date(b.expire_time)+1/86400>=to_date(a.eff_time) or
to_date(a.expire_time)+/86400>=to_date(b.eff_time)
)
)
正确的格式是to_date(str,'yyyy/mm/dd hh24:mi:ss'),请自行替换
select * from a where exists
(select 1 from b where b.carrier_cd=b.carrier_cd and
(to_date(b.expire_time)+1/86400>=to_date(a.eff_time)
and to_date(b.expire_time)+1/86400<=to_date(a.expire_time)
) or
(to_date(a.expire_time)+/86400>=to_date(b.eff_time)
and to_date(a.expire_time)+/86400<=to_date(b.expire_time)
)
)
where exists
(
select * from table b
where a.carrier_cd=b.carrier_cd
and a.exprie_time>=b.eff_time
)
where a.eff_time<=b.expire_time and a.eff_time>b.eff_time
select a.* from carriers a,carriers b
where a.eff_time<=b.expire_time and a.eff_time>b.eff_time and a.carrier_cd=b.carrier_cd
不是你理解能力差吧,应该是我没有说得很清楚.我再详细说一下吧:如表中有这样的数据:
----------- ----------------- ----------------------
|carrier_cd | eff_time | expire_time |
----------- ------------------ ----------------------
| CHNCT |1992/02/01 00:00:00| 2007/02/01 00:00:00 |
----------- ------------------ ----------------------
| CHNCT |1997/02/01 00:00:00| 2008/09/01 00:00:00 |
----------- ------------------ ----------------------
| CHNCT |2006/02/01 00:00:00| 2010/09/01 00:00:00 |
----------- ------------------ ----------------------
| CHDBM |2007/02/01 00:00:00| 2010/09/01 00:00:00 |
----------- ------------------ ----------------------要查出这样的记录:
同一carrier_cd条件下,eff_time和expire_time有交叉的情况,什么是交叉的情况呢,如上面给出的数据,在carrier_cd为CHNCT的记录都为重复记录,因为这几条记录的时间段都出现了交叉的情况了,如下图:
时间轴: |-------------------------------------------------------------------|
第1条记录时间 : ^1992/02/01 00:00:00 ^2007/02/01 00:00:00
第2条记录时间 : ^1997/02/01 00:00:00 ^2008/09/01 00:00:00
第3条记录时间 : ^2006/02/01 00:00:00 ^2010/09/01 00:00:00不知道这样你看清楚了吗?
where a.eff_time<b.expire_time and a.eff_time>b.eff_time and a.carrier_cd=b.carrier_cd
union all
select a.* from carriers a,carriers b
where a.expire_time<b.expire_time and a.expire_time>b.eff_time and a.carrier_cd=b.carrier_cd
union all
select a.* from carriers a,carriers b
where a.eff_time<b.eff_time and a.expire_time>b.expire_time and a.carrier_cd=b.carrier_cd
union all
select a.* from carriers a,carriers b
where a.eff_time>b.eff_time and a.expire_time<b.expire_time and a.carrier_cd=b.carrier_cd