字段1   字段2
36        0
36        0
36        0
36        78
36        78
36        78
36        43要求SQL语句把 字段2 不等于0的重复记录过滤输出。如果如下:字段1   字段2
36        0
36        0
36        0
36        78
36        43
  

解决方案 »

  1.   

    select * from tb where ziduan2=0
    union all
    select distinct * from tb where ziduan2=0 
      

  2.   

    select * from tb where ziduan2=0
    union all
    select distinct * from tb where ziduan2!=0 
      

  3.   


    declare @table table(字段1 int,字段2 int)
    insert into @table
    select 36 ,0 union all
    select 36 ,0 union all
    select 36 ,0 union all
    select 36 ,78 union all
    select 36 ,78 union all
    select 36 ,78 union all
    select 36 ,43select * from @table where 字段2=0
    union all
    select distinct * from @table where 字段2<>0字段1         字段2
    ----------- -----------
    36          0
    36          0
    36          0
    36          43
    36          78(5 行受影响)
      

  4.   


    如果其它字段有不同的值,这个查询就无效了啊!
    如:
    字段1 字段2 字段3
    123   36    0
    456   36    0
    789   36    66
    654   36    66
    fdd   36    78