17.10 - Example: Aggregate and Ordered Analytic Function Usage - Advanced SQL Engine - Teradata Database

Teradata Vantage™ - SQL Data Definition Language Syntax and Examples

Product
Advanced SQL Engine
Teradata Database
Release Number
17.10
Release Date
July 2021
Content Type
Programming Reference
Publication ID
B035-1144-171K
Language
English (United States)

The first recursive view definition demonstrates the valid use of an ordered analytic function, which is highlighted in bold typeface. The usage is valid because it is within the seed statement rather than the recursive statement of the definition.

    CREATE RECURSIVE VIEW reachable_from (source, destination, cost,
                           depth) AS (
      SELECT source, destination,  MSUM(flights.cost, 25,              flights.destination),  0 AS depth
      FROM flights
    UNION ALL
      SELECT in1.source, out1.destination, in1.cost + out1.cost,
             in1.depth + 1
      FROM reachable_from in1, flights AS out1
      WHERE in1.destination = out1.source
      AND   in1.depth <= 100);

The second recursive view definition demonstrates a non-valid use of the same ordered analytic function, which is again highlighted in bold typeface. The usage is not valid because it is within the recursive statement rather than the seed statement of the definition.

    CREATE RECURSIVE VIEW oaf_problem (source, destination, mcost,
                           depth) AS (
      SELECT source, destination, MSUM(cost, 25, destination), 
             0 AS depth
      FROM flights
    UNION ALL
      SELECT in1.source, out1.destination, 
             MSUM(in1.mcost + out1.cost, 25, out1.destination), 
              in1.depth + 1
      FROM oaf_problem AS in1, flights AS out1
      WHERE in1.destination = out1.source
      AND   in1.depth <= 100;