A      B
001    a
002    b
002    0.1
003    0.1规则:
表中,如果对于一个A(比如002)如果对应两个B列的值(b和0.1),那么取出B列中不等于0.1的那条。
最后结果应该为
A      B
001    a
002    b
003    0.1sql语句怎么写?
谢谢

解决方案 »

  1.   


    WITH tab AS(
    SELECT '001'a, 'a' b FROM dual UNION ALL
    SELECT '002', 'b' FROM dual UNION ALL 
    SELECT '002', '0.1' FROM dual UNION ALL
    SELECT '003', '0.1' FROM dual
    )
    SELECT a,b FROM (
    SELECT a,b,Decode(b,'0.1',1,0) bb,Count(1)over(PARTITION BY a) cnt FROM tab
    )
    WHERE cnt=1 OR (cnt!=1 AND bb=0)
      

  2.   

    SQL> edi
    已写入 file afiedt.buf  1  with tb as
      2  (
      3  select '001' id,'a' value from dual union all
      4  select '002','b' from dual union all
      5  select '002','0.1' from dual union all
      6  select '003','0.1' from dual)
      7  select id,value
      8  from (select id,value,row_number() over(partition by id order by length(value)) rn
      9  from tb) a
     10* where rn=1
    SQL> /ID  VAL
    --- ---
    001 a
    002 b
    003 0.1
      

  3.   

    select * from tb t
    where (select count(*) from tb where t.a=a and t.b='0.1')<2;
      

  4.   

    SQL> select * from tt;
     
    A                                                                                B
    -------------------------------------------------------------------------------- --------------------------------------------------------------------------------
    001                                                                              a
    002                                                                              b
    002                                                                              0.1
    003                                                                              0.1
     
    SQL> select * from tt a where b<>'0.1' or (b='0.1' and not exists(select 1 from tt b where a.a=b.a and b.b<>'0.1'));
     
    A                                                                                B
    -------------------------------------------------------------------------------- --------------------------------------------------------------------------------
    001                                                                              a
    002                                                                              b
    003                                                                              0.1
     
    SQL> 
      

  5.   


    用length来控制不够健壮,但是就这个问题来说是可以实现的。但是如果修改002的B列的值为bbbb,那么就无法取到002里B列不是0.1的那条记录了。