我不知道如何在SQL中编写这个递归。当我有两个初始假设时,如何处理CTE? 以下简单示例:
我尝试写下面的内容,但遗憾的是我不知道如何应对:
with recur(n,results) as
(
select 1,2
union all
select 2,3
union all
select
/*how to write this pattern?*/
where n<
)
select * from recur
你有什么想法吗?
答案 0 :(得分:2)
似乎您想使用递归CTE生成斐波那契数字。
尝试这样的事情:
WITH CTE AS (
SELECT 1 AS N, 2 AS A, 3 AS B
UNION ALL
SELECT N+1 AS N, B AS A, A+B AS B
FROM CTE
WHERE N<10
)
SELECT A FROM CTE