<?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 MERGE to update a column of a table using Spark SQL in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/merge-to-update-a-column-of-a-table-using-spark-sql/m-p/6905#M2906</link>
    <description>&lt;P&gt;Coming from MS SQL background, I'm trying to write a query in Spark SQL that simply update a column value of table A (source table) by INNER JOINing a new table B with a filter.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;MS SQL query looks like this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;UPDATE T
SET T.OfferAmount = OSE.EndpointEventAmountValue
FROM #TempOffer T
INNER JOIN OfferSeq OSE
	ON OSE.OfferId = T.OfferID
	AND OSE.OfferEventSequenceId = 1&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Since spark sql didn't like the UPDATE command, I've been using MERGE to update records (as @Hubert Dudek​&amp;nbsp;suggested &lt;A href="https://community.databricks.com/s/question/0D53f00001nXEfJCAW/does-spark-sql-can-perform-update-with-inner-join-and-like-with-column-" alt="https://community.databricks.com/s/question/0D53f00001nXEfJCAW/does-spark-sql-can-perform-update-with-inner-join-and-like-with-column-" target="_blank"&gt;in this post)&lt;/A&gt; which worked fine for cases without the filter. But when there is an filter like "&lt;B&gt;OSE.OfferEventSequenceId = 1&lt;/B&gt;" in the spark sql query below, I'm not sure where I should add the filter (I added a WHERE clause here but not sure if that's the correct syntax):&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;%sql
MERGE into TempOffer VIEW
USING OfferSeq OSE ON VIEW.OfferId = OSE.OfferID
WHERE OSE.OfferId = 1
WHEN MATCHED THEN UPDATE set VIEW.OfferAmount = OSE.EndpointEventAmountValue;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
    <pubDate>Tue, 28 Mar 2023 20:36:30 GMT</pubDate>
    <dc:creator>oleole</dc:creator>
    <dc:date>2023-03-28T20:36:30Z</dc:date>
    <item>
      <title>MERGE to update a column of a table using Spark SQL</title>
      <link>https://community.databricks.com/t5/data-engineering/merge-to-update-a-column-of-a-table-using-spark-sql/m-p/6905#M2906</link>
      <description>&lt;P&gt;Coming from MS SQL background, I'm trying to write a query in Spark SQL that simply update a column value of table A (source table) by INNER JOINing a new table B with a filter.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;MS SQL query looks like this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;UPDATE T
SET T.OfferAmount = OSE.EndpointEventAmountValue
FROM #TempOffer T
INNER JOIN OfferSeq OSE
	ON OSE.OfferId = T.OfferID
	AND OSE.OfferEventSequenceId = 1&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Since spark sql didn't like the UPDATE command, I've been using MERGE to update records (as @Hubert Dudek​&amp;nbsp;suggested &lt;A href="https://community.databricks.com/s/question/0D53f00001nXEfJCAW/does-spark-sql-can-perform-update-with-inner-join-and-like-with-column-" alt="https://community.databricks.com/s/question/0D53f00001nXEfJCAW/does-spark-sql-can-perform-update-with-inner-join-and-like-with-column-" target="_blank"&gt;in this post)&lt;/A&gt; which worked fine for cases without the filter. But when there is an filter like "&lt;B&gt;OSE.OfferEventSequenceId = 1&lt;/B&gt;" in the spark sql query below, I'm not sure where I should add the filter (I added a WHERE clause here but not sure if that's the correct syntax):&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;%sql
MERGE into TempOffer VIEW
USING OfferSeq OSE ON VIEW.OfferId = OSE.OfferID
WHERE OSE.OfferId = 1
WHEN MATCHED THEN UPDATE set VIEW.OfferAmount = OSE.EndpointEventAmountValue;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 28 Mar 2023 20:36:30 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/merge-to-update-a-column-of-a-table-using-spark-sql/m-p/6905#M2906</guid>
      <dc:creator>oleole</dc:creator>
      <dc:date>2023-03-28T20:36:30Z</dc:date>
    </item>
    <item>
      <title>Re: MERGE to update a column of a table using Spark SQL</title>
      <link>https://community.databricks.com/t5/data-engineering/merge-to-update-a-column-of-a-table-using-spark-sql/m-p/6906#M2907</link>
      <description>&lt;PRE&gt;&lt;CODE&gt;Posting answer to my question:
&amp;nbsp;
MERGE into TempOffer VIEW
USING OfferSeq OSE ON VIEW.OfferId = OSE.OfferID AND OSE.OfferId = 1
WHEN MATCHED THEN UPDATE set VIEW.OfferAmount = OSE.EndpointEventAmountValue;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 29 Mar 2023 03:21:37 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/merge-to-update-a-column-of-a-table-using-spark-sql/m-p/6906#M2907</guid>
      <dc:creator>oleole</dc:creator>
      <dc:date>2023-03-29T03:21:37Z</dc:date>
    </item>
  </channel>
</rss>

