pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Mark Fenbers <Mark.Fenbers@noaa.gov>
Cc: pgsql-sql@postgresql.org
Subject: Re: Simple aggregate query brain fart
Date: Thu, 18 Mar 2010 11:11:13 -0400
Message-ID: <11074.1268925073@sss.pgh.pa.us> (raw)
In-Reply-To: <4BA24184.7060603@noaa.gov>
References: <62e0109f1003162359l65b6961ercd14b687433a9ff3@mail.gmail.com>
	<20100317071335.GC25571@a-kretschmer.de>
	<62e0109f1003170130q7a86d8afidf6159dcbd35ade@mail.gmail.com>
	<62e0109f1003170525m6e187b32n58850bd48259daa8@mail.gmail.com>
	<27653.1268835046@sss.pgh.pa.us>
	<4BA24184.7060603@noaa.gov>

Mark Fenbers <Mark.Fenbers@noaa.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



view thread (4+ messages)  latest in thread

Message-ID: <11074.1268925073@sss.pgh.pa.us>
Permalink:  ../11074.1268925073@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/11074.1268925073@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: tgl@sss.pgh.pa.us, Mark.Fenbers@noaa.gov
  Subject: Re: Simple aggregate query brain fart
  In-Reply-To: <11074.1268925073@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox