Received: from nts1.rtp.epa.gov (nts1.rtp.epa.gov [134.67.208.208]) by hub.org (8.9.3/8.9.3) with ESMTP id KAA55020 for ; Thu, 3 Feb 2000 10:22:34 -0500 (EST) (envelope-from volpe.mark@epamail.epa.gov) Received: from epamail.epa.gov (IDENT:mvs@wolfpack.rtpnc.epa.gov [134.67.136.18]) by nts1.rtp.epa.gov (8.9.3/8.9.3) with ESMTP id KAA16542 for ; Thu, 3 Feb 2000 10:25:27 -0500 (EST) Sender: mvs@nts1.rtp.epa.gov Message-ID: <38999D08.4D92B943@epamail.epa.gov> Date: Thu, 03 Feb 2000 10:21:44 -0500 From: Mark Volpe X-Mailer: Mozilla 4.7 [en] (X11; U; Linux 2.2.9-27mdk i586) X-Accept-Language: en MIME-Version: 1.0 CC: pgsql-sql@postgreSQL.org Subject: Re: [SQL] Question about SELECT and ORDER BY References: <200002031434.QAA01945@lorna.digsys.bg> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit I think "ORDER BY" must refer to an existing column. To get the desired result, try adding a third column containing the result of the calculation you want to sort by: SELECT table1.attr1, attr2, length(table.attr1) FROM table1 UNION ALL SELECT table2.attr1, attr3, length(table2.attr1) FROM table2 ORDER BY length; but you have to put up w/ the extra column. Then again, I also find this to work: SELECT table1.attr1 AS alias, attr2 INTO table3 FROM table1 UNION ALL SELECT table2.attr1, attr3 FROM table2; SELECT * from table3 ORDER BY length(alias); so go figure :-) Mark Vladimir Terziev wrote: > > Hi, > > I have two tables: > > table1 (attr1 text, attr2 int4) > > table2 (attr1 text, attr3 int4, attr4 bool) > > I have a query: > > SELECT table1.attr1 AS ALIAS, attr2 from table1 UNION ALL > SELECT table2.attr1, attr3 from table2 ORDER BY ALIAS; > > I that syntax all is OK. > I want to have a result from ORDER BY length(ALIAS), but this is imposible. > > When ORDER BY clause is in format ORDER BY length(attr1), the result is not > ORDERED BY length of attr1. > > What can I do? Are anybody have an idea? > > Vladimir > > ************