
In many data pipelines, raw data is first collected in a staging or snapshot table before it is merged into the main analytics table. This pattern works well because it allows ingestion to run frequently while keeping the main dataset clean and structured.
However, pipelines that ingest data multiple times per day often introduce an unexpected issue: duplicate rows. The ingestion process itself might run perfectly, and the merge query might also appear correct, yet duplicate records still find their way into the final table.
I recently encountered this exact situation while working on a data ingestion pipeline that collected application activity logs several times a day. Each ingestion run pulled the latest records from an external API and inserted them into a snapshot table. Later, a merge job moved those records into the primary analytics table used by dashboards and reports.
Everything seemed fine until we noticed that certain events were appearing more than once in the main table.
How Frequent Ingestion Creates Duplicate Records
The root cause turned out to be the ingestion frequency. Because the pipeline ran multiple times a day, the same logical record could appear in several ingestion runs. When those runs inserted data into the snapshot table, the table accumulated multiple rows representing the same event.
When the merge query later processed this table, it treated these rows as separate inputs.If the merge logic does not account for this situation, duplicate rows can end up in the final dataset.
Why the MERGE Query Alone Is Not Enough
At first glance, it may seem like the merge query should already prevent duplicates. After all, the merge condition checks whether a record already exists in the target table.
The problem is that the snapshot table itself contains multiple versions of the same record. When those duplicates are processed together, the merge operation may still insert more than one row depending on how the matching logic is defined.
In other words, the merge query is not the issue. The real problem lies in the data being passed into the merge.
Deduplicating Snapshot Data Before the Merge
The most reliable solution is to clean the snapshot data before it reaches the merge step. Instead of merging every row from the snapshot table, we first select a single representative row for each logical record.
A common SQL technique for this is using the ROW_NUMBER() window function. This function allows us to group duplicate rows and assign an order to them based on a chosen criterion, such as the ingestion timestamp.
The following query keeps only the most recent version of each record.
SELECT * FROM ( SELECT *, ROW_NUMBER() OVER( PARTITION BY user_id, event_hash ORDER BY ingestion_ts DESC ) rn FROM dataset.activity_snapshot ) WHERE rn = 1;
This query assigns a rank to each duplicate group and keeps the row with the latest ingestion timestamp.
Using the Cleaned Data in the MERGE Operation
Once the snapshot data is deduplicated, the merge query becomes safe because it operates only on unique records.
MERGE dataset.user_activity t USING (
SELECT *
FROM (
SELECT *,
ROW_NUMBER() OVER(
PARTITION BY user_id, event_hash
ORDER BY ingestion_ts DESC
) rn
FROM dataset.activity_snapshot
)
WHERE rn = 1
) s ON t.user_id = s.user_id AND t.event_hash = s.event_hash
WHEN NOT MATCHED THEN INSERT ( user_id, event_type, event_hash, ingestion_ts )
VALUES ( s.user_id, s.event_type, s.event_hash, s.ingestion_ts );
Because duplicates have already been filtered out, the merge operation processes only one row per logical record.
Why This Pattern Works Across Many SQL Systems
The approach above is not specific to a single database system. The ROW_NUMBER() window function is supported in most modern SQL engines, including PostgreSQL, SQL Server, Snowflake, BigQuery, and others.
This makes the pattern widely applicable for pipelines that rely on snapshot tables and frequent ingestion runs.
When You Should Use This Approach
This deduplication technique becomes particularly useful when ingestion pipelines run several times per day and may retrieve overlapping data. It is also common in systems that collect logs, events, API responses, or incremental updates where the same record might appear multiple times.
Adding a small deduplication step before merging ensures that the main analytics table remains clean and consistent.
Final Thoughts
Snapshot tables are extremely useful for staging incoming data, but they often accumulate duplicate rows when ingestion jobs run frequently. Instead of relying on the merge query alone to handle duplicates, it is safer to deduplicate the snapshot data first.
A simple window function can ensure that only the most recent version of each record moves into the final dataset. This small adjustment makes merge pipelines far more reliable and prevents subtle data quality issues from appearing later.
About the Author
Hi, I am Ankit Raj, a Data Engineer who spends most of his time building data pipelines and solving practical data challenges. I work with modern data platforms and cloud analytics systems, focusing on ingestion workflows, transformation pipelines, and scalable data architectures.
Alongside my engineering work, I also freelance with teams that need help designing reliable data systems or improving their existing pipelines. I enjoy sharing real production lessons from these experiences through writing.
About the author
- Data Engineer passionate about turning raw data into reliable pipelines. Sharing practical insights on modern data engineering.
Comments
Join the discussionNo comments yet. Be the first to comment.





