Re: Select aliasses in where and other places of the selectlist?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Arjen van der Meijden <acm(at)tweakers(dot)net>
Cc: "'Postgre General'" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Select aliasses in where and other places of the selectlist?
Date: 2003-03-29 23:04:54
Message-ID: 884.1048979094@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Arjen van der Meijden <acm(at)tweakers(dot)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

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2003-03-29 23:29:06 Re: 7.3.2 Regression Failures Worth Looking Into
Previous Message Thomas T. Thai 2003-03-29 21:32:09 7.3.2 Regression Failures Worth Looking Into