{"id":109,"date":"2020-12-24T10:14:11","date_gmt":"2020-12-24T00:14:11","guid":{"rendered":"http:\/\/data-engineer.net\/?p=109"},"modified":"2023-02-04T18:10:23","modified_gmt":"2023-02-04T08:10:23","slug":"using-merge-for-scd-type-2-with-multiple-updates","status":"publish","type":"post","link":"https:\/\/data-engineer.net\/?p=109","title":{"rendered":"Using MERGE for SCD Type 2 with multiple updates"},"content":{"rendered":"\n<p>Below is the typical MERGE statement for SCD Type 2:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>insert into dest (a, b)\nselect a, b\nfrom\n(\n  merge dest as target\n  using src as source\n  on source.a = target.a\n  when not matched by target\n    then insert (a,b) values(a,b)\n  when matched\n    then update set target.a = source.a, target.b = source.b\n  output $Action as my_action\n) mrg\nwhere mrg.my_action = 'UPDATE'\n;<\/code><\/pre>\n\n\n\n<p>Since a MERGE statement cannot update more than one match at a time, in scenarios which the source contains historical data that for each matching key (per ON statament) may need more than 1 action in a single merge statement run (e.g. one INSERT and multiple UPDATEs), above statement will not work properly.<\/p>\n\n\n\n<p>As a work around you can rank the source for multiple singular actions and put merge statement in a loop. Then every INSERT\/UPDATE action will run correctly per each MERGE run<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>select a,b, rownumber() over(partition by a order by b) as rank\ninto #src\nfrom src\n\ndeclare @counter int = 1\ndeclare @max_counter int = (select max(rank) from #src)\n\nwhile @counter &lt;= @max_counter\nbegin\n  insert into dest (a, b)\n  select a, b\n  from\n  (\n    merge dest as target\n    using <strong>(select a,b, from #src where rank = @counter)<\/strong> as source\n    on source.a = target.a\n    when not matched by target\n      then insert (a,b) values(a,b)\n    when matched\n      then update set target.a = source.a, target.b = source.b\n    output $Action as my_action\n  ) mrg\n  where mrg.my_action = 'UPDATE'\n  ;\n  set @counter = @counter + 1\nend<\/code><\/pre>\n\n\n\n<p>Note the bold snippet in above is replaced with the previous &#8220;src&#8221;.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Below is the typical MERGE statement for SCD Type 2: Since a MERGE statement cannot update more than one match at a time, in scenarios which the source contains historical data that for each matching key (per ON statament) may need more than 1 action in a single merge statement run (e.g. one INSERT and &#8230; <a title=\"Using MERGE for SCD Type 2 with multiple updates\" class=\"read-more\" href=\"https:\/\/data-engineer.net\/?p=109\" aria-label=\"More on Using MERGE for SCD Type 2 with multiple updates\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[40,38,39,9],"_links":{"self":[{"href":"https:\/\/data-engineer.net\/index.php?rest_route=\/wp\/v2\/posts\/109"}],"collection":[{"href":"https:\/\/data-engineer.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-engineer.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-engineer.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/data-engineer.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=109"}],"version-history":[{"count":1,"href":"https:\/\/data-engineer.net\/index.php?rest_route=\/wp\/v2\/posts\/109\/revisions"}],"predecessor-version":[{"id":110,"href":"https:\/\/data-engineer.net\/index.php?rest_route=\/wp\/v2\/posts\/109\/revisions\/110"}],"wp:attachment":[{"href":"https:\/\/data-engineer.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=109"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-engineer.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=109"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-engineer.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=109"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}