Skip to content

Instantly share code, notes, and snippets.

@JohnAmican
Last active October 22, 2019 14:12
Show Gist options
  • Save JohnAmican/82d794c28c7037af9fcc8bcb5c0cf51a to your computer and use it in GitHub Desktop.
Save JohnAmican/82d794c28c7037af9fcc8bcb5c0cf51a to your computer and use it in GitHub Desktop.
WITH q AS (
SELECT id,
root_id,
version,
lag(version) OVER (PARTITION BY root_id ORDER BY root_id, version) as prev_version,
lead(version) OVER (PARTITION BY root_id ORDER BY root_id, version) as next_version,
latest
FROM trades
ORDER BY root_id, version
)
SELECT id, root_id, version, latest
FROM q
WHERE (next_version IS NOT NULL AND next_version - version <> 1)
OR (prev_version IS NULL AND version <> 1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment