[英]How to compare three colums between source and target tables during merge or update operation
I have two tables, contribution(Member_number,Salyear,Salmonth,ReceiptDate)
and contribution_update(Member_number,Salyear,Salmonth,ReceiptDate)
. 我有两个表, contribution(Member_number,Salyear,Salmonth,ReceiptDate)
和contribution_update(Member_number,Salyear,Salmonth,ReceiptDate)
。 The column Receiptdate
in the table contribution
has null values. 表格contribution
的列Receiptdate
具有空值。 I want to update the column by comparing a combination of Member_Number
, Salyear
, Salmonth
and where matches occur the Receiptdate
gets updated. 我想通过比较Member_Number
, Salyear
, Salmonth
的组合来更新列,以及匹配发生的地方, Receiptdate
会更新。
I wrote the following code but it merges o values. 我编写了以下代码,但它合并了o值。
anyone with the idea on how to improve or rewrite the script. 任何有关如何改进或重写脚本的想法的人。
i started with this code 我开始使用这段代码
MERGE INTO CONTRIBUTION cgt
USING (select MEMBER_NUMBER,SALYEAR,SALMONTH,RECEIVED_DATE from CONTRIBUTION_UPDATE )cga
ON (cgt.MEMBER_NUMBER=cga.MEMBER_NUMBER AND cgt.SALYEAR=cga.SALYEAR AND cgt.SALMONTH=cga.SALMONTH)
WHEN matched then
update
SET cgt.RECEIPTDATE=cga.RECEIVED_DATE;
then I discovered that the table contribution_update
has duplicates (Gor message unable to get stable set of rows..). 然后我发现表contribution_update
有重复(Gor消息无法获得稳定的行集......)。 I recreated the table with no duplicates in it. 我重新创建了表,没有重复。
MERGE INTO Contribution M
USING
(Select Member_Number,Salyear,Salmonth,ReceiptDate From
(select MEMBER_NUMBER,SALYEAR,SALMONTH,RECEIptDATE,row_number() over (partition by MEMBER_NUMBER,SALYEAR,SALMONTH
order by null ) as qry from Contribution_update)where qry=1) vu
ON (Vu.Member_Number = M.Member_Number and M.Salyear=Vu.Salyear and M.Salmonth=Vu.Salmonth )
WHEN MATCHED
THEN
UPDATE
SET M.Receiptdate = Vu.ReceiptDate;
WHERE M.Salyear=Vu.Salyear and M.Salmonth=Vu.Salmonth;
THE query is supposed to merge a number of rows in the target table. 该查询应该合并目标表中的多个行。
You don't need that WHERE
condition on the UPDATE
part of your MERGE
, it's taken care of by ON
conditions. 您在MERGE
的UPDATE
部分不需要WHERE
条件,它由ON
条件处理。 The WHEN MATCHED
statement will only execute for matching rows - so the rows where MEMBER_NUMBER
, SALYEAR
and SALMONTH
are the same between source and target. WHEN MATCHED
语句仅对匹配行执行 - 因此MEMBER_NUMBER
, SALYEAR
和SALMONTH
在源和目标之间的行相同。
Let's assume source - ContributionUpdate
looks like this: 让我们假设源 - ContributionUpdate
看起来像这样:
MEMBER_NUMBER SALYEAR SALMONTH RECEIVED_DATE
------------- ----------- ----------- -------------
1 2019 1 2019-10-10
2 2019 1 2019-10-20
And target - Contribution
like this: 目标 - 像这样的Contribution
:
MEMBER_NUMBER SALYEAR SALMONTH RECEIVED_DATE
------------- ----------- ----------- -------------
1 2019 1 NULL
There's one matching row in the target, for member 1, year 2019, month 1. If you want to just update the matching years with RECEIVED_DATE
from the source, this is enough: 对于成员1,2019年,第1个月,目标中有一个匹配的行。如果您只想从源更新RECEIVED_DATE
的匹配年份,这就足够了:
MERGE INTO Contribution t
USING ContributionUpdate s
ON s.MEMBER_NUMBER = t.MEMBER_NUMBER
AND s.SALYEAR = t.SALYEAR
AND s.SALMONTH = t.SALMONTH
WHEN MATCHED
THEN UPDATE
SET RECEIVED_DATE = s.RECEIVED_DATE;
(1 row affected)
If you wanted to also insert the one missing row from source into target (for member 2), you'd need to use WHEN NOT MATCHED BY TARGET
, like so: 如果你还想将一个缺失的行从源插入目标(对于成员2),你需要使用WHEN NOT MATCHED BY TARGET
,如下所示:
MERGE INTO Contribution t
USING ContributionUpdate s
ON s.MEMBER_NUMBER = t.MEMBER_NUMBER
AND s.SALYEAR = t.SALYEAR
AND s.SALMONTH = t.SALMONTH
WHEN MATCHED
THEN UPDATE
SET RECEIVED_DATE = s.RECEIVED_DATE
WHEN NOT MATCHED BY TARGET
THEN INSERT
(
MEMBER_NUMBER,
SALYEAR ,
SALMONTH ,
RECEIVED_DATE
)
VALUES
(
s.MEMBER_NUMBER,
s.SALYEAR ,
s.SALMONTH ,
s.RECEIVED_DATE
);
(2 rows affected)
And as a result, the Contribution
table will look like: 结果, Contribution
表将如下所示:
MEMBER_NUMBER SALYEAR SALMONTH RECEIVED_DATE
------------- ----------- ----------- -------------
2 2019 1 2019-10-20
1 2019 1 2019-10-10
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.