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

SQL Data Manipulation Language

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

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;