Example: Recursive Common Table Expressions in a WITH Modifier - Analytics Database - Teradata Vantage

SQL Data Manipulation Language

Deployment
VantageCloud
VantageCore
Edition
Enterprise
IntelliFlex
VMware
Product
Analytics Database
Teradata Vantage
Release Number
17.20
Published
June 2022
Language
English (United States)
Last Update
2024-04-05
dita:mapPath
pon1628111750298.ditamap
dita:ditavalPath
qkf1628213546010.ditaval
dita:id
esx1472246586715
lifecycle
latest
Product Category
Teradata Vantage™

If you mix nonrecursive queries with recursive queries in a WITH modifier, you must specify all forward CTE references or all backward CTE references. You cannot mix forward CTE references and backward CTE references.

Following is the table definition for this example.

CREATE TABLE t1(a1 INT, b1 INT);

These statements insert rows of data into the table.

INS t1(1,2);
INS t1(1,4);
INS t1(2,3);
INS t1(3,4);
This statement includes the recursive queries s3 and s4.
WITH
RECURSIVE s3 (MinVersion)  AS (SELECT a1 FROM t1 WHERE a1 > 1
                               UNION ALL
                              SEL MinVersion FROM s3 WHERE MinVersion > 3),
RECURSIVE s4(MinVersion)  AS (SELECT a1 FROM t1 WHERE a1 = 2
                              UNION ALL
                              SEL MinVersion FROM S4 WHERE MinVersion > 2)
SEL * FROM s3,s4;

The query returns this answer set:

MinVersion MinVersion
3 2
2 2