|
首先我们创建表并插入数据:
- Create table A(A1 number(12),A2 varchar2(50));
- Create table B(B1 number(12),B2 varchar2(50));
- Insert Into A Values (1,'a');
- Insert Into A Values (2,'ba');
- Insert Into A Values (3,'ca');
- Insert Into A Values (4,'da');
- Insert Into B Values (1,'a');
- Insert Into B Values (2,'bba');
- Insert Into B Values (3,'ca');
- Insert Into B Values (5,'dda');
- Insert Into B Values (6,'Eda');
复制代码 假设表A为原始表,表B为比较表:
--原始表A与比较表B的增量差异(原始表A有、比较表B没有)
Select * from A minus select * from B;
--比较表B与原始表A的增量差异(比较表B有、原始表A没有)
Select * from B minus select * from A;
--两种增量差异的合集:此合集包含3类数据
--1、原始表A存在、比较表B不存在,属于删除类数据,出现次数1
--2、原始表A不存在、比较表B存在,属于新增类数据,出现次数1
--3、原始表A和比较表B都存在,属于修改类数据,出现次数2
Select A1,A2,1 t from (Select * from A minus select * from B) union
Select B1,B2,2 t from (Select * from B minus select * from A);
最后,实现判别出比较表相对于原始表是进行了“插入”、“修改”、“删除”的情况
--SUM(T)为1的为“删除”的数据,SUM(T)为2的为“新增”的数据,SUM(T)为3的为“修改”的数据
Select A1, sum(t)
from (Select A1, A2, 1 t
from (Select * from A minus select * from B)
union
Select B1, B2, 2 t
from (Select * from B minus select * from A))
Group by A1;
|
|