X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.130]) by svr1.postgresql.org (Postfix) with ESMTP id 9B4E7D1B991 for ; Wed, 27 Aug 2003 19:52:15 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (neptune.hub.org [64.117.224.130]) (amavisd-new, port 10024) with ESMTP id 11644-07 for ; Wed, 27 Aug 2003 16:51:46 -0300 (ADT) Received: from anchor-post-35.mail.demon.net (anchor-post-35.mail.demon.net [194.217.242.85]) by svr1.postgresql.org (Postfix) with ESMTP id 8054ED1B8D2 for ; Wed, 27 Aug 2003 16:51:28 -0300 (ADT) Received: from mwynhau.demon.co.uk ([193.237.186.96] helo=mainbox.archonet.com) by anchor-post-35.mail.demon.net with esmtp (Exim 3.36 #2) id 19s6KH-0005rm-0Z; Wed, 27 Aug 2003 20:51:29 +0100 Received: from localhost (localhost.localdomain [127.0.0.1]) by mainbox.archonet.com (Postfix) with ESMTP id 080A916352; Wed, 27 Aug 2003 20:51:28 +0100 (BST) Received: from client17.archonet.com (client17.archonet.com [192.168.1.17]) by mainbox.archonet.com (Postfix) with ESMTP id 5BE811592A; Wed, 27 Aug 2003 20:51:27 +0100 (BST) From: Richard Huxton To: PS PS , pgsql-sql@postgresql.org Subject: Re: One-2-many relation - need distinct counts Date: Wed, 27 Aug 2003 20:51:26 +0100 User-Agent: KMail/1.5 References: <20030826135458.83369.qmail@web20302.mail.yahoo.com> In-Reply-To: <20030826135458.83369.qmail@web20302.mail.yahoo.com> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200308272051.26634.dev@archonet.com> X-Virus-Scanned: by AMaViS snapshot-20020531 X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200308/319 X-Sequence-Number: 14935 On Tuesday 26 August 2003 14:54, PS PS wrote: > Select Count(Distinct(account_no)) > from A, B > where A.Account_no = B.Account_no > > I get the correct count. If I do this: > Select Count(Distinct(account_no)), B.Account_type > from A, B > where A.Account_no = B.Account_no > group by B.Account_type > > I get wrong counts because there some are duplicated. > I tried everything that I can think of - subquery, sub > table etc. I would appreciate some help in writing > the query. Thanks in advance. I'm not sure the query is well formed. If you have the following in B: Acct_type | Acct_no alpha | 0001 beta | 0002 alpha | 0003 beta | 0003 I think you're saying you get: alpha 2 beta 2 Are you saying you want alpha 2 beta 1 or: alpha 1 beta 2 If you're not sure which you want, that's the route of your problem. If you want the first try something like SELECT account_no, min(account_type) FROM B GROUP BY account_no -- Richard Huxton Archonet Ltd