Create a recursive view that has a β€œrecursive” statement in Teradata

I would like to create a recursive view in Teradata (i.e. CREATE RECURSIVE VIEW ) from the following reproducible example:

 CREATE VOLATILE TABLE vt1 ( foo VARCHAR(10) , counter INTEGER , bar INTEGER ) ON COMMIT PRESERVE ROWS; INSERT INTO vt1 VALUES ('a', 1, '1'); INSERT INTO vt1 VALUES ('a', 2, '2'); INSERT INTO vt1 VALUES ('a', 3, '2'); INSERT INTO vt1 VALUES ('a', 4, '4'); INSERT INTO vt1 VALUES ('a', 5, '1'); INSERT INTO vt1 VALUES ('b', 1, '3'); INSERT INTO vt1 VALUES ('b', 2, '1'); INSERT INTO vt1 VALUES ('b', 3, '1'); INSERT INTO vt1 VALUES ('b', 4, '2'); WITH RECURSIVE cte (foo, counter, bar, rsum) AS ( SELECT foo , counter , bar , bar AS rsum FROM vt1 QUALIFY ROW_NUMBER() OVER (PARTITION BY foo ORDER BY counter) = 1 UNION ALL SELECT t.foo , t.counter , t.bar , CASE WHEN cte.rsum < 3 THEN t.bar + cte.rsum ELSE t.bar END FROM vt1 t JOIN cte ON t.foo = cte.foo AND t.counter = cte.counter + 1 ) SELECT cte.* , CASE WHEN rsum < 5 THEN 0 ELSE 1 END AS tester FROM cte ORDER BY foo , counter ; 

This creates this conclusion:

 ╔═════╦═════════╦═════╦══════╦════════╗ β•‘ foo β•‘ counter β•‘ bar β•‘ rsum β•‘ tester β•‘ ╠═════╬═════════╬═════╬══════╬════════╣ β•‘ a β•‘ 1 β•‘ 1 β•‘ 1 β•‘ 0 β•‘ β•‘ a β•‘ 2 β•‘ 2 β•‘ 3 β•‘ 0 β•‘ β•‘ a β•‘ 3 β•‘ 2 β•‘ 5 β•‘ 1 β•‘ β•‘ a β•‘ 4 β•‘ 4 β•‘ 4 β•‘ 0 β•‘ β•‘ a β•‘ 5 β•‘ 1 β•‘ 5 β•‘ 1 β•‘ β•‘ b β•‘ 1 β•‘ 3 β•‘ 3 β•‘ 0 β•‘ β•‘ b β•‘ 2 β•‘ 1 β•‘ 4 β•‘ 0 β•‘ β•‘ b β•‘ 3 β•‘ 1 β•‘ 5 β•‘ 1 β•‘ β•‘ b β•‘ 4 β•‘ 2 β•‘ 2 β•‘ 0 β•‘ β•šβ•β•β•β•β•β•©β•β•β•β•β•β•β•β•β•β•©β•β•β•β•β•β•©β•β•β•β•β•β•β•©β•β•β•β•β•β•β•β•β• 

What I ultimately would like to β€œsave” as a view. I tried CREATE RECURSIVE VIEW and several options, but I think I don’t understand how to get around the WITH RECURSIVE cte .

For the question of what is happening, see this question.

+7
sql recursion view recursive-query teradata
source share
2 answers

Well, it was actually more complicated than I thought:

 create recursive view db.test_view ( foo, counter,bar,rsum) as (SELECT foo, counter, bar, bar AS rsum FROM vt1 QUALIFY ROW_NUMBER() OVER (PARTITION BY foo ORDER BY counter) = 1 UNION ALL SELECT t.foo, t.counter, t.bar, CASE WHEN cte.rsum < 5 THEN t.bar + cte.rsum ELSE t.bar END FROM vt1 t JOIN test_view cte ON t.foo = cte.foo AND t.counter = cte.counter + 1 ) 

Do not qualify a recursive join for presentation. IE, JOIN test_view , not JOIN db.test_view .

+3
source share

Flying tables are stored in the user spool space quota and must be qualified with your username.
PS Why do you use volatile tables in the first place?

0
source share

All Articles