我通过一串SELECT语句得到一下结果: 
    
    订单号      行号      商品代码        数量        单价        标识 
     123      001        14021        200        20.12      1 
    123      001        14021        200        20.12      4 
    215      001        14021        200        20.00      1 
    215      002        14021        500        10.18      1 
    215      003        14021        212        20.12      1 
    111      001        14021        200        20.12      4 
    111      001        14021        200        20.12      1 
    200      001        14021        200        20.12      4  
    200      001        14021        200        20.12      1 我现在的目的是把 行号 和 订单号相同的记录中标识是 4 的记录删除掉(注意有的标识是4,但没有重复记录,也要保留)
,保留其他的记录
   我的要求是SELECT的是由筛选掉重复的且标识为4的记录,而不是用DELETE,
谢谢各位大哥!!!!

解决方案 »

  1.   

    select distinct 订单号, 行号 from table_name where 标识=4是不是这个意思呢?
      

  2.   


    select *from table where 标识='4'
    and exists(select *from 
    (select 订单号,行号 from table group by 订单号,行号 having count(*)>1) as t
    where t.订单号=table.订单号 and t.行号=table.行号
    )
      

  3.   


    --test 
    create table #3 (订单号 varchar(10),      行号  varchar(10),     商品代码  varchar(20),      数量 int,       单价   numeric(12,2),  标识 int )
     insert into #3 insert into #3 select '123','001','14021',200,20,1insert into #3 select '123','001','14021',200,20,4insert into #3 select '215','001','14021',200,20,1insert into #3 select '215','002','14021',200,20,1insert into #3 select '215','003','14021',200,20,1insert into #3 select '111','001','14021',200,20,1insert into #3 select '111','001','14021',200,20,4insert into #3 select '200','001','14021',200,20,1insert into #3  select '200','001','14021',200,20,4 
    select *from #3 where 标识='4'
    and exists(select *from
    (select 订单号,行号 from #3 group by 订单号,行号 having count(*)>1) as t
    where t.订单号=#3.订单号 and t.行号=#3.行号
      

  4.   

    如果没有 null 值 select *  from tab a
    where  not exists ( select * from 
    (select   订单号,行号 from tab
    group by 订单号,行号
    having count(*) >1 )x 
    where x.订单号=a.订单号 and x.行号=a.订单号 and a.标时=4)
      

  5.   

    用 distinct  去掉重复的select * from table a where 标示<>4
    union
    select distinct * from table b where 标示=4
      

  6.   

    看lz的数据都是重复的,仅仅标识列不一样。否则就这样就可以了
    select a.* from t a
    where exists
    (select 订单号,行号
     from t b
     where b.=a.订单号 and b.行号=a.行号
    group by 订单号,行号 having count(*)>1) and a.标识='4'