Forum Discussion
Update table with SQL MERGE issue duplicates: help needed with combining SQL deduplicate & update
- Anonymous2 years ago
Hi Vogels
Thanks for using Fabric Community.
You are doing right.
Instead of copying and deleting the table, perform deduplication onLarge_Table_Previous_Daysbefore merging. This ensures unique rows are fed into theMERGEstatement. You can achieve this using theROW_NUMBERfunction to assign a unique sequence number based on a specific order:-- Deduplicate Large_Table_Previous_Days SELECT *, ROW_NUMBER() OVER (PARTITION BY ID ORDER BY LOAD_DATE DESC) AS row_num FROM Large_Table_Previous_Days -- Use only the first row (latest LOAD_DATE) for each ID in the merge WHERE row_num = 1
Modify yourMERGEstatement to use the deduplicatedLarge_Table_Previous_Daysas the source:-- Deduplicate Large_Table_Previous_Days SELECT *, ROW_NUMBER() OVER (PARTITION BY ID ORDER BY LOAD_DATE DESC) AS row_num FROM Large_Table_Previous_Days WHERE row_num = 1 -- Merge deduplicated data into Large_Table MERGE INTO Large_Table AS target USING ( -- Deduplicated result from the previous step ) AS source ON (target.ID = source.ID) WHEN MATCHED THEN UPDATE SET target.LOAD_DATE = source.LOAD_DATE, target.ID = source.ID, target.Content = source.Content WHEN NOT MATCHED THEN INSERT (LOAD_DATE, ID, Content) VALUES (source.LOAD_DATE, source.ID, source.Content)Replace
Large_Table,Large_Table_Previous_Days, and the column names (ID,LOAD_DATE,Content) with your actual table and column names.This code performs the following steps:
- Deduplicates
Large_Table_Previous_Days:- It assigns a unique
row_numto each row within each group of the sameID, ordered by descendingLOAD_DATE. - Only the first row (latest
LOAD_DATE) for eachIDis kept, ensuring unique data.
- It assigns a unique
- Merges deduplicated data:
- The deduplicated result is used as the
sourcein theMERGEstatement. ONclause matches rows based on the sameIDin both tables.WHEN MATCHEDupdates existing rows inLarge_Table.WHEN NOT MATCHEDinserts new rows from thesource.
- The deduplicated result is used as the
This combined code should prevent duplicate row errors and ensure only new or changed entries are updated in your large table.
While this is one approach , if you want to perform an upsert (update or insert) operation on a table, you can use Dataflow Gen2 here. Incrementally amassing data in a data destination requires a technique to load only new or updated data into your data destination. This technique can be done by using a query to filter the data based on the data destination. The source will be the Large_Table_Previous_Days and the target table will be Large_Table. For more information refer to this document:
https://learn.microsoft.com/en-us/fabric/data-factory/tutorial-setup-incremental-refresh-with-dataflows-gen2
Hope this helps. Please let us know if you have any further queries. - Deduplicates
Hi Vogels
We haven’t heard from you on the last response and was just checking back to see if you have a resolution yet. Otherwise, will respond back with the more details and we will try to help.
Thanks
Hi @Vogels
We haven’t heard from you on the last response and was just checking back to see if you have a resolution yet. Otherwise, will respond back with the more details and we will try to help.
Thanks
- Vogels2 years agoFrequent Visitor
Hi Anonymous,
Thank you very much for the detailed answer. It works perfectly now.