Row Number Deduplication Keep Latest

Deduplicate rows keeping the newest per key with ROW_NUMBER, sweeping duplicates in one statement or blocking them upstream.

SqlData Integrity

Code

sqlsnippet
33 lines
-- 每个邮箱保留 created_at 最新的一行,标记其余为重复
WITH ranked AS (
  SELECT
    id,
    email,
    created_at,
    ROW_NUMBER() OVER (
      PARTITION BY email
      ORDER BY created_at DESC, id DESC
    ) AS rn
  FROM subscribers
)
SELECT id, email, created_at, (rn = 1) AS is_primary
FROM ranked
ORDER BY email, rn;

-- 一次性清扫:删除全部非首行
WITH ranked AS (
  SELECT id,
         ROW_NUMBER() OVER (
           PARTITION BY email
           ORDER BY created_at DESC, id DESC
         ) AS rn
  FROM subscribers
)
DELETE FROM subscribers s
USING ranked r
WHERE s.id = r.id
  AND r.rn > 1;

-- 上游堵漏:唯一索引阻止重复再次写入
CREATE UNIQUE INDEX CONCURRENTLY idx_subscribers_email
  ON subscribers (email);

How the snippet works

The file runs SELECT / DELETE / CREATE statements, touching subscribers and ranked.

Concretely, the SQL combines: a CTE (WITH … AS) that names an intermediate result before the main query; a WHERE clause deciding which rows the statement applies to; a window function (OVER (…)) working across rows while keeping each row in the result; an ORDER BY that fixes the output order; UNIQUE constraints that keep duplicate values out.

Points of care

  • Remember the evaluation order: WHERE first, window functions later — filtering on the latter needs another query level.

Applying it

Run the statement against the subscribers table.

Statements that matter

WITH ranked AS (
SELECT
id,
email,
created_at,
ROW_NUMBER() OVER (
PARTITION BY email
ORDER BY created_at DESC, id DESC
) AS rn
FROM subscribers
)
SELECT id, email, created_at, (rn = 1) AS is_primary
FROM ranked
ORDER BY email, rn;
WITH ranked AS (
SELECT id,
ROW_NUMBER() OVER (
PARTITION BY email
ORDER BY created_at DESC, id DESC
) AS rn
FROM subscribers
)
DELETE FROM subscribers s
USING ranked r
WHERE s.id = r.id
AND r.rn > 1;
CREATE UNIQUE INDEX CONCURRENTLY idx_subscribers_email
ON subscribers (email);

Interactive Tools for This Topic

FAQ

Which tables does this file reference?
`subscribers`, `ranked`.