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

Teradata Vantage™ - SQL Data Manipulation Language

Product
Advanced SQL Engine
Teradata Database
Release Number
17.05
Published
January 2021
Language
English (United States)
Last Update
2021-01-22
dita:mapPath
vnq1596660420420.ditamap
dita:ditavalPath
hoy1596145193032.ditaval

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