pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Joseph Shraibman <jks@selectacast.net>
Cc: pgsql-sql <pgsql-sql@postgresql.org>
Subject: Re: WHERE on an alias
Date: Fri, 24 Aug 2001 16:56:44 -0400
Message-ID: <8808.998686604@sss.pgh.pa.us> (raw)
In-Reply-To: <3B8698E7.4080106@selectacast.net>
References: <3B8698E7.4080106@selectacast.net>

Joseph Shraibman <jks@selectacast.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



view thread (9+ messages)  latest in thread

Message-ID: <8808.998686604@sss.pgh.pa.us>
Permalink:  ../8808.998686604@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/8808.998686604@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: tgl@sss.pgh.pa.us, jks@selectacast.net
  Subject: Re: WHERE on an alias
  In-Reply-To: <8808.998686604@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox