 3,733,249 Users
 2,246,737 Discussions
 7,856,634 Comments
Forum Stats
Discussions
Howdy, Stranger!
Categories
 380.9K All Categories
 2.1K Data
 203 Big Data Appliance
 1.9K Data Science
 446.1K Databases
 220.4K General Database Discussions
 3.7K Java and JavaScript in the Database
 22 Multilingual Engine
 506 MySQL Community Space
 459 NoSQL Database
 7.7K Oracle Database Express Edition (XE)
 2.8K ORDS, SODA & JSON in the Database
 437 SQLcl
 3.9K SQL Developer Data Modeler
 185.4K SQL & PL/SQL
 20.7K SQL Developer
 291.2K Development
 6 Developer Projects
 116 Programming Languages
 288K Development Tools
 96 DevOps
 3K QA/Testing
 645.2K Java
 16 Java Learning Subscription
 36.9K Database Connectivity
 148 Java Community Process
 104 Java 25
 22.1K Java APIs
 137.7K Java Development Tools
 165.3K Java EE (Java Enterprise Edition)
 12 Java Essentials
 138 Java 8 Questions
 85.9K Java Programming
 79 Java Puzzle Ball
 65.1K New To Java
 1.7K Training / Learning / Certification
 13.8K Java HotSpot Virtual Machine
 94.2K Java SE
 13.8K Java Security
 195 Java User Groups
 22 JavaScript  Nashorn
 Programs
 177 LiveLabs
 33 Workshops
 10.2K Software
 6.7K Berkeley DB Family
 3.5K JHeadstart
 5.7K Other Languages
 2.3K Chinese
 165 Deutsche Oracle Community
 1.2K Español
 1.9K Japanese
 225 Portuguese
Insert problem
586231
Member Posts: 4
Hi,
I have the following situation:
I have column A and column B and I need an insert statement that will work in the following way:
B(0) = A(0)
B(X) = B(X1)+A(X)
Can someone help?
Thanks in advance.
I have the following situation:
I have column A and column B and I need an insert statement that will work in the following way:
B(0) = A(0)
B(X) = B(X1)+A(X)
Can someone help?
Thanks in advance.
Comments

What is X?

Ah, is this your homework's requirement?
By the way, there is no need using table on RDBMS.SQL> create table Fibonacci 2 (x int, 3 a int, 4 b int) 5 ; Table created. SQL> insert into Fibonacci values (0,1,1); 1 row created. SQL> insert into Fibonacci values (1,2,1); 1 row created. SQL> declare 2 max_x int := 9; 3 begin 4 for i in 1..max_x loop 5 insert into Fibonacci 6 select i+1,a+b,a from Fibonacci where x=i; 7 end loop; 8 end; 9 / PL/SQL procedure successfully completed. SQL> select * from Fibonacci; X A B    0 1 1 1 2 1 2 3 2 3 5 3 4 8 5 5 13 8 6 21 13 7 34 21 8 55 34 9 89 55 10 144 89 11 rows selected.

Message was edited by:
ushitaki 
Does x> rownumber?

Yes, X is the rownumber

This should be the answer.
I don't think its possible using a single insert statement unless somebody comes with a great idea. 
Yeah, that covers it up quite nicely, with few modifications.
Thanks a lot. 
There is not 0 on rownumber, rownuber is origined from 1.
By the way...
For your homework, please refer [url http://en.wikipedia.org/wiki/Fibonacci_number]Fibonacci number, and program with PL/SQL or something by yourself.
Maybe, you get more good performance. 
if you want to calculate Fibonacci numbers  it can easily be done with model clause:
SQL> select * from dual 2 model 3 dimension by (0 x) 4 measures (0 A) 5 rules(A[for x from 1 to 15 increment 1]=A[CV()1]+nvl(A[CV()2],1)) 6 / X A   0 0 1 1 2 1 3 2 4 3 5 5 6 8 7 13 8 21 9 34 10 55 11 89 12 144 13 233 14 377 15 610 16 rows selected SQL>

if you want to calculate Fibonacci numbers  it can easily be done with model clause:Or with XQuery (though really slow):
michaels> select rownum  1 r, column_value fib from XMLTable ('declare function local:fib ($i) { if ($i = 0) then (0) else if ($i = 1) then (1) else if($i >= 2) then number(local:fib($i  1) + local:fib($i  2)) else (1) }; (: eof :) for $i in 0 to 15 return local:fib($i)') R FIB   0 0 1 1 2 1 3 2 4 3 5 5 6 8 7 13 8 21 9 34 10 55 11 89 12 144 13 233 14 377 15 610

btw.
there's even a math formula for generating fibonacci numbers quite elegantly:michaels> select level  1 r, round ((power ((1 + sqrt (5)) / 2, level  1)  power ((1  sqrt (5)) / 2, level  1)) / sqrt (5)) fib from dual connect by level <= 16 R FIB   0 0 1 1 2 1 3 2 4 3 5 5 6 8 7 13 8 21 9 34 10 55 11 89 12 144 13 233 14 377 15 610

Actually the problem is a bit more complicated that generating fibonacci numbers:
I have the following table structure:
ID TRANSACTIONS TOTAL_TRANSACTIONS_ACC

1  100  100
1  120  220
1  50  270
2  80  80
2  160  240
2  20  260
3  75  75
3  100  175
3  50  225
So I need to populate the TOTAL_TRANSACTIONS_ACC column with information from the TRANSACTIONS column in regards to the ID column
Any ideas? 
Right, nothing to do with fibonacci ;)Actually the problem is a bit more complicated that generating fibonacci numbersActually it is simpler now:
michaels> with t as ( select 1 id, 100 transactions from dual union all select 1 , 120 from dual union all select 1 , 50 from dual union all select 2 , 80 from dual union all select 2 , 160 from dual union all select 2 , 20 from dual union all select 3 , 75 from dual union all select 3 , 100 from dual union all select 3 , 50 from dual) select t.*, sum(transactions) over (partition by id order by rownum /* choose appropriate in your real scenario */) total_transactions_acc from t ID TRANSACTIONS TOTAL_TRANSACTIONS_ACC    1 100 100 1 120 220 1 50 270 2 80 80 2 160 240 2 20 260 3 75 75 3 100 175 3 50 225
This discussion has been closed.