Depth First Reporting - Advanced SQL Engine - Teradata Database

SQL Data Definition Language Detailed Topics

Product
Advanced SQL Engine
Teradata Database
Release Number
17.05
17.00
Published
June 2020
Language
English (United States)
Last Update
2021-01-24
dita:mapPath
jpx1556733107962.ditamap
dita:ditavalPath
lze1555437562152.ditaval
dita:id
B035-1184
lifecycle
previous
Product Category
Teradata Vantage™

You can simulate a DFS result by ordering the final result by the path of the recursion. The path in this context means how a row is found through recursive iterations. In this example, the path is the concatenation of the city names.

    CREATE RECURSIVE VIEW reachable_from (destin,cost,depth,path) AS (
      SELECT root.destin, root.cost, 0 AS depth, destin AS path
      FROM flights AS root
      WHERE root.source = 'paris'
    UNION ALL
      SELECT result.destin,seed.cost+result.cost,seed.depth+1 AS depth,
         seed.path||' '||result.destin
      FROM reachable_from AS seed, flights AS result
      WHERE seed.destin = result.source
      AND   depth <= 20);

    SELECT *
    FROM reachable_from
    ORDER BY path;