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 BA98AD1B56C for ; Tue, 26 Aug 2003 10:55:15 -0300 (ADT) 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 14227-08 for ; Tue, 26 Aug 2003 10:55:01 -0300 (ADT) Received: from web20302.mail.yahoo.com (web20302.mail.yahoo.com [216.136.226.83]) by svr1.postgresql.org (Postfix) with SMTP id A9930D1B8DE for ; Tue, 26 Aug 2003 10:54:56 -0300 (ADT) Message-ID: <20030826135458.83369.qmail@web20302.mail.yahoo.com> Received: from [138.69.160.2] by web20302.mail.yahoo.com via HTTP; Tue, 26 Aug 2003 06:54:58 PDT Date: Tue, 26 Aug 2003 06:54:58 -0700 (PDT) From: PS PS Subject: One-2-many relation - need distinct counts To: pgsql-sql@postgresql.org MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200308/304 X-Sequence-Number: 14920 Hi gurus, I have table A, B and need a distinct count of Accounts from A. There is a one-2-many relation between A and B. Accounts # in A are all unique. However, in B there will be duplicates. So the problem I have is, when I join as follows: 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. PS __________________________________ Do you Yahoo!? Yahoo! SiteBuilder - Free, easy-to-use web site design software http://sitebuilder.yahoo.com