表:
0101 1
0985 0
0985 1
0985 1
1133 0
1385 0
1387 0
是否可以用SQL得出:
0101 1
0985 1
1133 0
1385 0
1387 0
如果第一列相同(例如0985),第二列若有一个记录是1,则取1(例如0985),谢谢指点!

解决方案 »

  1.   

    select col1, max(val) from table1 group by col1;
      

  2.   

    with temp as(
    select '0101' id,1 num from dual
    union all
    select '0985' id,0 num from dual
    union all
    select '0985' id,1 num from dual
    union all
    select '0985' id,1 num from dual
    union all
    select '1133' id,0 num from dual
    union all
    select '1385' id,0 num from dual
    union all
    select '1387' id,0 num from dual
    )
    select id,num from (
    select id,num,row_number() over(partition by id order by num desc) rn from temp
    ) where rn = 1
      

  3.   

    SQL> ed
    已写入 file afiedt.buf  1  with temp as(
      2  select '0101' id,1 num from dual
      3  union all
      4  select '0985',0 from dual
      5  union all
      6  select '0985',1 from dual
      7  union all
      8  select '0985',1 from dual
      9  union all
     10  select '1133',0 from dual
     11  union all
     12  select '1385',0 from dual
     13  union all
     14  select '1387',0 from dual
     15  )
     16* select id,max(num) from temp group by id
    SQL> /ID     MAX(NUM)
    ---- ----------
    0101          1
    1387          0
    0985          1
    1133          0
    1385          0
      

  4.   

    --直接来
    select num,max(id) from tb group by num