cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results for 
Search instead for 
Did you mean: 

MERGE to update a column of a table using Spark SQL

oleole
Contributor

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.

MS SQL query looks like this:

UPDATE T
SET T.OfferAmount = OSE.EndpointEventAmountValue
FROM #TempOffer T
INNER JOIN OfferSeq OSE
	ON OSE.OfferId = T.OfferID
	AND OSE.OfferEventSequenceId = 1

Since spark sql didn't like the UPDATE command, I've been using MERGE to update records (as @Hubert Dudek​ suggested in this post) which worked fine for cases without the filter. But when there is an filter like "OSE.OfferEventSequenceId = 1" 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):

%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;

1 ACCEPTED SOLUTION

Accepted Solutions

oleole
Contributor
Posting answer to my question:
 
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;

View solution in original post

1 REPLY 1

oleole
Contributor
Posting answer to my question:
 
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;

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you won’t want to miss the chance to attend and share knowledge.

If there isn’t a group near you, start one and help create a community that brings people together.

Request a New Group