Re: Select aliasses in where and other places of the selectlist? - Mailing list pgsql-general

From Tom Lane
Subject Re: Select aliasses in where and other places of the selectlist?
Date
Msg-id 884.1048979094@sss.pgh.pa.us
Whole thread Raw
In response to Select aliasses in where and other places of the selectlist?  (Arjen van der Meijden <acm@tweakers.net>)
List pgsql-general
Arjen van der Meijden <acm@tweakers.net> writes:
> I know it isn't possible to do queries like:
> SELECT 1 AS c, c + 1 AS d;

> But is there a good reason not to support it or is it something like
> "not yet implemented", "not interesting" or "to complex to (easily)
> implement".

It's not supported because it would violate the SQL spec.  The spec is
perfectly clear about the scope of names, and a SELECT's output column
names aren't in scope anywhere in the SELECT itself (except in ORDER
BY).  If we treated them as if they were, we'd break queries that rely
on the spec-mandated scoping --- think about cases where the output
column names happen to conflict with column names available from the
input tables.

You can however use a sub-select:
SELECT * FROM
 (SELECT intfield AS a, intfield * intfield AS square FROM tableX) AS ss
WHERE a < 10 AND square < 50

Note that it'd be unwise to assume this technique will eliminate
double evaluations of expressions.  But it saves having to type them
more than once, at least.

            regards, tom lane


pgsql-general by date:

Previous
From: "Thomas T. Thai"
Date:
Subject: 7.3.2 Regression Failures Worth Looking Into
Next
From: Tom Lane
Date:
Subject: Re: 7.3.2 Regression Failures Worth Looking Into