A表
ID        TName            RpCode  Rmoced
01        0010             
02        0020                 
03        0010              ABS     
04        0070                   
05        0070              ABS    
06        0080              ABS  
07        0090                
保留的数据
ID        TName            RpCode  Rmoced
02        0020                 
03        0010              ABS     
05        0070              ABS   
06        0080              ABS  
07        0090      就是根据TName分组,如果发现分组的数据中的有2条以上的数据,看RpCode的值是否空,如果空那么就过滤掉
上面就过虑了
01        0010   
04        0070  

解决方案 »

  1.   

    搞不懂 02 和 07 的 RpCode 不也是空,为什么过滤掉?
      

  2.   

    select * from tb where RpCode is not null
    union all
    select * from tb A where RpCode is null and notexists (select 1 from tb B where A.TName=B.Tname and A.id<>B.id)
      

  3.   

    因为02 07按Tname分组 只有一行记录
      

  4.   

    LZ 数据没给全 这个看不懂  大概就是 group + having 吧应该就能搞定
      

  5.   

    原来是 tname 分组啊,看花了,以为是 id 分组。
      

  6.   

    select * from A表 t
    where RpCode is not null
    and not exists (select 1 from A表 where TName=t.TName and ID!=t.ID)