This article is half-done without your Comment! *** Please share your thoughts via Comment ***
In this post, I am sharing a demonstration on how to RECURSIVE VIEW in PostgreSQL?
Recursive View is similar to Recursive CTE. Last year, I shared a post on Fibonacci Series where I used Recursive CTE.
Here, I am converting that same Recursive CTE into the Recursive View.
PostgreSQL: Fibonacci Series Function for Database Developer Interview
Please check the below demonstration:
Create a RECURSIVE VIEW for finding the Fibonacci Series:
1 2 3 4 5 6 7 8 9 |
CREATE RECURSIVE VIEW Fibonacci(Num1,Num2) AS ( VALUES(0,1) UNION ALL SELECT GREATEST(Num1,Num2),Num1 + Num2 AS FibonacciSeries FROM Fibonacci WHERE Num2 < 50 ); |
SELECT the view data:
1 2 |
select Num1 as FibonacciSeries from Fibonacci; |
The Result:
1 2 3 4 5 6 7 8 9 10 11 12 |
fibonacciseries --------------- 0 1 1 2 3 5 8 13 21 34 |
Leave a Reply