Re: A join of 2 tables with sum(column) > 30

From: Vibhor Kumar <vibhor(dot)kumar(at)enterprisedb(dot)com>
To: Alexander Farber <alexander(dot)farber(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: A join of 2 tables with sum(column) > 30
Date: 2011-03-15 21:47:46
Message-ID: 28422D2E-7EC8-4F9E-BFF0-5974659858A7@enterprisedb.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


On Mar 16, 2011, at 3:12 AM, Alexander Farber wrote:

> Unfortunately I get:
>
> # select u.id, u.first_name, sum(m.completed) from pref_users u,
> pref_match m where u.id=m.id and u.id like 'DE%' and sum(m.completed)
>> 30 group by u.id, u.first_name order by sum desc limit 3;
> ERROR: aggregates not allowed in WHERE clause
> LINE 1: ...f_match m where u.id=m.id and u.id like 'DE%' and sum(m.comp...
>

My Bad... Missed you have to use having clause as given below:
select u.id, u.first_name, sum(m.completed) from pref_users u, pref_match m where u.id=m.id and u.id like 'DE%' group by u.id, u.first_name having sum(m.completed > 30 order by sum desc limit 3;

>
> On Tue, Mar 15, 2011 at 10:43 PM, Vibhor Kumar
> <vibhor(dot)kumar(at)enterprisedb(dot)com> wrote:
>>
>> On Mar 16, 2011, at 3:03 AM, Alexander Farber wrote:
>>
>>> # select u.id, u.first_name, sum(m.completed)
>>> from pref_users u, pref_match m
>>> where u.id=m.id and u.id like 'DE%' and
>>> sum > 30 group by u.id, u.first_name
>>> order by sum desc limit 3;
>>>
>>> ERROR: column "sum" does not exist
>>> LINE 4: ...f_match m where u.id=m.id and u.id like 'DE%' and sum > 30 g...
>>>
>>> Any suggestions please? I've tried "... sum(m.completed) as total" too..
>>
>>
>> Try following:
>> select u.id, u.first_name, sum(m.completed) from pref_users u, pref_match m where u.id=m.id and u.id like 'DE%' and sum(m.completed > 30 group by u.id, u.first_name order by sum desc limit 3;
>>
>
> --
> Sent via pgsql-general mailing list (pgsql-general(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-general

Thanks & Regards,
Vibhor Kumar
EnterpriseDB Corporation
The Enterprise PostgreSQL Company
vibhor(dot)kumar(at)enterprisedb(dot)com
Blog:http://vibhork.blogspot.com

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Vibhor Kumar 2011-03-15 21:49:02 Re: A join of 2 tables with sum(column) > 30
Previous Message Alexander Farber 2011-03-15 21:45:01 Re: A join of 2 tables with sum(column) > 30