Re: where cannot use alias name of column? - Mailing list pgsql-general

From Peter Eisentraut
Subject Re: where cannot use alias name of column?
Date
Msg-id Pine.LNX.4.30.0109131553390.680-100000@peter.localdomain
Whole thread Raw
In response to where cannot use alias name of column?  (Giorgio Volpe <giorgio.volpe@gtngroup.it>)
List pgsql-general
Giorgio Volpe writes:

> May be it's my ignorance about sql ...
> but why cannot i use alias name of a column in a where clause?
>
> # select key as cc from mytable where cc > 0;
> ERROR:  Attribute 'cc' not found

The processing order of this command is, perhaps unintuitively, FROM ->
WHERE -> SELECT [-> ORDER BY].  The aliases introduced in the SELECT list
are not available in the WHERE expression (but they would be in the ORDER
BY list).  If you want to use an alias in the WHERE clause you have to
introduce it in the FROM clause, such as:

SELECT * FROM mytable AS myalias (xx, yy, zz) WHERE zz > 0;

This may or may not be actually useful in your case.

--
Peter Eisentraut   peter_e@gmx.net   http://funkturm.homeip.net/~peter


pgsql-general by date:

Previous
From: "Thurstan R. McDougle"
Date:
Subject: Re: count of occurences PLUS optimisation
Next
From: Martijn van Oosterhout
Date:
Subject: Re: count of occurences PLUS optimisation