Example: Recursive Common Table Expressions in a WITH Modifier - Advanced SQL Engine - Teradata Database

SQL Data Manipulation Language

Product
Advanced SQL Engine
Teradata Database
Release Number
17.00
Published
September 2020
Language
English (United States)
Last Update
2021-01-23
dita:mapPath
qtb1554762060450.ditamap
dita:ditavalPath
lze1555437562152.ditaval
dita:id
B035-1146
lifecycle
previous
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