taba:
col1   col2   col3
001    HKD    01
001    HKD    01
001    HKD    01
002    USD    02
002    USD    02
003    AUD    03tabb:
col1   col2   col3
001    HKD    01
002    USD    02怎样用minus保留重复记录呢?
result tab:
col1   col2   col3
001    HKD    01
001    HKD    01
002    USD    02
003    AUD    03

解决方案 »

  1.   

    select t1.id,t1.id2,t1.id3,row_number() over(partition by id,id2,id3 order by id,id2,id3  ) rn from table1 t1 minus  select t2.id,t2.id2,t2.id3,row_number() over(partition by id,id2,id3 order by id,id2,id3) from table2 t2
      

  2.   

    select distinct * from taba
    union all
    select  * from tabb
    order by col1 , col2  ,col3 --如果tabb也存在重复
    select distinct * from taba
    union all
    select distinct * from tabb
    order by col1 , col2  ,col3 
      

  3.   

    select taba.*,row_number()over(partition by col1,col2,col3 order by rownum)rn
    from taba
    minus
    select tabb.*,row_number()over(partition by col1,col2,col3 order by rownum)rn
    from tabb
      

  4.   

    create table ta(id int);
    insert into ta select null from dual;
    insert into ta select 1 from dual;
    insert into ta select 1 from dual;
    insert into ta select null from dual;
    insert into ta select 2 from dual;create table tb as select * from ta where id=1;select a.*,row_number()over(partition by id order by id) as num from ta a
    minus
    select b.*,row_number()over(partition by id order by id) from tb b
      

  5.   

    MINUS 指令是运用在两个 SQL 语句上。它先找出第一个 SQL 语句所产生的结果,然后看这些结果有没有在第二个 SQL 语句的结果中。如果有的话,那这一笔资料就被去除,而不会在最后的结果中出现。如果第二个 SQL 语句所产生的结果并没有存在于第一个 SQL 语句所产生的结果内,那这笔资料就被抛弃。 
    MINUS 的语法如下: 
    [SQL 语句 1]
    MINUS
    [SQL 语句 2] 
    我们继续使用一样的例子: 
    Store_Information 表格 
    store_name  Sales  Date  
    Los Angeles  $1500  Jan-05-1999  
    San Diego  $250  Jan-07-1999  
    Los Angeles  $300  Jan-08-1999  
    Boston  $700  Jan-08-1999  
     
    Internet Sales 表格 Date  Sales  
    Jan-07-1999  $250  
    Jan-10-1999  $535  
    Jan-11-1999  $320  
    Jan-12-1999  $750  
     而我们要知道有哪几天是有店面营业额而没有网络营业额的。要达到这个目的,我们用以下的 SQL 语句: 
    SELECT Date FROM Store_Information
    MINUS
    SELECT Date FROM Internet_Sales 
    结果: 
    Date 
    Jan-05-1999 
    Jan-08-1999 "Jan-05-1999", "Jan-07-1999", and "Jan-08-1999" 是 "SELECT Date FROM Store_Information" 所产生的结果。在这里面,"Jan-07-1999" 是存在于 "SELECT Date FROM Internet_Sales" 所产生的结果中。因此 "Jan-07-1999" 并不在最后的结果中。 
    请注意,在 MINUS 指令下,不同的值只会被列出一次。