<?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 Re: How to prevent duplicate records when using MERGE INTO? in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/how-to-prevent-duplicate-records-when-using-merge-into/m-p/177961#M57431</link>
    <description>&lt;P&gt;hi&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/274445"&gt;@dataumer&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;MERGE expects at most one source row per key. If the same customer ID appears twice, it fails with "multiple source rows matched" or applies changes in an unpredictable order. So yes, ROW_NUMBER() is the right approach: cut the source down to the latest row per key, then merge that result.&lt;/P&gt;&lt;P&gt;MERGE INTO customers AS t&lt;/P&gt;&lt;P&gt;USING (&lt;/P&gt;&lt;P&gt;&amp;nbsp; SELECT *&lt;/P&gt;&lt;P&gt;&amp;nbsp; FROM customer_updates&lt;/P&gt;&lt;P&gt;&amp;nbsp; QUALIFY ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY updated_at DESC, ingest_seq DESC) = 1&lt;/P&gt;&lt;P&gt;) AS s&lt;/P&gt;&lt;P&gt;ON t.customer_id = s.customer_id&lt;/P&gt;&lt;P&gt;WHEN MATCHED AND s.updated_at &amp;gt; t.updated_at THEN UPDATE SET *&lt;/P&gt;&lt;P&gt;WHEN NOT MATCHED THEN INSERT *&lt;/P&gt;&lt;P&gt;Same-timestamp ties: ROW_NUMBER() picks one of the tied rows arbitrarily, so add a tie-breaker column to the ORDER BY (an ingest sequence number, file offset, or _metadata.file_modification_time) so the result is deterministic. If there's genuinely no way to order them, that's a data contract issue to raise with the source team.&lt;/P&gt;&lt;P&gt;A few other details:&lt;/P&gt;&lt;P&gt;- The s.updated_at &amp;gt; t.updated_at condition stops a late or replayed batch from overwriting newer data.&lt;/P&gt;&lt;P&gt;- In a streaming job, put the dedup and MERGE inside foreachBatch so each micro-batch is deduped before it merges.&lt;/P&gt;&lt;P&gt;- If you need every change kept as history (SCD2), use AUTO CDC / APPLY CHANGES in Lakeflow Declarative Pipelines with SEQUENCE BY updated_at. It handles the ordering for you.&lt;/P&gt;&lt;P&gt;Docs: &lt;A href="https://docs.databricks.com/aws/en/delta/merge" target="_blank"&gt;https://docs.databricks.com/aws/en/delta/merge&lt;/A&gt;&lt;/P&gt;</description>
    <pubDate>Sat, 28 Nov 2026 19:24:55 GMT</pubDate>
    <dc:creator>SumeshKashyap</dc:creator>
    <dc:date>2026-11-28T19:24:55Z</dc:date>
    <item>
      <title>How to prevent duplicate records when using MERGE INTO?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-prevent-duplicate-records-when-using-merge-into/m-p/177887#M57402</link>
      <description>&lt;P&gt;&lt;SPAN&gt;hello guys&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;What is the best way to handle a source dataset that contains multiple records for the same business key before performing a MERGE into a Delta table?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;For example, the same customer ID may appear multiple times with different update timestamps.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Would using ROW_NUMBER() to retain the latest record be sufficient? How should we handle cases where multiple records have the same timestamp?&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 28 Nov 2026 09:09:29 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-prevent-duplicate-records-when-using-merge-into/m-p/177887#M57402</guid>
      <dc:creator>dataumer</dc:creator>
      <dc:date>2026-11-28T09:09:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to prevent duplicate records when using MERGE INTO?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-prevent-duplicate-records-when-using-merge-into/m-p/177961#M57431</link>
      <description>&lt;P&gt;hi&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/274445"&gt;@dataumer&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;MERGE expects at most one source row per key. If the same customer ID appears twice, it fails with "multiple source rows matched" or applies changes in an unpredictable order. So yes, ROW_NUMBER() is the right approach: cut the source down to the latest row per key, then merge that result.&lt;/P&gt;&lt;P&gt;MERGE INTO customers AS t&lt;/P&gt;&lt;P&gt;USING (&lt;/P&gt;&lt;P&gt;&amp;nbsp; SELECT *&lt;/P&gt;&lt;P&gt;&amp;nbsp; FROM customer_updates&lt;/P&gt;&lt;P&gt;&amp;nbsp; QUALIFY ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY updated_at DESC, ingest_seq DESC) = 1&lt;/P&gt;&lt;P&gt;) AS s&lt;/P&gt;&lt;P&gt;ON t.customer_id = s.customer_id&lt;/P&gt;&lt;P&gt;WHEN MATCHED AND s.updated_at &amp;gt; t.updated_at THEN UPDATE SET *&lt;/P&gt;&lt;P&gt;WHEN NOT MATCHED THEN INSERT *&lt;/P&gt;&lt;P&gt;Same-timestamp ties: ROW_NUMBER() picks one of the tied rows arbitrarily, so add a tie-breaker column to the ORDER BY (an ingest sequence number, file offset, or _metadata.file_modification_time) so the result is deterministic. If there's genuinely no way to order them, that's a data contract issue to raise with the source team.&lt;/P&gt;&lt;P&gt;A few other details:&lt;/P&gt;&lt;P&gt;- The s.updated_at &amp;gt; t.updated_at condition stops a late or replayed batch from overwriting newer data.&lt;/P&gt;&lt;P&gt;- In a streaming job, put the dedup and MERGE inside foreachBatch so each micro-batch is deduped before it merges.&lt;/P&gt;&lt;P&gt;- If you need every change kept as history (SCD2), use AUTO CDC / APPLY CHANGES in Lakeflow Declarative Pipelines with SEQUENCE BY updated_at. It handles the ordering for you.&lt;/P&gt;&lt;P&gt;Docs: &lt;A href="https://docs.databricks.com/aws/en/delta/merge" target="_blank"&gt;https://docs.databricks.com/aws/en/delta/merge&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 28 Nov 2026 19:24:55 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-prevent-duplicate-records-when-using-merge-into/m-p/177961#M57431</guid>
      <dc:creator>SumeshKashyap</dc:creator>
      <dc:date>2026-11-28T19:24:55Z</dc:date>
    </item>
    <item>
      <title>Re: How to prevent duplicate records when using MERGE INTO?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-prevent-duplicate-records-when-using-merge-into/m-p/178047#M57467</link>
      <description>&lt;P&gt;With the SQL semantics of MERGE, when more than one source row matches the same target row on the join key, the operation is ambiguous - Spark cannot tell which source row should win, so the MERGE either fails outright (multiple source rows matched and attempted to update the same target row error) or silently inserts duplicates when the duplicate rows happen to be unmatched new keys. You can &lt;STRONG&gt;pre process&lt;/STRONG&gt; the source to eliminate the possibility of multiple matches before merging. MERGE &lt;STRONG&gt;deduplicates&lt;/STRONG&gt; against the &lt;STRONG&gt;target&lt;/STRONG&gt;, but never within the source - so a good merge can land duplicates that came in inside one batch.&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Deduplicate &lt;/STRONG&gt;the &lt;STRONG&gt;source&lt;/STRONG&gt; to exactly one row per business key before the USING clause, and &lt;STRONG&gt;ROW_NUMBER&lt;/STRONG&gt;&amp;nbsp;instinct is right as it's the right pattern. You must add a &lt;STRONG&gt;deterministic&lt;/STRONG&gt; &lt;STRONG&gt;tie breaker&lt;/STRONG&gt;, because if two records share the same customer_id and the same updated_at, ROW_NUMBER&amp;nbsp;alone picks a winner &lt;STRONG&gt;nondeterministically&lt;/STRONG&gt; (winning row can flip between runs giving different results on a re run or a retry). You can chain &lt;STRONG&gt;secondary sort&lt;/STRONG&gt; keys such as a &lt;STRONG&gt;sequence number&lt;/STRONG&gt;, ingestion timestamp or a stable &lt;STRONG&gt;hash&lt;/STRONG&gt; of the row so the same input always produces the same winner.&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;WITH ranked AS (
  SELECT *, ROW_NUMBER() OVER (
            PARTITION BY customer_id
            ORDER BY updated_at DESC,
                     batch_seq DESC,          -- tie breaker 1
                     to_json(struct(*)) DESC
         ) AS rn
  FROM   source_updates
)
MERGE INTO customers t
USING (SELECT * FROM ranked WHERE rn = 1) s
ON    t.customer_id = s.customer_id
WHEN MATCHED AND t.updated_at &amp;lt; s.updated_at THEN UPDATE SET *
WHEN NOT MATCHED THEN INSERT *&lt;/LI-CODE&gt;</description>
      <pubDate>Sun, 29 Nov 2026 14:51:16 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-prevent-duplicate-records-when-using-merge-into/m-p/178047#M57467</guid>
      <dc:creator>balajij8</dc:creator>
      <dc:date>2026-11-29T14:51:16Z</dc:date>
    </item>
    <item>
      <title>Re: How to prevent duplicate records when using MERGE INTO?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-prevent-duplicate-records-when-using-merge-into/m-p/178097#M57476</link>
      <description>&lt;DIV&gt;&lt;DIV&gt;Hello&lt;/DIV&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;P&gt;&lt;SPAN&gt;Yes, &lt;/SPAN&gt;&lt;SPAN&gt;ROW_NUMBER()&lt;/SPAN&gt;&lt;SPAN&gt; is a good approach, but the important part is making the deduplication deterministic.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Delta MERGE does not automatically deduplicate source records. Multiple source records matching the same target record can cause ambiguous update errors.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;STRONG&gt;1. Deduplicate the source before MERGE&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;DIV&gt;&lt;PRE&gt;CREATE OR REPLACE TEMP VIEW clean_source AS
