问题是这样的:
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.   

    我们假设这个表的ROWID不存在重复的问题。
    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)
       )
    )
      

  2.   

    刚刚用你的SQL试了一下好像不行吧,因为我用
    select * from carriers where carrier_cd in 
    (select carrier_cd from carriers group by carrier_cd having count(*) > 1)
    测试了一下都没有数据返回,而用你的SQL测试了一下就返回很多条记录.但是还是谢谢你了
      

  3.   

    select distinct * from (
    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)
    )
    )
      

  4.   

    dobetterthatnthink,很感谢你再次帮我!可能我上面没有清楚要求吧,我要找出的记录其实就是effc_tm和expire_tm是同一区间内或者effc_tm和expire_tm这个时间有出现交叉的记录,换句话说,我上面举倒的记录其实都要查出来了,但是对于没有时间段出现交叉的记录则不查出来,但是很明显你给我的SQL语句里面查出来的话就算没有时间段交叉的记录也应该会被查出来的~~~.
      

  5.   

    不考虑别的,就再加一些条件即可。 此外这里的TO_DATE都是简单化了。
    正确的格式是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) 
      )
    )
      

  6.   

    select * from  table a 
    where exists
    (
        select * from table b
         where a.carrier_cd=b.carrier_cd
         and   a.exprie_time>=b.eff_time    
    )
      

  7.   

    select a.* from carriers a,carriers b
    where a.eff_time<=b.expire_time and a.eff_time>b.eff_time
      

  8.   

    to:jacobrong79老兄,你这个根本就行不通~~~~~
      

  9.   

    怎麼會行不通呢?是不是相同carrier_cd之間的比較?那就再加上a.carrier_cd=b.carrier_cd 不行嗎?
    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
      

  10.   

    to 19az:
    不是你理解能力差吧,应该是我没有说得很清楚.我再详细说一下吧:如表中有这样的数据:
     -----------  -----------------   ----------------------         
    |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不知道这样你看清楚了吗?
                     
      

  11.   

    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
    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