Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id A6809632BCF for ; Tue, 10 Aug 2010 23:51:22 -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 04283-03 for ; Wed, 11 Aug 2010 02:51:15 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-qy0-f181.google.com (mail-qy0-f181.google.com [209.85.216.181]) by mail.postgresql.org (Postfix) with ESMTP id 61319632ADC for ; Tue, 10 Aug 2010 23:51:15 -0300 (ADT) Received: by qyk36 with SMTP id 36so110727qyk.19 for ; Tue, 10 Aug 2010 19:51:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:received:date:message-id :subject:from:to:content-type; bh=L1ZkXyjl6i4eLE8JCZBIq+Mvh7dMP4KpMXhZGVlNrrM=; b=tQ98jvvZCkxmlbzgwt0DqpqHXec5AfkMuY3KLiudZjuOI25Lc71ytcsEhKwXjka2yS XUMY/M6vhqf6Afc9+fUsbRcb1nnyX1ytkgGKFH0Rb2oY6zEDOYeH8S8eJz1PFmLhamSC JWtbTEudc2EIBfWFrYLMSoG012VMu+4I6/t6k= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:date:message-id:subject:from:to:content-type; b=U/1F9fbyOvkWnMGiFRadirOUghV9WAImrcizf0YkXdcaaz7Sxks0LvksuAVi/whj6I K13/RetdSJ9m34YFNnHvhm9INzfbVwVFmL0Kw1ZDlUxBrnRiTL4suPGaoWqjVy87peqS RCGca2IR+7KsW+4N6zfVtwfwCZkW0Hj8RDg7I= MIME-Version: 1.0 Received: by 10.220.157.141 with SMTP id b13mr11004977vcx.27.1281495073725; Tue, 10 Aug 2010 19:51:13 -0700 (PDT) Received: by 10.220.109.86 with HTTP; Tue, 10 Aug 2010 19:51:13 -0700 (PDT) Date: Tue, 10 Aug 2010 22:21:13 -0430 Message-ID: Subject: Using count on a join, group by required? From: Jose Ildefonso Camargo Tolosa To: pgsql-sql@postgresql.org Content-Type: text/plain; charset=ISO-8859-1 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.8 tagged_above=-5 required=5 tests=BAYES_50=0.8, RCVD_IN_DNSWL_NONE=-0.0001 X-Spam-Level: X-Archive-Number: 201008/51 X-Sequence-Number: 34491 Greetings! First: This is working, I just need a clarification on concept, so, it is not necessary for you to look deeply at the SQL statement. I have this: Table: products that references manufacturer via products.manufacturer_id to manufacturer.id (not important, just informative). Table: product_serials that references products via product_serials.product_id to products.id And I wanted to get this output: Product ID, Product Name, Product Code, Manufacturer ID, Manufacturer Name, Number of Associated Serials. So, I build a query for that: select products.id as product_id,products.name as name,products.code as code,manufacturer.id as manufacturer_id,manufacturer.name as manufacturer_name,count(product_serials.product_id) as num_serials from products left join manufacturer on products.manufacturer_id=manufacturer.id left join product_serials on product_serials.product_id=products.id group by products.id,products.name,products.code,manufacturer.id,manufacturer.name; 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? Thanks in advance, Ildefonso Camargo