Reputation: 15
I don't have idea how to write this recursive in SQL. How handle with CTE when I have two initial assumptions? Below easy example:
I tried write something like below but unfortunately I don't know how handle with this:
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
Do you have any idea?
Upvotes: 0
Views: 274
Reputation: 5694
It seems you want to generate the Fibonacci numbers using a recursive CTE.
Try something like this:
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
Upvotes: 2