Skip to content

Instantly share code, notes, and snippets.

@komamitsu
Created February 11, 2015 08:01
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save komamitsu/c2a5c46a22d765df5aa2 to your computer and use it in GitHub Desktop.
Save komamitsu/c2a5c46a22d765df5aa2 to your computer and use it in GitHub Desktop.
Fibonacci number with "with recursive" in PostgreSQL
with recursive r(a, b) as (
select 0::int, 1::int
union all
select b, a + b from r where b < 1000
)
select a from r;
a
-----
0
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
(17 rows)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment