Skip site navigation (1) Skip section navigation (2)

Peripheral Links

Header And Logo

PostgreSQL
| The world's most advanced open source database.

Site Navigation

Search for
  Advanced Search

Re: How to use result column names in having cause


  • From: Stephan Szabo <sszabo(at)megazone(dot)bigpanda(dot)com>
  • To: Andrus <eetasoft(at)online(dot)ee>
  • Cc: pgsql-general(at)postgresql(dot)org
  • Subject: Re: How to use result column names in having cause
  • Date: Fri, 31 Mar 2006 07:04:26 -0800 (PST)
  • Message-id: <20060331065235(dot)M61282(at)megazone(dot)bigpanda(dot)com>

On Fri, 31 Mar 2006, Andrus wrote:

> >> In real application I have long expression instead of 123 and do'nt want
> >>  repeat this expression in HAVING clause.
> >
> > You have to repeat the expression. "AS" changes the output name, it
> > can't be used either in the where clause or any other limiting factor
> > like 'having':
>
> Doc about HAVING condition says:
>
> Each column referenced in condition must unambiguously reference a grouping
> colum
>
> HAVING x> AVG(bar) unambiguously references to a grouping column x

IIRC technically the query is invalid, because group by isn't supposed to
run on the output of select entries (as I think is stated by "Each
<grouping column reference> shall unambiguously reference a column of the
table resulting from the <from clause>.") and I'd guess this is a side
effect of allowing group by to work on the table resulting from the select
list as well.

I think the SQL way of writing this is to use a subselect and do two
levels (ie, generate a subselect that gives the table you want to group
and use it in the from clause of the outer query that does the grouping).



Home | Main Index | Thread Index

Privacy Policy | PostgreSQL Archives hosted by Command Prompt, Inc. | Designed by tinysofa
Copyright © 1996 – 2008 PostgreSQL Global Development Group