Home » RDBMS Server » Server Administration » Generating a Fibonacii Series using the SQL
Generating a Fibonacii Series using the SQL [message #371671] Wed, 22 November 2000 08:11 Go to next message
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
What is a fibonacii series ? (any link or whatever ?) [message #371672 is a reply to message #371671] Wed, 22 November 2000 09:33 Go to previous messageGo to next message
Tittom
Messages: 15
Registered: November 2000
Junior Member
If you had any link or whatever which would explain what this is, thanks for giving it...
Re: What is a fibonacii series ? (any link or whatever ?) [message #371674 is a reply to message #371672] Thu, 23 November 2000 03:19 Go to previous messageGo to next message
John R
Messages: 156
Registered: March 2000
Senior Member
It's a sequence of numbers where (in the general case) each number is the sum of the two numbers preceeding it.
Re: Generating a Fibonacii Series using the SQL [message #371676 is a reply to message #371671] Thu, 23 November 2000 05:10 Go to previous messageGo to next message
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 Go to previous messageGo to next message
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 :)
Re: What is a fibonacii series ? (any link or whatever ?) [message #372389 is a reply to message #371674] Thu, 08 February 2001 00:05 Go to previous messageGo to next message
Shanthi
Messages: 8
Registered: October 2000
Junior Member
please tell me what is fibonacii sereis ?..I am a learner and want to know more abt it.
Re: What is a fibonacii series ? (any link or whatever ?) [message #372395 is a reply to message #371674] Thu, 08 February 2001 12:24 Go to previous message
Orson
Messages: 1
Registered: February 2001
Junior Member
Fibonacii sequence is a sequence that consists fo adding the last and preceding element to generate the next element.
1 1 2 3 5 8 13 21 34...
a a a+a=b a+b=c b+c=d c+d=e e+f=g...
Previous Topic: Function based index not used
Next Topic: Simple Trace !
Goto Forum:
  


Current Time: Wed May 15 14:59:06 CDT 2024