Skip to content

Instantly share code, notes, and snippets.

@faulker
Created May 23, 2017 17:03
Show Gist options
  • Save faulker/90eed124bb746c26cf9a6f6881dccee4 to your computer and use it in GitHub Desktop.
Save faulker/90eed124bb746c26cf9a6f6881dccee4 to your computer and use it in GitHub Desktop.
-- Delete all duplicate entries that match 'PARTITION BY' section and are 'completed' and 'finished_at' falls between timestamps
DELETE FROM edge_scores AS a
WHERE a.id IN (
SELECT id
FROM (
SELECT
id,
ROW_NUMBER()
OVER (
PARTITION BY user_id, assignment_id, finished_at::DATE, asset_id, score, completed
ORDER BY id) AS rnum
FROM edge_scores
WHERE completed = TRUE
AND finished_at between finished_at - interval '3 min' and finished_at + interval '3 min'
ORDER BY id DESC) t
WHERE t.rnum > 1
ORDER BY id DESC);
-- Selects duplicate entries
SELECT *
FROM (
SELECT
*,
ROW_NUMBER()
OVER (
PARTITION BY user_id, assignment_id, finished_at::DATE, asset_id, score, completed
ORDER BY id) AS rnum
FROM edge_scores
WHERE completed = TRUE
AND finished_at between finished_at - interval '5 min' and finished_at + interval '5 min'
ORDER BY id DESC) t
WHERE t.rnum > 1
ORDER BY id DESC;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment