Skip to content

Instantly share code, notes, and snippets.

@sorentwo
Created March 7, 2014 22:08
Show Gist options
  • Select an option

  • Save sorentwo/9421244 to your computer and use it in GitHub Desktop.

Select an option

Save sorentwo/9421244 to your computer and use it in GitHub Desktop.
CTE / Window query for efficient current, prev, next id row retrieval
WITH linked AS (
SELECT id,
lead(id) OVER(ORDER BY id DESC) AS next_id,
lag(id) OVER(ORDER BY id ASC) AS prev_id
FROM snippets
WHERE snippets.mission_id = $1
)
SELECT snippets.* FROM snippets, linked WHERE linked.id = $2 AND snippets.id = linked.next_id;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment