Skip to content

Instantly share code, notes, and snippets.

@infogulch
Created October 3, 2012 21:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save infogulch/3830136 to your computer and use it in GitHub Desktop.
Save infogulch/3830136 to your computer and use it in GitHub Desktop.
table t1 contains 4 rows, with id = 1, 2, 3, 5
SELECT X, Y
FROM (
SELECT A.id as X, (SELECT MIN(b.id) FROM t1 AS B WHERE B.id > A.id) as Y
FROM t1 AS B
) C
WHERE Y IS NOT NULL
id1 | id2
-----+------
1 | 2
2 | 3
3 | 5
(4 rows)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment