Example: Recursive Common Table Expressions in a WITH Modifier - Teradata VantageCloud Lake

Lake - Working with SQL

Deployment
VantageCloud
Edition
Lake
Product
Teradata VantageCloud Lake
Release Number
Published
February 2025
ft:locale
en-US
ft:lastEdition
2025-11-21
dita:mapPath
jbe1714339405530.ditamap
dita:ditavalPath
pny1626732985837.ditaval
dita:id
jbe1714339405530

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