X-Original-To: pgsql-sql@postgresql.org Received: from spampd.localdomain (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 2634E475EC9 for ; Wed, 26 Mar 2003 04:55:44 -0500 (EST) Received: from fzkmail2.fzk.de (fzkmail2.fzk.de [141.52.27.52]) by postgresql.org (Postfix) with SMTP id 09DA1475D91 for ; Wed, 26 Mar 2003 04:55:43 -0500 (EST) Received: FROM resy5.fzk.de BY fzkmail2.fzk.de ; Wed Mar 26 10:55:44 2003 +0100 Received: œby rodos.fzk.de with ESMTP (8.8.6 (PHNE_17135)/8.7.1) id KAA20048; Wed, 26 Mar 2003 10:55:43 +0100 (MET) Message-ID: <3E81791F.8D120A60@rodos.fzk.de> Date: Wed, 26 Mar 2003 10:55:43 +0100 From: Christoph Haller X-Mailer: Mozilla 4.77 [en] (X11; U; HP-UX B.10.20 9000/780) X-Accept-Language: en MIME-Version: 1.0 To: pgsql-sql@postgresql.org Cc: pgsql-sql@seahat.com Subject: Re: column label Content-Type: text/plain; charset=iso-8859-2 Content-Transfer-Encoding: 7bit X-Spam-Status: No, hits=-9.8 required=5.0 tests=BAYES_01,QUOTED_EMAIL_TEXT autolearn=ham version=2.50 X-Spam-Level: X-Spam-Checker-Version: SpamAssassin 2.50 (1.173-2003-02-20-exp) X-Archive-Number: 200303/577 X-Sequence-Number: 12615 > >> Ok I have a query in a program that looks something like this > >> > >> select > >> reference_number as foo, > >> '(10,10)'::point <-> point(x,y) as distance > >> from imagemap > >> where > >> index_number =3D '2322' and > >> block_number =3D '140' > >> order by distance > >> limit 1; > >> > >> I wanted to add a clause in where 'and distance < 30' > >> but according to the postgres manual it says I cannot do that for > >> WHERE or HAVING clauses, instead use the real name. > >> > >> However dropping the 'as distance' from my query gives me a column > >> name of '?column?' which happens to not be worth a dang thing. > >> > >> How can it be done? > >> > >> (I suddenly have a feeling its going to require a subselect) > > Your feeling is 50% accurate. If you want to use subselect, throw "<10" > > into this subselect, because you will have performance loss. > > > > You can also use whole function in where clause and order by: > > select > > reference_number as foo, > > '(10,10)'::point <-> point(x,y) as distance > > from imagemap > > where > > index_number =3D '2322' and > > block_number =3D '140' > > where '(10,10)'::point <-> point(x,y)<10 > > order by '(10,10)'::point <-> point(x,y) > > limit 1; > > > Thats kind of a pain... I'd rather perform the calculation once and then > do my order and where on the one value. Is there any reason why postgres > does not recognize labels under WHERE? > So how about select foo, distance from ( select reference_number as foo, '(10,10)'::point <-> point(x,y) as distance from imagemap where index_number = '2322' and block_number = '140' ) as bar where distance < 30 order by distance limit 1; The reason why postgres does not recognize labels under WHERE is the WHERE clause is evaluated before column aliasing takes place. But there is this valuable feature of using a sub-SELECT as from_item, so this should not be a problem at all. Alternatively you could use select reference_number, '(10,10)'::point <-> point(x,y) from imagemap alias (foo, distance) where index_number = '2322' and block_number = '140' and distance < 30 order by distance limit 1; Does this help? Regards, Christoph