SQL 如何生成斐波那契数列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21746100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 01:03:05  来源:igfitidea点击:

How to generate Fibonacci Series

sqlsql-server

提问by praveen

How to Generate Fibonacci series in sql !

如何在 sql 中生成斐波那契数列!

I need to generate Fibonacci series 0 1 1 2 3 5 8 13 21 ... N

我需要生成斐波那契数列 0 1 1 2 3 5 8 13 21 ... N

I did this easily using C-code I need to do this using Sql !

我使用 C 代码轻松做到了这一点我需要使用 Sql 做到这一点!

回答by 124

Try This Simple Query:

试试这个简单的查询:

1) For Result In Row-by-Row (Single Column, Multiple Rows)

1)对于逐行结果(单列,多行)

WITH Fibonacci (PrevN, N) AS
(
     SELECT 0, 1
     UNION ALL
     SELECT N, PrevN + N
     FROM Fibonacci
     WHERE N < 1000000000
)
SELECT PrevN as Fibo
     FROM Fibonacci
     OPTION (MAXRECURSION 0);

Output 1:

输出 1:

enter image description here

在此处输入图片说明

2) For Result in Only One Row (Comma sepreted, in Single Cell)

2) 对于只有一行的结果(逗号分隔,在单个单元格中)

WITH Fibonacci (PrevN, N) AS
(
 SELECT 0, 1
    UNION ALL
    SELECT N, PrevN + N
    FROM Fibonacci
    WHERE N < 1000000000
)
SELECT Substring(
    (SELECT cast(', ' as varchar(max)) + cast(PrevN as varchar(max)
);
FROM Fibonacci
FOR XML PATH('')),3,10000000) AS list

Output 2:enter image description here

输出 2:在此处输入图片说明

回答by vhadalgi

Try this !

尝试这个 !

declare @a int
declare @b int
declare @c int
Declare @i int

set @a=0
set @b=1
set @i=0
set @c=0
Print 'Fibonacci Series'
print @a
print @b
while @i<10 
Begin
set @c=@a+@b
print @c
set @i=@i+1
set @a=@b
set @b=@c
end

Outputs !

输出!

Fibonacci Series

0 1 1 2 3 5 8 13 21 34 55 89

0 1 1 2 3 5 8 13 21 34 55 89

回答by TechDo

Please try:

请尝试:

SELECT 0  AS fib_number UNION ALL
SELECT
    FLOOR(POWER(( 1 + SQRT(5) ) / 2.0, number) / SQRT(5) + 0.5)
FROM master..spt_values
WHERE TYPE = 'p' AND number BETWEEN 1 AND 70

回答by praveen

Try this :-

尝试这个 :-

 Declare @Fib int = 5
 ;with cte as
 (
   Select 0 as Previous,1 as Next ,0 as Level 
   union all
   Select Next,Next + Previous,Level +1 from cte 
   where Level +1 <@Fib
 )
 Select Previous as FibonacciSeries from cte

回答by MusicLovingIndianGirl

This is for generating the first 10 numbers in the series.

这是用于生成系列中的前 10 个数字。

DECLARE @NoOne INT,  @NoTwo INT
DECLARE @FibonacciTable TABLE (Number INT NOT NULL)
--Insert first two numbers 0 and 1
INSERT @FibonacciTable (Number) SELECT 0 UNION ALL SELECT 1
SELECT @NoOne = 0, @NoTwo = 1

WHILE (SELECT COUNT(*) FROM @FibonacciTable) < 10
BEGIN
    INSERT @FibonacciTable (Number) VALUES(@NoOne + @NoTwo)
    SELECT @NoTwo = @NoOne + @NoTwo, @NoOne = @NoTwo - @NoOne
END
SELECT * FROM @FibonacciTable   
GO

回答by muhammed sümer

CREATE TABLE #Fibonacci (value BIGINT)
GO

INSERT INTO #Fibonacci(value) SELECT 0
INSERT INTO #Fibonacci(value) SELECT 1
SELECT * FROM #Fibonacci
GO

INSERT INTO #Fibonacci(value) 
SELECT SUM(value) FROM ( SELECT TOP 2 * FROM #Fibonacci ORDER BY value DESC ) AS value
GO 10   --  Loop insert 10 value...

SELECT * FROM #Fibonacci
DROP TABLE #Fibonacci

Result Code

结果代码