Click to See Complete Forum and Search --> : Implementing fibonacci in SQL2000


technowerk
November 19th, 2007, 05:34 AM
Hi,

Can somebody help me implement fibonacci in SQL2000?

I have a table 'CUSTOMER' as follows

CUSTOMER AMOUNT

A 1000
B 2000
c 8000
d -6000
e 11000
f -3500

I need to apply fibonacci to get another column 'BALANCE' in the same table as follows

CUSTOMER AMOUNT BALANCE

A 1000 1000
B 2000 3000
c 8000 11000
d -6000 5000
e 11000 16000
f -3500 12500

Balance is cumulative addition of all values in 'AMOUNT'. How can I do this?
Thanking you in anticipation.

KrisSimonis
November 19th, 2007, 05:48 AM
is this a one-time conversion type change, or does this column have to be calculated every time someone does a select on the table?
If it's a one-time thing, best way to do this IMHO, is use a cursor to walk through the whole table, and update al records with the proper amount using a fibonacci formula.

DECLARE @Balance as int
DECLARE @Customer as varchar(4)
DECLARE @Amount as int

DECLARE fibo_cursor CURSOR FOR
SELECT Customer, Amount
FROM CUSTOMER
ORDER BY Customer

SET @Balance = 0

OPEN CURSOR Fibo_cursor

FETCH NEXT FROM Fibo_cursor
INTO @Customer, @Amount

WHILE @@FETCH_STATUS <> 0
BEGIN
SET @Balance = @Balance + @Amount
UPDATE Customer
SET Balance = @Balance
WHERE Customer = @Customer

FETCH NEXT FROM Fibo_cursor
INTO @Customer, @Amount
END



Something like this should be aproximately it. I didn't test this however.