TableA TableB
PKey Col1 PKey Col1
---- ---- ---- ----
1 aaaa 1 bbbb
2 cccc 2 dddd
3 eeee 4 ffff
5 gggg
TableAにあるがTableBにない、PKeyの数と
TableBにあるがTableAにない、PKeyの数
を出力する。
出力結果
TableAOnly TablebOnly
---------- ----------
1 2
create table TableA( PKey number(3) primary key, Col1 varchar2(4)); create table TableB( PKey number(3) primary key, Col1 varchar2(4)); insert all into TableA(PKey,Col1) values(1,'aaaa') into TableB(PKey,Col1) values(1,'bbbb') into TableA(PKey,Col1) values(2,'cccc') into TableB(PKey,Col1) values(2,'dddd') into TableA(PKey,Col1) values(3,'eeee') into TableB(PKey,Col1) values(4,'ffff') into TableB(PKey,Col1) values(5,'gggg') select 1 from dual; commit;
select count(a.PKey) as TableAOnly,
count(b.PKey) as TablebOnly
from TableA A full join TableB b
on (a.PKey = b.PKey)
where a.PKey is null
or b.PKey is null;
TableAとTableBの和集合と、 TableAとTableBの共通集合との 差集合のそれぞれの要素数を求めてます。 脳内でベン図を書いて (A∪B)-(A∩B) をイメージすると、分かりやすいでしょう。