pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Michael Richards <miker@scifair.acadiau.ca>
Cc: pgsql-sql@postgreSQL.org
Cc: hackers@postgreSQL.org
Subject: Re: [SQL] Re: [HACKERS] Counting bool flags in a complex query
Date: Fri, 16 Jul 1999 17:56:44 -0400
Message-ID: <11614.932162204@sss.pgh.pa.us> (raw)
In-Reply-To: <Pine.BSF.4.10.9907161815460.38362-100000@scifair.acadiau.ca>

Michael Richards <miker@scifair.acadiau.ca> 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 <derived column> in the <select list> specifies
              an <as clause> that contains a <column name> C, then the
              <column name> of the i-th column of the result is C.

            b) If the i-th <derived column> in the <select list> does not
              specify an <as clause> and the <value expression> of that
              <derived column> is a single <column reference>, then the
              <column name> of the i-th column of the result is C.

            c) Otherwise, the <column name> of the i-th column of the <query
              specification> is implementation-dependent and different
              from the <column name> of any column, other than itself, of
              a table referenced by any <table reference> 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 <sort specification> contains a <column name>, then T
              shall contain exactly one column with that <column name> and
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
              the <sort specification> 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



view thread (12+ messages)  latest in thread

Message-ID: <11614.932162204@sss.pgh.pa.us>
Permalink:  ../11614.932162204@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/11614.932162204@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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: tgl@sss.pgh.pa.us, miker@scifair.acadiau.ca, hackers@postgreSQL.org
  Subject: Re: [SQL] Re: [HACKERS] Counting bool flags in a complex query
  In-Reply-To: <11614.932162204@sss.pgh.pa.us>
  References: <Pine.BSF.4.10.9907161815460.38362-100000@scifair.acadiau.ca> <11614.932162204@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