逐字段比较2表数据,只提取不匹配的行Oracle
Compare 2 tables data field by field and extract only unmatched rows Oracle
我有以下 2 tables,我只需要从 table A 中提取不匹配的行。如果 table A 中的任何列具有空值,那么我们不需要需要与 table 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,0,1)=1
or decode(a.col2,b.col2,0,1)=1
or decode(a.col3,b.col3,0,1)=1
or decode(a.col4,b.col4,0,1)=1
or decode(a.col5,b.col5,0,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' ,'test account' ,919722100948 from dual
)
,b(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' ,'Thakur_2' ,date'1990-08-05' ,'test account' ,919722100947 from dual union all
select 3,'Thakur_3' ,'Thakur_4' ,null ,'test account' ,919722100948 from dual
)
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
inner join b
on (a.id=b.id)
where
decode(a.col1,null,0,decode(a.col1,b.col1,0,1))=1
or decode(a.col2,null,0,decode(a.col2,b.col2,0,1))=1
or decode(a.col3,null,0,decode(a.col3,b.col3,0,1))=1
or decode(a.col4,null,0,decode(a.col4,b.col4,0,1))=1
or decode(a.col5,null,0,decode(a.col5,b.col5,0,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.
我有以下 2 tables,我只需要从 table A 中提取不匹配的行。如果 table A 中的任何列具有空值,那么我们不需要需要与 table 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,0,1)=1
or decode(a.col2,b.col2,0,1)=1
or decode(a.col3,b.col3,0,1)=1
or decode(a.col4,b.col4,0,1)=1
or decode(a.col5,b.col5,0,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' ,'test account' ,919722100948 from dual
)
,b(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' ,'Thakur_2' ,date'1990-08-05' ,'test account' ,919722100947 from dual union all
select 3,'Thakur_3' ,'Thakur_4' ,null ,'test account' ,919722100948 from dual
)
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
inner join b
on (a.id=b.id)
where
decode(a.col1,null,0,decode(a.col1,b.col1,0,1))=1
or decode(a.col2,null,0,decode(a.col2,b.col2,0,1))=1
or decode(a.col3,null,0,decode(a.col3,b.col3,0,1))=1
or decode(a.col4,null,0,decode(a.col4,b.col4,0,1))=1
or decode(a.col5,null,0,decode(a.col5,b.col5,0,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.