在网上找了很久很久的,可能有一天了吧,还是没有找到解决方案
希望各位看到了能够高抬贵手,帮帮我,虽然没有实物相赠,我从内心里面先感激各位了废话不多说了。。
问题是这样的我有两个表,比如第一个表为:
NAME 
---------------表1
A
A
A
B
B
C
第二个表的长相为
NMAE 
---------------表2
A
B
表2其实是表1 的一个子集 
我希望能够从数据库中,将两个表中能够查询到 类似于补集但是不是很像的结果,结果是
NAME
------------------
A
A
B
C
也就是表1中有的,表2中也有的,这一行就在结果集中去掉,不过只去掉一次再次先感谢各位大神了

解决方案 »

  1.   

    加个row_number就行了,例如:
     select ta.a,row_number() over(partition by a order by 1) rn from 
     (select 'A' a from dual
     union all
     select 'A' from dual
     union all
     select 'A' from dual
     union all
     select 'B' from dual
     union all
     select 'B' from dual
     union all
     select 'C' from dual
     ) ta  
     minus
    select tb.a,row_number() over(partition by a order by 1) from 
     (select 'A' a from dual
     union all
     select 'B' from dual
     ) tb
     ;
     或者
    select ta.*
    from (select a,row_number() over(partition by a order by 1) rn from 
     (select 'A' a from dual
     union all
     select 'A' from dual
     union all
     select 'A' from dual
     union all
     select 'B' from dual
     union all
     select 'B' from dual
     union all
     select 'C' from dual
     ) ) ta  left join (select a,row_number() over(partition by a order by 1) rn from 
     (select 'A' a from dual
     union all
     select 'B' from dual
     ) ) tb  on ta.a=tb.a and ta.rn=tb.rn
     where tb.a is null;
      

  2.   

    select name
      from tab1
     where col_pk not in (select min(col_pk) r
                                from tab1
                               inner join  tab2 
                                  on tab1.name = tab2.nmae
                                  group by tab1.name)说明:col_pk为tab1的主键字段,相信应该有