Example: GROUP BY Clause Usage - Advanced SQL Engine - Teradata Database

SQL Data Definition Language Syntax and Examples

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

Similarly to aggregates and ordered analytic functions, a GROUP BY clause is valid when coded as part of the seed statement in a recursive view definition, but is not valid when coded as part of the recursive statement.

The first example demonstrates correct usage of a GROUP BY clause. In this example, the GROUP BY is coded as part of the seed statement.

    CREATE RECURSIVE VIEW aggregation (source,destination,mycount) AS (
      SELECT source, destination, 0 AS mycount
      FROM flights
      GROUP BY source, destination
    UNION ALL
      SELECT in1.source, out1.destination, in1.mycount + 1
      FROM aggregation AS in1, flights AS out1
      WHERE in1.destination = out1.source
      AND   in1.mycount <=100);

The second example demonstrates non-valid usage of a GROUP BY clause. In this example, the GROUP BY clause is coded as part of the recursive statement.

    CREATE RECURSIVE VIEW aggregation (source,destination,mycount) AS (
      SELECT source, destination, 0 AS mycount
      FROM flights
    UNION ALL
      SELECT in1.source, out1.destination, in1.mycount + 1
      FROM aggregation AS in1, flights AS out1
      WHERE in1.destination = out1.source
      AND   in1.mycount <=100
      GROUP BY in1.source, out1.destination);