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:ย 

Upsert When the Origin NOT Exists, but you need to change status in the target

William_Scardua
Valued Contributor

Hi guys,

I have a question about upsert/merge ... What do you do when que origin NOT exists, but you need to change status in the target

โ€‹For exemple:

01/03 : source dataset [ id =1 and status = Active] ; target table [*not exists*] >> in this time the upsert/merge add the source record in target table

โ€‹โ€‹02/03: source dataset [ id = 1 and status = Wait] ; target table [id =1 and status = Active] >> in this time the upsert/merge change the status record in targe table

โ€‹โ€‹03/03: source dataset [ * id = 1, it disappeared in source *] ; target table [id =1 and status = Deactivate] >> in this time the upsert/merge not found id = 1 in the source and can`t the change status in target table, but I need to change the status of record to 'Deactivate'

Have any idea ?

1 ACCEPTED SOLUTION

Accepted Solutions

pvignesh92
Honored Contributor

Hi @William Scarduaโ€‹ Delta table gives you the option where you can match with either source or target table and decide the possible action on your target table.

Please try to use the below approach and let us know it this meets your requirement.

-- Delete all target rows that have no matches in the source table.
> MERGE INTO target USING source
  ON target.key = source.key
  WHEN NOT MATCHED BY SOURCE THEN DELETE
 
-- Multiple NOT MATCHED BY SOURCE clauses conditionally deleting unmatched target rows and updating two columns for all other matched rows.
> MERGE INTO target USING source
  ON target.key = source.key
  WHEN NOT MATCHED BY SOURCE AND target.marked_for_deletion THEN DELETE
  WHEN NOT MATCHED BY SOURCE THEN UPDATE SET target.value = DEACTIVATE

Thanks,

Vignesh

View solution in original post

3 REPLIES 3

pvignesh92
Honored Contributor

Hi @William Scarduaโ€‹ Delta table gives you the option where you can match with either source or target table and decide the possible action on your target table.

Please try to use the below approach and let us know it this meets your requirement.

-- Delete all target rows that have no matches in the source table.
> MERGE INTO target USING source
  ON target.key = source.key
  WHEN NOT MATCHED BY SOURCE THEN DELETE
 
-- Multiple NOT MATCHED BY SOURCE clauses conditionally deleting unmatched target rows and updating two columns for all other matched rows.
> MERGE INTO target USING source
  ON target.key = source.key
  WHEN NOT MATCHED BY SOURCE AND target.marked_for_deletion THEN DELETE
  WHEN NOT MATCHED BY SOURCE THEN UPDATE SET target.value = DEACTIVATE

Thanks,

Vignesh

Thank you @Vigneshraja Palanirajโ€‹ ,

For this I need to change the my cluster version, but thank you man

NandiniN
Databricks Employee
Databricks Employee

Hello @William Scarduaโ€‹ ,

Just adding to what @Vigneshraja Palanirajโ€‹ replied.

Reference: https://docs.databricks.com/sql/language-manual/delta-merge-into.html

Thanks & Regards,

Nandini

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