Received: from sss.sss.pgh.pa.us (sss.pgh.pa.us [209.114.166.2] (may be forged)) by hub.org (8.9.3/8.9.3) with ESMTP id SAA29907; Fri, 16 Jul 1999 18:00:12 -0400 (EDT) (envelope-from tgl@sss.pgh.pa.us) Received: from sss.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.sss.pgh.pa.us (8.9.1/8.9.1) with ESMTP id RAA11616; Fri, 16 Jul 1999 17:56:45 -0400 (EDT) To: Michael Richards cc: pgsql-sql@postgreSQL.org, hackers@postgreSQL.org Subject: Re: [SQL] Re: [HACKERS] Counting bool flags in a complex query In-reply-to: Your message of Fri, 16 Jul 1999 18:19:58 -0300 (ADT) Date: Fri, 16 Jul 1999 17:56:44 -0400 Message-ID: <11614.932162204@sss.pgh.pa.us> From: Tom Lane Michael Richards writes: >> For example, >> >> create table z1 (f1 int4, f2 int4); >> CREATE >> select f1 as f2, f2 from z1 order by f2; >> f2|f2 >> --+-- >> (0 rows) >> >> Which column do you think it's ordering by? Which column *should* it >> order by? I think this ought to draw an "ambiguous column label" error > Good point. Is there anything in the SQL standard that defined how this > "is supposed" to work? After looking at the SQL spec I think the above definitely ought to draw an error. We have the following verbiage concerning the column names for the result of a SELECT: a) If the i-th in the does not specify an and the of that is a single , then the of the i-th column of the result is C. c) Otherwise, the of the i-th column of the is implementation-dependent and different from the of any column, other than itself, of a table referenced by any contained in the SQL-statement. which Postgres does indeed follow, and we see from (a) and (b) that "f2" is the required column name for both columns of the SELECT result. Now ORDER BY says a) If a contains a , then T shall contain exactly one column with that and ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the identifies that column. which sure looks to me like it mandates an error for the example statement. However, since SQL doesn't consider the possibility of expressions as ORDER BY entries, we are more or less on our own for those. An expression appearing in the target list of a SELECT is not allowed to refer to columns by their "AS" names (and this does seem to be mandated by SQL92). So I think it makes sense to carry over the same restriction to ORDER BY. regards, tom lane