X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id EDFDDD9B6D for ; Thu, 29 Sep 2005 07:05:26 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 43703-04 for ; Thu, 29 Sep 2005 10:05:22 +0000 (GMT) Received: from mail.metronet.co.uk (mail.metronet.co.uk [213.162.97.75]) by svr1.postgresql.org (Postfix) with ESMTP id B76F0D9B69 for ; Thu, 29 Sep 2005 07:05:23 -0300 (ADT) Received: from mainbox.archonet.com (84-51-143-99.archon037.adsl.metronet.co.uk [84.51.143.99]) by smtp.metronet.co.uk (MetroNet Mail) with ESMTP id 0D82041583E; Thu, 29 Sep 2005 11:05:19 +0100 (BST) Received: from localhost (localhost.localdomain [127.0.0.1]) by mainbox.archonet.com (Postfix) with ESMTP id A698915F1D; Thu, 29 Sep 2005 11:03:55 +0100 (BST) Received: from mainbox.archonet.com ([127.0.0.1]) by localhost (mainbox [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 15043-05; Thu, 29 Sep 2005 11:03:51 +0100 (BST) Received: from [192.168.1.17] (client17.office.archonet.com [192.168.1.17]) by mainbox.archonet.com (Postfix) with ESMTP id B1DB015F1C; Thu, 29 Sep 2005 11:03:51 +0100 (BST) Message-ID: <433BBC07.5040208@archonet.com> Date: Thu, 29 Sep 2005 11:03:51 +0100 From: Richard Huxton User-Agent: Mozilla Thunderbird 1.0.2 (X11/20050317) X-Accept-Language: en-us, en MIME-Version: 1.0 To: Axel Rau Cc: pgsql-sql@postgresql.org Subject: Re: Selecting count of details along with details columns References: <8365994213849c93946e61ded7d12318@Chaos1.DE> <433BA637.7040106@archonet.com> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.161 required=5 tests=[AWL=-0.096, FORGED_RCVD_HELO=0.05, UPPERCASE_25_50=0.207] X-Spam-Level: X-Archive-Number: 200509/295 X-Sequence-Number: 22842 Axel Rau wrote: > > Am 29.09.2005 um 10:30 schrieb Richard Huxton: > >> Axel Rau wrote: >> >>> SELECT T2.T2_name, COUNT(T1.id) AS xx >>> FROM T2, T1 >>> WHERE T2.id = T1.fk_t2 >>> GROUP BY T2.T2_name >>> HAVING COUNT(T1.id) > 1 >>> ORDER BY xx DESC; >>> t2_name | xx >>> ---------+---- >>> T2-N2 | 3 >>> T2-N3 | 2 >>> (2 rows) >>> Adding column t1_name to the result set breaks COUNT(T1.id): >>> SELECT T2.T2_name, T1.T1_name, COUNT(T1.id) AS xx >>> FROM T2, T1 >>> WHERE T2.id = T1.fk_t2 >>> GROUP BY T2.T2_name, T1.T1_name >>> HAVING COUNT(T1.id) > 1 >>> ORDER BY xx DESC; >>> t2_name | t1_name | xx >>> ---------+---------+---- >>> (0 rows) >>> How can I do this with pg ? >> >> >> Do what? You don't say what results you are expecting. >> >> Do you want: >> 1. ALL values of T1_name (in which case what count do you want)? >> 2. The FIRST value of T1_name (in which case what do you mean by first)? > > > #1.: > > t2_name | t1_name | count > ---------+---------+------- > T2-N2 | T1-CCC | 3 > T2-N3 | T1-FFF | 2 > T2-N2 | T1-BBB | 3 > T2-N2 | T1-DDD | 3 > T2-N3 | T1-EEE | 2 Ah - this is two questions: 1. What are the unique (t2_name,t1_name) pairings? 2. How many different (t1.id) values are there for each t2. So - something like: SELECT names.T2_name, names.T1_name, counts.num_t2 FROM ( SELECT DISTINCT T2.T2_name, T1.T1_name FROM T2,T1 WHERE T2.id = T1.fk_t2 ) AS names, ( SELECT T2.T2_name, COUNT(T1.id) AS num_t2 FROM T2, T1 WHERE T2.id = T1.fk_t2 GROUP BY T2.T2_name HAVING COUNT(T1.id) > 1 ) AS counts WHERE names.T2_name = counts.T2_name ; You could write the "names" sub-query with a GROUP BY if you wanted of course. -- Richard Huxton Archonet Ltd