Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 8479B6329BD for ; Wed, 11 Aug 2010 09:39:39 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 51172-04 for ; Wed, 11 Aug 2010 12:39:31 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail.gmx.net (mailout-de.gmx.net [213.165.64.23]) by mail.postgresql.org (Postfix) with SMTP id 5921E633285 for ; Wed, 11 Aug 2010 09:39:31 -0300 (ADT) Received: (qmail invoked by alias); 11 Aug 2010 12:39:28 -0000 Received: from fsgw.f-secure.com (EHLO [10.128.133.70]) [193.110.108.33] by mail.gmx.net (mp013) with SMTP; 11 Aug 2010 14:39:28 +0200 X-Authenticated: #495269 X-Provags-ID: V01U2FsdGVkX1/Tx3NO3wnTQ1YgsTfCS5wghZw7cWSMr5ABJj7oXh IBaQ71P4troph8 Subject: Re: Using count on a join, group by required? From: Peter Eisentraut To: Jose Ildefonso Camargo Tolosa Cc: pgsql-sql@postgresql.org In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Date: Wed, 11 Aug 2010 15:39:28 +0300 Message-ID: <1281530368.8340.4.camel@fsopti579.F-Secure.com> Mime-Version: 1.0 X-Mailer: Evolution 2.30.2 Content-Transfer-Encoding: 7bit X-Y-GMX-Trusted: 0 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.79 tagged_above=-5 required=5 tests=BAYES_50=0.8, RCVD_IN_DNSWL_NONE=-0.0001, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201008/53 X-Sequence-Number: 34493 On tis, 2010-08-10 at 22:21 -0430, Jose Ildefonso Camargo Tolosa wrote: > And it works, it gives me something like: > > product_id | name | code | manufacturer_id | > manufacturer_name | num_serials > ------------+----------------------+---------------+-----------------+----------------------------+------------- > 17 | THE product | 1235711131719 | 19 | > THE product's manufacturer | 5 > 6 | Car Battery 500A 12V | 7591512021575 | 8 | > Acumuladores Duncan, C.A. | 11 > 1 | Test product 1 | 123456789012 | 1 | > Test Manufacturer | 6 > > Which is correct, and exactly what I wanted. > > So far, so good. The thing is: the group by clause, I had to add it > because the parser forced me to, because it complained like this: > > ERROR: column "manufacturer.name" must appear in the GROUP BY clause > or be used in an aggregate function > > and I had to include *all* the requested columns on the group by > clause, can anybody tell me why? or at least point to some doc that > help me understanding this? This is fixed in PostgreSQL 9.1devel (*); there you only need to put the primary key into the GROUP BY clause. Earlier versions didn't know that that was enough to ensure a deterministic result. (*) -- It will probably be a bit over a year before that is released.