Forum Discussion
SCD Type 2 Using MERGE in delta
- 1 year ago
Hello AndersASorensen
give it a try
from pyspark.sql.functions import *
from delta.tables import *# Assuming 'target_table' is your existing Delta table
target_table = DeltaTable.forName(spark, "target_table")# 'source_df' is your new data
merge_condition = "target.natural_key = source.natural_key"(target_table.alias("target")
.merge(source_df.alias("source"), merge_condition)
.whenMatchedUpdate(set={
"attribute1": "source.attribute1",
"attribute2": "source.attribute2",
"is_current": lit(True),
"update_date": current_date()
})
.whenNotMatchedInsert(values={
"natural_key": "source.natural_key",
"attribute1": "source.attribute1",
"attribute2": "source.attribute2",
"is_current": lit(True),
"update_date": current_date()
})
.execute())Hope this helps.
Please accept the answer if this works.
Thanks
Hi again 🙂
The code provided won't work, but the article you linked to had a working solution 🙂
Thanks!
This comment saved my life. Thank you AndersASorensen