Re: Using an ALIAS in WHERE clause

From: Tycho Fruru <tycho(at)fruru(dot)com>
To: "Ron St(dot)Pierre" <rstpierre(at)syscor(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Using an ALIAS in WHERE clause
Date: 2002-11-29 00:30:44
Message-ID: 1038529845.6531.19.camel@bozo.fruru.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Fri, 2002-11-29 at 01:17, Ron St.Pierre wrote:
> I'm using a query with similar functionality to the following:
>
> SELECT id,
> sum(hours) AS totalhours
> FROM mytable
> WHERE totalhours > 50;
>
> I get the following error:
> Attribute 'totalhours' not found.
>
> Am I not allowed to use an alias here? If not, how can I get my desired
> output?

select id, sum(hours) as totalhours
from mytable
group by id
having totalhours > 50

'where' is for tuple selection criteria
'having' is for group selection criteria
i suppose you want to have the total number of hours per id (therefore
we need to group by id.

Does this help ?
Tycho

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2002-11-29 00:38:00 Re: Using an ALIAS in WHERE clause
Previous Message Ron St.Pierre 2002-11-29 00:17:30 Using an ALIAS in WHERE clause