Prompt
I need a way to load data incrementally and deduplicate records
Latest observation
Nov 8, 2026
OpenAI APIWeb search: off
A common way to do this is to use an incremental load + merge/upsert pattern.
Core idea
-
Track what you’ve already loaded
- Use a watermark like
updated_at,ingestion_time, or an increasingid. - Store the last successfully processed value.
- Use a watermark like
-
Only pull new/changed records
- Example:
WHERE updated_at > last_watermark
- Example:
-
Deduplicate before writing
- Define a business key (for example:
customer_id,order_id) - Keep only the latest record per key based on a timestamp or version number.
- Define a business key (for example:
-
Upsert into the target
- Insert new rows
- Update existing rows when the incoming record is newer
Example SQL pattern
Stage new data
SELECT *
FROM source_table
WHERE updated_at > :last_watermark;
Deduplicate
WITH ranked AS (
SELECT *,
ROW_NUMBER() OVER (
PARTITION BY business_key
ORDER BY updated_at DESC
) AS rn
FROM staging_table
)
SELECT *
FROM ranked
WHERE rn = 1;
Merge into target
MERGE INTO target_table t
USING deduped_staging s
ON t.business_key = s.business_key
WHEN MATCHED AND s.updated_at > t.updated_at THEN
UPDATE SET *
WHEN NOT MATCHED THEN
INSERT *;
If you’re using Python/Pandas
import pandas as pd
df = pd.read_csv("new_data.csv")
# deduplicate by key, keeping latest record
df = df.sort_values("updated_at").drop_duplicates("business_key", keep="last")
Best practices
- Use a stable unique key for deduplication
- Prefer
updated_atover raw ingestion order if available - Make the load idempotent so reruns don’t create duplicates
- Keep a checkpoint/watermark so you know where to resume
- Handle late-arriving data if your source can send older records later
If you want, I can show this in:
- SQL
- Python/pandas
- Spark/Databricks
- dbt
- A full ETL pipeline example