简体   繁体   English

如何使用纯SQL获取增量记录?

[英]How to get delta records using pure SQL?

I have two tables T_Person and T_Person_New in Oracle SQL. 我在Oracle SQL中有两个表T_Person和T_Person_New。

在此处输入图片说明

For ease, lets take Name as the unique identifier of both tables. 为方便起见,让Name作为两个表的唯一标识符。

Can i Compare both tables to get the delta records using an SQL query? 我可以比较两个表以使用SQL查询获取增量记录吗?

The delta records should consist of the following condition: 增量记录应包含以下条件:

  1. If it is a change in an existing record. 如果是现有记录中的更改。 Ie a change in DOB / Gender / Name 即DOB /性别/名称的更改
  2. If its a new record. 如果是新纪录。

Thanks. 谢谢。

We can try using an EXISTS clause here: 我们可以在这里尝试使用EXISTS子句:

SELECT ID, Name, DOB, Gender
FROM T_Person_New t1
WHERE NOT EXISTS (SELECT 1 FROM T_Person t2
                  WHERE t1.Name = t2.Name AND t1.DOB = t2.DOB AND t1.Gender = t2.Gender);

The logic here is to return every new record for which we cannot find an exactly matching record in the original table. 这里的逻辑是返回每条新记录,我们无法在原始表中找到与其完全匹配的记录。 This covers the case that the person already exists, but one or more of the fields have changed. 这涵盖了该人已经存在但一个或多个字段已更改的情况。 And it also covers the case where the person is completely new, and did not even exist previously. 它也涵盖了这个人是完全新的,甚至以前不存在的情况。

I have added one more record each in your sample data for old (t_person) and new (t_person_new) tables to cover for missing records from either tables. 我在您的样本数据中为旧表(t_person)和新表(t_person_new)分别添加了一条记录,以弥补这两个表中缺少的记录。

I assume that id column is primary key in both tables (it's not clear from you description although you did mention name is unique). 我假设id列是两个表中的主键(虽然您提到名称是唯一的,但从您的描述中并不清楚)。

old table sample data 旧表样本数据

insert into t_person values (1, 'Tom', '2000-01-01', 'M');
insert into t_person values (2, 'Gary', '2000-01-01', 'M');
insert into t_person values (3, 'Pam', '2000-01-01', 'F');
insert into t_person values (4, 'Hans', '2000-01-01', 'M');

new table sample data 新表样本数据

insert into t_person_new values (1, 'Tom', '2000-01-01', 'M');
insert into t_person_new values (2, 'Gary', '2001-01-01', 'F');
insert into t_person_new values (3, 'Pamela', '2000-01-01', 'F');
insert into t_person_new values (5, 'Jane', '2000-01-02', 'F');

Here is a query that could show you all possible differences. 这是一个查询,可以显示所有可能的差异。 I have done it only on name column, you can expand it for all columns. 我仅在名称列上完成了此操作,您可以将其扩展到所有列。

select case when t.id is null then 'NEW: MISSING-FROM-OLD' 
            else case when tn.id is null then 'DELETED: MISSING-FROM-NEW' 
                      else 'EXISTS-IN-BOTH' 
                 end 
       end record_type    
       ,case when tn.name is null then 'MISSING-FROM-NEW, VALUE-IN-OLD (' + t.name + ')'
        else case when t.name is null then 'MISSING-FROM-OLD, VALUE-IN-NEW (' + tn.name + ')'
             else case when t.name = tn.name then 'SAME-IN-BOTH (' + t.name +')'
                  else 'CHANGED, VALUE-IN-OLD (' + t.name + '), VALUE-IN-NEW (' + tn.name +')' 
                 end 
            end 
       end name_state       
from t_person_new tn
full outer join t_person t on tn.id = t.id

Note: for Oracle you will have to use '||' 注意:对于Oracle,您必须使用'||' instead of '+' to concatenate. 而不是“ +”进行连接。 I used '+' as I have SQL Server 我使用SQL Server时使用了'+'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM