17.05 - Example: Ordering on a Column Name Alias - Advanced SQL Engine - Teradata Database

Teradata Vantage™ - SQL Data Manipulation Language

Product
Advanced SQL Engine
Teradata Database
Release Number
17.05
Release Date
January 2021
Content Type
Programming Reference
Publication ID
B035-1146-175K
Language
English (United States)

Define a column named j in table t1.

The following statement returns an error because j is defined to be an alias for the expression SUM(t1.j). However, when it is used in the ORDER BY clause, the system resolves it to the column j and not the expression aliased by j:

     SELECT t1.i, SUM(t1.j) AS j, t1.k
     FROM t1
     GROUP BY 1,3
     ORDER BY j;

The following statement works because the column name alias jj is not the same as the column name j, and there is no column named jj in the table definition.

     SELECT t1.i, SUM(t1.j) AS jj, t1.k
     FROM t1
     GROUP BY 1,3
     ORDER BY jj;