Created
March 6, 2015 23:06
-
-
Save edpaget/3a597cefe780fca99a67 to your computer and use it in GitHub Desktop.
This file contains 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 RECURSIVE rand(ids, id, done) AS ( | |
SELECT ARRAY[]::int[], sub.id, 0 FROM (SELECT t.id FROM test t WHERE t.name = 'asdf' AND t.id = ceiling(random() * 8)::integer) AS sub | |
UNION ALL | |
SELECT (n.ids || n.id), sub.id, done+1 FROM rand n, (SELECT t.id FROM test t WHERE t.name = 'asdf' AND t.id = ceiling(random() * 8)::integer) AS sub WHERE NOT done = 5 | |
) | |
SELECT n.* FROM rand n; | |
SELECT t.* FROM test t, rand n WHERE t.id = ANY(n.ids) LIMIT 20; | |
WITH RECURSIVE t(n) AS ( | |
VALUES (1) | |
UNION ALL | |
SELECT n+1 FROM t WHERE n < 100 | |
) | |
SELECT n FROM t; | |
WITH RECURSIVE rand(ids, done) AS ( | |
SELECT ARRAY[sub.id]::int[], 0 FROM (SELECT t.id FROM test t WHERE t.name = 'asdf' AND t.id = ceiling(random() * 8)::integer union ALL NULL limit 1) AS sub | |
UNION ALL | |
SELECT (n.ids || sub.id), done + 1 FROM rand n, (SELECT t.id FROM test t WHERE t.name = 'asdf' AND t.id = ceiling(random() * 8)::integer union all NULL limit 1) AS sub WHERE array_length(n.ids, 1) < 20 | |
) | |
SELECT n.* FROM rand n; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment