Re: Question, how intelligent is optimizer with subplans?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Greg Stark <gsstark(at)MIT(dot)EDU>
Cc: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: Question, how intelligent is optimizer with subplans?
Date: 2003-01-20 21:09:45
Message-ID: 21650.1043096985@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Greg Stark <gsstark(at)MIT(dot)EDU> writes:
> On second thought what I wanted to do should work, I think. I'm not clear why
> the first of these works fine but the second doesn't. What I want to do is
> effectively the second of these:
> slo=> select (select count(*) from t2) as x from t order by x;
> x
> ---
> 0
> (1 row)

> slo=> select (select count(*) from t2) as x from t order by sign(x);
> ERROR: Attribute "x" not found

The first of those works because the SQL spec says it should: unadorned
names appearing in ORDER BY are output column names per the spec.

The second of those is not legal per SQL spec (you can't put anything
except an unadorned output column name or number in ORDER BY, according
to the spec). Postgres accepts expressions in ORDER BY, but we consider
them to be expressions in the input column names.

You could do something like

SELECT x
FROM
(select count(*) as x from t2) as ss
ORDER BY sign(x);

regards, tom lane

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Stephan Szabo 2003-01-20 21:12:00 Re: Help on query plan. (was: select like and indexes)
Previous Message elein 2003-01-20 21:02:03 Re: Writing apps for ORDBMS