column alias and group by/having/order

Lists: pgsql-sql
From: Tomasz Myrta <jasiek(at)klaster(dot)net>
To: Lista dyskusyjna pgsql-sql <pgsql-sql(at)postgresql(dot)org>
Subject: column alias and group by/having/order
Date: 2004-02-13 13:34:54
Message-ID: 402CD27E.7050001@klaster.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-sql

Hi
I found some strange column alias behaviour:

select val1+val2 as val
from some_table
group by val;

result - OK

select val1+val2 as val
from some_table
order by val;

result - OK

select val1+val2 as val
from some_table
group by val having val1+val2>1;

result - OK

select val1+val2 as val
from some_table
group by val having val>1;
ERROR: Attribute "val" not found

Is it a bug or a feature?

Regards,
Tomasz Myrta

----------------------------------------------------------------------------------------------------------------
PostgreSQL 7.3.4 on i386-pc-linux-gnu, compiled by GCC i386-linux-gcc
(GCC) 3.3.2 20031005 (Debian prerelease)


From: Rod Taylor <pg(at)rbt(dot)ca>
To: Tomasz Myrta <jasiek(at)klaster(dot)net>
Cc: Lista dyskusyjna pgsql-sql <pgsql-sql(at)postgresql(dot)org>
Subject: Re: column alias and group by/having/order
Date: 2004-02-13 14:09:05
Message-ID: 1076681344.10896.19.camel@jester
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-sql

> select val1+val2 as val
> from some_table
> group by val having val>1;
> ERROR: Attribute "val" not found
>
> Is it a bug or a feature?

It's a mis-feature that group by accepts aliases of the select list.

Having is proper.


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Tomasz Myrta <jasiek(at)klaster(dot)net>
Cc: Lista dyskusyjna pgsql-sql <pgsql-sql(at)postgresql(dot)org>
Subject: Re: column alias and group by/having/order
Date: 2004-02-13 15:15:31
Message-ID: 2373.1076685331@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-sql

Tomasz Myrta <jasiek(at)klaster(dot)net> writes:
> I found some strange column alias behaviour:

That's how it is. The fact that you can reference output-column aliases
at all in GROUP BY or ORDER BY is a wart, because it conflicts with the
logical model of query evaluation --- the output list should be computed
last. We have extended the SQL92 requirement (unadorned aliases in
ORDER BY) to allow the same in GROUP BY, but we don't take it to the
level of allowing them inside arbitrary expressions.

regards, tom lane