Generating a Fibonacii Series using the SQL [message #371671] |
Wed, 22 November 2000 08:11 |
S.Dharanikumar
Messages: 1 Registered: November 2000
|
Junior Member |
|
|
Hi,
I need to know as to how to generate the fibonacii series using plain sql command. Is there any way of doing it apart from using functions or procedures.
I need a quicker response and immediate solution for this, if one such exists.
Dharanikumar.s
|
|
|
|
|
Re: Generating a Fibonacii Series using the SQL [message #371676 is a reply to message #371671] |
Thu, 23 November 2000 05:10 |
Tittom
Messages: 15 Registered: November 2000
|
Junior Member |
|
|
Here is the result of my search :
I created this table :
create table ztbn (val number);
I inserted 2 initial rows :
insert into ztbn values(0);
insert into ztbn values(1);
Then I executed this sql statement :
insert into ztbn values(
(select sum(val)
from (select val from ztbn order by val desc)
where rownum<=2)
)
Each time you execute this SQL statement, it will add the two highest values in the table, and insert the result in a new row.
I hope this helps.
Very interesting...
Tittom.
|
|
|
This one is better [message #371677 is a reply to message #371671] |
Thu, 23 November 2000 05:30 |
Tittom
Messages: 15 Registered: November 2000
|
Junior Member |
|
|
The weakness of the first solution I suggested is that it supposes that at each step, the value increases. Which is not necessarily the case when you start your series with negative values.
Here is a better solution :
Here is the table I created :
create table ztbn(val number, rank number);
The val column contains the actual values of the series. The rank column is for sorting the rows and getting the two most recently created rows.
I inserted these initial rows :
insert into ztbn values(3, 1);
insert into ztbn values(-2, 2);
And this is the sql statement for inserting a new value based on the 2 previous ones :
insert into ztbn values(
(select sum(val)
from (select val from ztbn order by rank desc)
where rownum<=2),
(select count(*) + 1 from ztbn)
)
There might be better solutions... If so, thanks for posting them.
Very very interesting :)
Tittom.
Very very intersting :)
|
|
|
|
|