SELECT *
FROM staging_customers
QUALIFY ROW_NUMBER() OVER (
    PARTITION BY customer_id
    ORDER BY updated_at DESC, source_sequence DESC
) = 1;&lt;/PRE&gt;&lt;/DIV&gt;&lt;P&gt;&lt;SPAN&gt;For example, if customer 101 has two records with the same update timestamp but sequence numbers 5 and 6, the record with sequence 6 will be selected, assuming the sequence represents the actual event order.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;STRONG&gt;2. Prevent older records from overwriting newer records&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;DIV&gt;&lt;PRE&gt;MERGE INTO target_customers AS t
USING clean_source AS s
ON t.customer_id = s.customer_id

WHEN MATCHED AND (
    s.updated_at &amp;gt; t.updated_at
    OR (
        s.updated_at = t.updated_at
        AND s.source_sequence &amp;gt; t.source_sequence
    )
)
THEN UPDATE SET *

WHEN NOT MATCHED THEN INSERT *;&lt;/PRE&gt;&lt;/DIV&gt;&lt;P&gt;&lt;SPAN&gt;&lt;STRONG&gt;3. Handle identical timestamps&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;If timestamps are identical, use a reliable secondary ordering field such as:&lt;/SPAN&gt;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;P&gt;&lt;SPAN&gt;CDC sequence number.&lt;/SPAN&gt;&lt;/P&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/LI&gt;&lt;LI&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;P&gt;&lt;SPAN&gt;Source transaction sequence.&lt;/SPAN&gt;&lt;/P&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/LI&gt;&lt;LI&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;P&gt;&lt;SPAN&gt;Event version number.&lt;/SPAN&gt;&lt;/P&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;SPAN&gt;If no reliable ordering field exists and the records contain conflicting values, I would quarantine those records rather than arbitrarily selecting one.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;My recommendation is to combine deterministic source deduplication with conditional MERGE logic. Also, validate business key uniqueness in the target table and ensure ordering fields are non-null.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Avoid using &lt;/SPAN&gt;&lt;SPAN&gt;dropDuplicates(["customer_id"])&lt;/SPAN&gt;&lt;SPAN&gt; alone because it does not guarantee that the latest record is retained.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;STRONG&gt;Official references:&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;P&gt;&lt;SPAN&gt;&lt;A href="https://docs.databricks.com/aws/en/delta/merge?utm_source=chatgpt.com" target="_blank" rel="noopener"&gt;Delta Lake MERGE operations&lt;/A&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/LI&gt;&lt;LI&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;P&gt;&lt;SPAN&gt;&lt;A href="https://docs.databricks.com/aws/en/sql/language-manual/functions/row_number?utm_source=chatgpt.com" target="_blank" rel="noopener"&gt;ROW_NUMBER function&lt;/A&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/LI&gt;&lt;LI&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;P&gt;&lt;SPAN&gt;&lt;A href="https://docs.databricks.com/aws/en/sql/language-manual/delta-merge-into?utm_source=chatgpt.com" target="_blank" rel="noopener"&gt;MERGE INTO SQL reference&lt;/A&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Sun, 29 Nov 2026 19:20:10 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-prevent-duplicate-records-when-using-merge-into/m-p/178097#M57476</guid>
      <dc:creator>tajamulkhan</dc:creator>
      <dc:date>2026-11-29T19:20:10Z</dc:date>
    </item>
  </channel>
</rss>

