select * from tb a
    where   exists (select 1 from tb where bh=a.bh 
                                and rq=a.rq and 
                                        xh=a.xh and 
   convert(char(10),sj,120)=convert(char(10),a.sj,120)
                      and datepart(hh,sj)=datepart(hh,a.sj) 
                            and datepart(mi,sj)=datepart(mi,a.sj) 
                                 and   datepart(ss,sj)-datepart(ss,a.sj)<60
                                        and sj>a.sj)

解决方案 »

  1.   

    select * into #tt from tb a
        where not exists (select 1 from tb where bh=a.bh 
                                    and rq=a.rq and 
                                            xh=a.xh and 
       convert(char(10),sj,120)=convert(char(10),a.sj,120)
                          and datepart(hh,sj)=datepart(hh,a.sj) 
                                and datepart(mi,sj)=datepart(mi,a.sj) 
                                     and   datepart(ss,sj)-datepart(ss,a.sj)<60
                                            and sj>a.sj)truncate table tbinsert into tb select * from #tt
      

  2.   

    --删除
    delete a from tb a
    where exists(
    select * from tb 
    where bh=a.bh and rq=a.rq and xh=a.xh
    and sj>dateadd(minute,-1,a.sj)
    and sj<a.sj)
      

  3.   

    --测试--测试数据
    create table tb(bh int,rq datetime,sj datetime,xh int)
    insert tb select 1,'2004-12-26 00:00:00','1900-01-01 07:28:13',3
    union all select 1,'2004-12-26 00:00:00','1900-01-01 07:28:14',3
    union all select 1,'2004-12-26 00:00:00','1900-01-01 07:28:16',3
    union all select 1,'2004-12-26 00:00:00','1900-01-01 07:30:15',4
    union all select 1,'2004-12-26 00:00:00','1900-01-01 17:28:17',5
    union all select 1,'2004-12-26 00:00:00','1900-01-01 17:28:18',5
    union all select 1,'2004-12-26 00:00:00','1900-01-01 17:28:19',6
    union all select 1,'2004-12-26 00:00:00','1900-01-01 17:28:30',6
    union all select 2,'2004-12-27 00:00:00','1900-01-01 08:28:13',3
    union all select 2,'2004-12-27 00:00:00','1900-01-01 12:28:16',3
    union all select 2,'2004-12-27 00:00:00','1900-01-01 08:32:14',3
    union all select 2,'2004-12-27 00:00:00','1900-01-01 12:30:15',4
    union all select 2,'2004-12-27 00:00:00','1900-01-01 13:28:17',5
    union all select 2,'2004-12-27 00:00:00','1900-01-01 13:28:18',5
    union all select 2,'2004-12-27 00:00:00','1900-01-01 17:28:19',6
    union all select 2,'2004-12-27 00:00:00','1900-01-01 17:28:30',6
    go--删除
    delete a from tb a
    where exists(
    select * from tb 
    where bh=a.bh and rq=a.rq and xh=a.xh
    and sj>dateadd(minute,-1,a.sj)
    and sj<a.sj)--显示删除结果
    select * from tb
    go--删除测试
    drop table tb/*--测试结果bh    rq                       sj                       xh    
    ----- ------------------------ ------------------------ ----
    1     2004-12-26 00:00:00.000  1900-01-01 07:28:13.000  3
    1     2004-12-26 00:00:00.000  1900-01-01 07:30:15.000  4
    1     2004-12-26 00:00:00.000  1900-01-01 17:28:17.000  5
    1     2004-12-26 00:00:00.000  1900-01-01 17:28:19.000  6
    2     2004-12-27 00:00:00.000  1900-01-01 08:28:13.000  3
    2     2004-12-27 00:00:00.000  1900-01-01 12:28:16.000  3  --这条不应该删除,已超过几小时了
    2     2004-12-27 00:00:00.000  1900-01-01 08:32:14.000  3
    2     2004-12-27 00:00:00.000  1900-01-01 12:30:15.000  4
    2     2004-12-27 00:00:00.000  1900-01-01 13:28:17.000  5
    2     2004-12-27 00:00:00.000  1900-01-01 17:28:19.000  6(所影响的行数为 10 行)
    --*/