本帖最后由 wsg69454118 于 2011-06-21 16:10:54 编辑

解决方案 »

  1.   


    SQL> with t as(
      2       select 1 no,'a' name,'abc' ad from dual union all
      3       select 1,'a','cde' from dual union all
      4       select 1,'b','abc' from dual union all
      5       select 1,'c','abc' from dual union all
      6       select 2,'b','cde' from dual union all
      7       select 2,'d','cde' from dual)
      8  ,b as(
      9     select no,name,ad from (
     10            select no,name,ad,
     11                   row_number() over (partition by no order by no,ad) rn
     12            from t)
     13     where rn=1)
     14  ,c as(
     15     select distinct t.name from t
     16     where not exists(
     17           select 1 from b
     18           where t.name=b.name))
     19  select * from b
     20  union
     21  select null,name,null from c
     22  /
     
            NO NAME AD
    ---------- ---- ---
             1 a    abc
             2 b    cde
               c    
               d    
      

  2.   

    --oracle表是堆表,必须要有一个顺序,不能依赖自然顺序
    with t as (
      select 1 no, 'a' name, 'abc' addr from dual union all
      select 1 no, 'a' name, 'cde' addr from dual union all
      select 1 no, 'b' name, 'abc' addr from dual union all
      select 1 no, 'c' name, 'abc' addr from dual union all
      select 2 no, 'b' name, 'cde' addr from dual union all
      select 2 no, 'd' name, 'cde' addr from dual),
      a as (select rownum rn, t.* from t),
      c as (select * from a where not exists 
           (select * from a b where b.no=a.no and b.rn<a.rn))
     select no,name,addr from c union all
     select null,name,null from t where t.name not in (select c.name from c);