Monday, April 08, 2013

Easy way to generate Fibonacci number with oracle sql

You could use model but there is an easy way


With FB(k1, k2)
AS
(
select 0 k1, 1 k2 from dual
union all
select k2 as k1, k1 + k2 as k2 from fb where greatest(k1, k2) < 100
)
select k1 from fb


K1
0
1
1
2
3
5
8
13
21
34
55
89

The strange thing is that Oracle needs both columns in the where clause. If you only have one (like where k1 < 100), you will get error