Re: Simple aggregate query brain fart

From: Mark Fenbers <Mark(dot)Fenbers(at)noaa(dot)gov>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Simple aggregate query brain fart
Date: 2010-03-18 15:15:55
Message-ID: 4BA243AB.10804@noaa.gov
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Thanks, Joe and Tom. You cleared the webs out of my brain. I used HAVING before, but not lately and I got rusty.

Mark

Tom Lane wrote:

Mark Fenbers Mark(dot)Fenbers(at)noaa(dot)gov writes:


I want to do:
SELECT id, count(*) FROM mytable WHERE count(*) 2 GROUP BY id;




But this doesn't work because Pg won't allow aggregate functions in a
where clause.


Use HAVING, not WHERE. The way you are trying to write the query is
meaningless because WHERE filters rows before grouping/aggregation.
HAVING filters afterwards, which is when it makes sense to put a
condition on count(*).

regards, tom lane

Attachment Content-Type Size
unknown_filename text/html 1.2 KB
Mark_Fenbers.vcf text/x-vcard 402 bytes

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Justin Graf 2010-03-18 17:02:04 Re: MSSQL to PostgreSQL - Issue trying to generate unique ID using actual date
Previous Message Tom Lane 2010-03-18 15:11:13 Re: Simple aggregate query brain fart