问题描述
我有以下 2 个表,我只需要从表 A 中提取不匹配的行。如果表 A 中的任何列具有空值,那么我们不需要将相同的列与表 B 列进行比较。我怎样才能达到 A-B 的结果?。
TableA 减去 TableB 给出更接近的结果,但我需要在比较时检查空约束。
此外,由于数据是数百万,我需要执行一些批量操作。请帮助我以最好的方式做同样的事情。
解决方法
select
a.id as a_id,b.id as b_id,a.col1 as a_col1,b.col1 as b_col1,a.col2 as a_col2,b.col2 as b_col2,a.col3 as a_col3,b.col3 as b_col3,a.col4 as a_col4,b.col4 as b_col4,a.col5 as a_col5,b.col5 as b_col5
from a
full outer join b
on (b.id=a.id)
where
decode(a.col1,b.col1,1)=1
or decode(a.col2,b.col2,1)=1
or decode(a.col3,b.col3,1)=1
or decode(a.col4,b.col4,1)=1
or decode(a.col5,b.col5,1)=1
带有测试数据的完整示例:
with
a(id,col1,col2,col3,col4,col5) as (
select 1,'Testcase42','Testcase43',date'1987-07-03','test account',919599636744 from dual union all
select 2,'Thakur_1','',date'1990-08-05',919722100947 from dual union all
select 3,'Thakur_3','Thakur_4',date'1995-12-05',919722100948 from dual
),b(id,'Thakur_2',null,919722100948 from dual
)
select
a.id as a_id,b.col5 as b_col5
from a
inner join b
on (a.id=b.id)
where
decode(a.col1,decode(a.col1,1))=1
or decode(a.col2,decode(a.col2,1))=1
or decode(a.col3,decode(a.col3,1))=1
or decode(a.col4,decode(a.col4,1))=1
or decode(a.col5,decode(a.col5,1))=1;
A_ID B_ID A_COL1 B_COL1 A_COL2 B_COL2 A_COL3 B_COL3 A_COL4 B_COL4 A_COL5 B_COL5
---------- ---------- ---------- ---------- ---------- ---------- ------------------- ------------------- ------------ ------------ ---------- ----------
3 3 Thakur_3 Thakur_3 Thakur_4 Thakur_4 1995-12-05 00:00:00 test account test account 9.1972E+11 9.1972E+11
1 row selected.