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
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)
where RpCode is not null
and not exists (select 1 from A表 where TName=t.TName and ID!=t.ID)