查询具有多个数据加载的单个 table 中的更改并设置 mutationcode 标识符的方法

method to query for changes in single table with multiple data loads and set the mutationcode identifier

我正在寻找最好的 SQL 服务器方法来比较 table 中的数据,如果与之前的加载存在差异,则用代码 01 标记字段 mutation_code。

如您所见,eric 和 lucy 在 table 中存在差异,因此已将它们标记为突变代码 01。实现此目的需要什么查询?

Table

loadno, firstname, lastname, street, streetno, mutationcode
load1, eric, smith, sophostreet, 42 
load1, mark, kras, downtownstreet, 10
load1, lucy, polka, newamsterdam, 466
load2, eric, smith, sophostreet, 43, 01
load2, mark, kras, townstreet, 10
load2, lucy, polka, newamsterdam, 466, 01

期待您的建议,祝好,

如果您的 table 只有两个 loadno 值,则类似以下内容可能会起作用:

UPDATE r1
SET r1.mutationcode = 1
FROM record as r1
join record as r2 on r2.loadno < r1.loadno
                  and r2.firstname = r1.firstname
                  and r2.lastname = r1.lastname
WHERE r2.street != r1.street
   or r2.streetno != r1.streetno
   ;