Discussions
Categories
- 196.8K All Categories
- 2.2K Data
- 238 Big Data Appliance
- 1.9K Data Science
- 450.3K Databases
- 221.7K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 550 MySQL Community Space
- 478 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3K ORDS, SODA & JSON in the Database
- 544 SQLcl
- 4K SQL Developer Data Modeler
- 187K SQL & PL/SQL
- 21.3K SQL Developer
- 295.8K Development
- 17 Developer Projects
- 138 Programming Languages
- 292.5K Development Tools
- 107 DevOps
- 3.1K QA/Testing
- 646K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 154 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.1K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 18 Java Essentials
- 160 Java 8 Questions
- 86K Java Programming
- 80 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 204 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 437 LiveLabs
- 38 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 171 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 232 Portuguese
add previous numer with current numebr

152933
Member Posts: 404
Hi All,
i want to add previous numer with current numebr ,that total numer with current number and so on
ex:
first 0+1=1
1+1=2
2+1=3
3+2=5
5+3=8..
to be generated upto 21 .Please help on this
0 1 1 2 3 5 8 13 21
Regards,
MR
i want to add previous numer with current numebr ,that total numer with current number and so on
ex:
first 0+1=1
1+1=2
2+1=3
3+2=5
5+3=8..
to be generated upto 21 .Please help on this
0 1 1 2 3 5 8 13 21
Regards,
MR
Answers
-
Hi,
Something like thisselect sum(col1) over (order by col2) from tab1
Regards
Anurag -
If you are on 10g check out: [Oracle 10g Fibonacci Sequence|http://www.oracle.com/technology/oramag/code/tips2006/052906.html]
-
Centinul wrote:Not an optimal use of MODEL:
If you are on 10g check out: [Oracle 10g Fibonacci Sequence|http://www.oracle.com/technology/oramag/code/tips2006/052906.html]select s seq from dual model return all rows dimension by(0 d) measures(0 s) rules( s[1] = 1, s[for d from 2 to 12 increment 1] = s[cv()-2] + s[cv()-1] ) / SEQ ---------- 0 1 1 2 3 5 8 13 21 34 55 SEQ ---------- 89 144 13 rows selected. SQL>
SY. -
with t as ( select rownum-1 n, ((1+ sqrt(5))/2) phi from dual connect by rownum < 10 ) select n, round(( power(phi,n) - power(1 - phi,n))/sqrt(5)) as fib from t
Gives:N FIB 0 0 1 1 2 1 3 2 4 3 5 5 6 8 7 13 8 21
-
Solomon Yakobson wrote:I just posted it as an example.Centinul wrote:Not an optimal use of MODEL:
If you are on 10g check out: [Oracle 10g Fibonacci Sequence|http://www.oracle.com/technology/oramag/code/tips2006/052906.html] -
Or (of course) the xquery way (in 11g):
SQL> select * from xmltable('declare function local:f($i) { if ($i = 0 or $i = 1) then 1 else local:f($i - 1) + local:f($i - 2) }; element e {for $i in 0 to xs:int(n) return local:f($i)}' passing xmlelement("n",8) columns fibonacci varchar2(30) path '.') / FIBONACCI ------------------------------ 1 1 2 3 5 8 13 21 34 1 row selected.
-
In addition to the other examples, just for fun and since this is (again) a nice question:
http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:15844208486026#1260264200346087371
http://en.wikipedia.org/wiki/Fibonacci_number -
In this case, it is effective to use recursive with clause B-)
Hehe I have used PostgreSQL8.4 because I do not have Oracle11gR2 :8}with recursive rec(SumVal,PreSum) as( select 1,0 union all select PreSum+SumVal,SumVal from rec where SumVal<21) select*from rec; sumval | PreSum --------+-------- 1 | 0 1 | 1 2 | 1 3 | 2 5 | 3 8 | 5 13 | 8 21 | 13
-
Hehe I have used Math B-)
http://en.wikipedia.org/wiki/Fibonacci_numberselect 1/sqrt(5) *(power((1+sqrt(5))/2,rowNum) -power((1-sqrt(5))/2,rowNum)) as val from dict where RowNum <= 10; VAL --- 1 1 2 3 5 8 13 21 34 55
-
I have used PostgreSQL8.4 because I do not have Oracle11gR2 :8}In Oracle it would be sth like
SQL> with rec (n, f) as ( select 1 n, 1 f from dual union all select n + f, n from rec where n <= 21 ) -- -- select f from rec F ---------- 1 1 2 3 5 8 13 21 8 rows selected.
This discussion has been closed.