Re: WHERE on an alias

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Joseph Shraibman <jks(at)selectacast(dot)net>
Cc: pgsql-sql <pgsql-sql(at)postgresql(dot)org>
Subject: Re: WHERE on an alias
Date: 2001-08-24 20:56:44
Message-ID: 8808.998686604@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Joseph Shraibman <jks(at)selectacast(dot)net> writes:
> playpen=# select a, sum(b) as dsum from taba where dsum > 5 group by(a);
> ERROR: Attribute 'dsum' not found

> Why can we GROUP BY on an alias but not do a WHERE on an alias?

Because WHERE is computed before the select's output list is.

Strictly speaking you shouldn't be able to GROUP on an alias either (the
SQL spec doesn't allow it). We accept that for historical reasons only,
ie, our interpretation of GROUP used to be wrong and we didn't want to
break applications that relied on the wrong interpretation.

Note that writing a GROUP on an alias does *not* mean the alias is only
computed once. It saves no computation, only writing out the expression
twice.

> I have a subselect that
> explain shows is being run twice if I have to put it in the WHERE clause.

Possibly you could restructure your query into something with a
subselect in the FROM clause?

regards, tom lane

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message jake johnson 2001-08-24 23:20:51 Re: DBD::Pg install error (freebsd)
Previous Message Stephan Szabo 2001-08-24 20:52:49 Re: WHERE on an alias