<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How to perform UPCERTS and  DELETE missing records from Target table using MERGE command? in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/how-to-perform-upcerts-and-delete-missing-records-from-target/m-p/19812#M13341</link>
    <description>&lt;P&gt;the WHEN NOT MATCHED clause can only contain an INSERT action.&lt;/P&gt;&lt;P&gt;So if you want to delete records, you should catch those records in the WHEN MATCHED clause.&lt;/P&gt;&lt;P&gt;If you have a record indicator (insert, update, delete) that is possible.&lt;/P&gt;</description>
    <pubDate>Wed, 25 May 2022 07:10:12 GMT</pubDate>
    <dc:creator>-werners-</dc:creator>
    <dc:date>2022-05-25T07:10:12Z</dc:date>
    <item>
      <title>How to perform UPCERTS and  DELETE missing records from Target table using MERGE command?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-perform-upcerts-and-delete-missing-records-from-target/m-p/19811#M13340</link>
      <description>&lt;P&gt;Hi have the following case. I want to perform upcert operation. But along with upcert, I want to delete the records which are missing in source table, but present in the target table. You can think it as a master data update.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;Source table contains a full set of master data. This is the latest incoming data.&lt;/LI&gt;&lt;LI&gt;Target table contains the full set of master data. This is old data present in the current database.&lt;/LI&gt;&lt;LI&gt;The source may contains new records, updates to some existing records, or some records might have removed compared to the target.&lt;/LI&gt;&lt;LI&gt;So during MERGE operation, I want to update the matching records in target from source, Insert new incoming records from the source, and delete the records from target which were not present in the source. How to achieve this with databricks MERGE?&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I see that the similar operation is possible in&amp;nbsp;&lt;A href="https://www.mssqltips.com/sqlservertip/1704/using-merge-in-sql-server-to-insert-update-and-delete-at-the-same-time/" alt="https://www.mssqltips.com/sqlservertip/1704/using-merge-in-sql-server-to-insert-update-and-delete-at-the-same-time/" target="_blank"&gt;MYSQL Server&lt;/A&gt;, as shown below. But Databricks SQL doesn't support "BY SOURCE" option.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;--Synchronize the target table with refreshed data from source table&lt;/P&gt;&lt;P&gt;MERGE Products AS TARGET&lt;/P&gt;&lt;P&gt;USING UpdatedProducts AS SOURCE&lt;/P&gt;&lt;P&gt;ON (TARGET.ProductID = SOURCE.ProductID)&lt;/P&gt;&lt;P&gt;--When records are matched, update the records if there is any change&lt;/P&gt;&lt;P&gt;WHEN MATCHED AND TARGET.ProductName &amp;lt;&amp;gt; SOURCE.ProductName OR TARGET.Rate &amp;lt;&amp;gt; SOURCE.Rate&lt;/P&gt;&lt;P&gt;THEN UPDATE SET TARGET.ProductName = SOURCE.ProductName, TARGET.Rate = SOURCE.Rate&lt;/P&gt;&lt;P&gt;--When no records are matched, insert the incoming records from source table to target table&lt;/P&gt;&lt;P&gt;WHEN NOT MATCHED BY TARGET&lt;/P&gt;&lt;P&gt;THEN INSERT (ProductID, ProductName, Rate) VALUES (SOURCE.ProductID, SOURCE.ProductName, SOURCE.Rate)&lt;/P&gt;&lt;P&gt;--When there is a row that exists in target and same record does not exist in source then delete this record target&lt;/P&gt;&lt;P&gt;WHEN NOT MATCHED BY SOURCE&lt;/P&gt;&lt;P&gt;THEN DELETE&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;Krishna&lt;/P&gt;</description>
      <pubDate>Tue, 24 May 2022 15:14:48 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-perform-upcerts-and-delete-missing-records-from-target/m-p/19811#M13340</guid>
      <dc:creator>Krish-685291</dc:creator>
      <dc:date>2022-05-24T15:14:48Z</dc:date>
    </item>
    <item>
      <title>Re: How to perform UPCERTS and  DELETE missing records from Target table using MERGE command?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-perform-upcerts-and-delete-missing-records-from-target/m-p/19812#M13341</link>
      <description>&lt;P&gt;the WHEN NOT MATCHED clause can only contain an INSERT action.&lt;/P&gt;&lt;P&gt;So if you want to delete records, you should catch those records in the WHEN MATCHED clause.&lt;/P&gt;&lt;P&gt;If you have a record indicator (insert, update, delete) that is possible.&lt;/P&gt;</description>
      <pubDate>Wed, 25 May 2022 07:10:12 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-perform-upcerts-and-delete-missing-records-from-target/m-p/19812#M13341</guid>
      <dc:creator>-werners-</dc:creator>
      <dc:date>2022-05-25T07:10:12Z</dc:date>
    </item>
    <item>
      <title>Re: How to perform UPCERTS and  DELETE missing records from Target table using MERGE command?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-perform-upcerts-and-delete-missing-records-from-target/m-p/19813#M13342</link>
      <description>&lt;P&gt;Any other option to DELETE with single MERGE command, where in we can perform, all insert, update, delete operations. Delete is mainly to delete any stale data from the target table. Record indicator, means a extra column in the database schema?&lt;/P&gt;</description>
      <pubDate>Wed, 25 May 2022 12:16:06 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-perform-upcerts-and-delete-missing-records-from-target/m-p/19813#M13342</guid>
      <dc:creator>Krish-685291</dc:creator>
      <dc:date>2022-05-25T12:16:06Z</dc:date>
    </item>
    <item>
      <title>Re: How to perform UPCERTS and  DELETE missing records from Target table using MERGE command?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-perform-upcerts-and-delete-missing-records-from-target/m-p/19814#M13343</link>
      <description>&lt;P&gt;The record indicator is indeed a column which contains what type of data the record is (insert; delete, update).  You might get this from a CDC system, or you can construct it yourself.&lt;/P&gt;&lt;P&gt;In case you have a cdc system, this is pretty straightforward as you can identify the deleted records and so you can use when matched.&lt;/P&gt;&lt;P&gt;Otherwise you will have to detect the deletes in another way.  What I do if there is no CDC system available is to extract the PK of the concerning table and compare with that.&lt;/P&gt;&lt;P&gt;The performance will be quite good as you only select the PK (clustered index).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;PS. I also never delete records on our data lake, I flag them as deleted or archived.&lt;/P&gt;</description>
      <pubDate>Wed, 25 May 2022 12:21:34 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-perform-upcerts-and-delete-missing-records-from-target/m-p/19814#M13343</guid>
      <dc:creator>-werners-</dc:creator>
      <dc:date>2022-05-25T12:21:34Z</dc:date>
    </item>
    <item>
      <title>Re: How to perform UPCERTS and  DELETE missing records from Target table using MERGE command?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-perform-upcerts-and-delete-missing-records-from-target/m-p/19815#M13344</link>
      <description>&lt;P&gt;Hey @Krishna Puthran​&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Hope you are well. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Just wanted to see if you were able to find an answer to your question and would you like to mark an answer as best? It would be really helpful for the other members too.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Cheers!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 22 Jul 2022 14:31:46 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-perform-upcerts-and-delete-missing-records-from-target/m-p/19815#M13344</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2022-07-22T14:31:46Z</dc:date>
    </item>
  </channel>
</rss>

