15.10 - Example: Aggregate and Ordered Analytic Function Usage - Teradata Database

Teradata Database SQL Data Definition Language Syntax and Examples

Product
Teradata Database
Release Number
15.10
Published
December 2015
Language
English (United States)
Last Update
2018-06-05
dita:mapPath
SQL_DDL_15_10.ditamap
dita:ditavalPath
ft:empty

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;