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
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
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
from taba
minus
select tabb.*,row_number()over(partition by col1,col2,col3 order by rownum)rn
from tabb
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
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 指令下,不同的值只会被列出一次。