Data Deduplication & Change Detection

Source data often contains technical duplicates (identical rows ingested multiple times) and mass updates (rows that were re-imported but haven't actually changed). A deduplication staging view filters these out before data reaches your core tables.

The Problem

When syncing data from a production system, you may encounter:

  1. Technical duplicates — the same row imported more than once with identical content
  2. Mass updates — rows re-synced with a new db_row_timestamp but no actual data change
  3. Historical bloat — keeping every version of every row, even when nothing changed

The Staging View Pattern

Create a staging view that handles all three issues in one pipeline:

Step 1: Remove Technical Duplicates

Compare full rows as JSON strings to find exact duplicates:

WITH technical_deduplication AS (
  SELECT
    *,
    ROW_NUMBER() OVER (
      PARTITION BY TO_JSON_STRING(tt)
      ORDER BY db_row_timestamp DESC
    ) AS tech_ranking
  FROM `{{ raw_value($project_id) }}.source_data.raw_table` tt
)

SELECT * FROM technical_deduplication
WHERE tech_ranking = 1

TO_JSON_STRING(tt) converts the entire row to a JSON string. When two rows produce the same JSON, they are identical — we keep only the latest one.

Step 2: Detect Real Changes

Use a hash-based comparison to identify rows where only the timestamp changed but the actual data did not:

create_row_ids AS (
  SELECT *,
    TO_HEX(MD5(
      `{{ raw_value($project_id) }}.functions.json_except`(
        TO_JSON_STRING(td),
        ['db_row_timestamp']
      )
    )) AS row_id
  FROM technical_deduplication td
  WHERE tech_ranking = 1
),

compare_row_ids AS (
  SELECT *,
    LAG(row_id) OVER (
      PARTITION BY CONCAT(id, tenant)
      ORDER BY db_row_timestamp ASC
    ) = row_id AS is_duplicate_update
  FROM create_row_ids
)

SELECT * FROM compare_row_ids
WHERE is_duplicate_update IS NOT TRUE

Key techniques:

  • json_except — a custom BigQuery function that removes specified keys (like db_row_timestamp) from the JSON before hashing, so timestamps don't affect the comparison
  • TO_HEX(MD5(...)) — creates a compact hash of the data content
  • LAG — compares each row's hash with the previous version of the same record. If they match, the row is a duplicate update

Step 3: Add Ranking

For the final output, add a ranking column to identify the latest version of each record:

SELECT *,
  ROW_NUMBER() OVER (
    PARTITION BY CONCAT(id, tenant)
    ORDER BY db_row_timestamp DESC
  ) AS ranking
FROM deduplicated_rows

Use WHERE ranking = 1 in downstream queries to get only the current version.

Complete Staging View

Here is the full staging view combining all three steps:

CREATE OR REPLACE VIEW
`{{ raw_value($project_id) }}.staging.my_table_filters`
AS

WITH
technical_deduplication AS (
  SELECT *,
    ROW_NUMBER() OVER (
      PARTITION BY TO_JSON_STRING(tt)
      ORDER BY db_row_timestamp DESC
    ) AS tech_ranking
  FROM `{{ raw_value($project_id) }}.source_data.raw_table` tt
),

create_row_ids AS (
  SELECT *,
    TO_HEX(MD5(
      `{{ raw_value($project_id) }}.functions.json_except`(
        TO_JSON_STRING(td),
        ['db_row_timestamp']
      )
    )) AS row_id
  FROM technical_deduplication td
  WHERE tech_ranking = 1
),

compare_row_ids AS (
  SELECT *,
    LAG(row_id) OVER (
      PARTITION BY CONCAT(id, tenant)
      ORDER BY db_row_timestamp ASC
    ) = row_id AS is_duplicate_update
  FROM create_row_ids
)

SELECT *,
  ROW_NUMBER() OVER (
    PARTITION BY CONCAT(id, tenant)
    ORDER BY db_row_timestamp DESC
  ) AS ranking
FROM compare_row_ids
WHERE is_duplicate_update IS NOT TRUE

Tips

  • Use CREATE OR REPLACE VIEW (not TABLE) for staging layers — views don't consume storage and always reflect the latest source data
  • The json_except function is specific to BigQuery; for other databases, manually exclude audit columns from the comparison
  • Combine with multi-step pipelines where the staging view feeds into core tables and datamarts
  • Add assertions after building your core tables to ensure the deduplication worked correctly