Thursday, January 5, 2017

SQL Server -Fibonacci series Query in T-SQL

---Fibonacci series in T-SQL
---Scnario 1
SELECT
    FLOOR(POWER(( 1 + SQRT(5) ) / 2.0, number) / SQRT(5) + 0.5) AS fib_number
FROM master..spt_values
WHERE TYPE = 'p' AND number BETWEEN 1 AND 70
go
declare @Y int
declare @z int
declare @fib int
set @Y=1
set @z=1
set @fib=0
print @y
print @z
while @fib <2000
begin
set @fib= @Y +@z
print @fib
set @Y=@z
set @z=@fib
end

go
---Scnario 2
WITH fib(num,fib,fib2) as
(select 0 as expr1,0 as expr2,1 as expr3
union all
select num+1 as expr1,fib+fib2 as expr2,fib
from fib as fib_2
where (num<44)
)
select num,fib as fibonacci_number from fib as fib_1 where num>0

No comments:

Search This Blog

DAX - Grouping on multiple columns and the count

Please go thorugh the below URL for entire soultion. http://community.powerbi.com/t5/Desktop/DAX-Grouping-on-multiple-columns-and-the-cou...