Created
March 7, 2014 22:08
-
-
Save sorentwo/9421244 to your computer and use it in GitHub Desktop.
CTE / Window query for efficient current, prev, next id row retrieval
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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