One-2-many relation - need distinct counts

From: PS PS <psus2020(at)yahoo(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Subject: One-2-many relation - need distinct counts
Date: 2003-08-26 13:54:58
Message-ID: 20030826135458.83369.qmail@web20302.mail.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Richard Huxton 2003-08-26 17:30:31 Eliminating duplicate lists
Previous Message Tom Lane 2003-08-26 13:09:40 Re: postgres 7.1.3: why does the query plan ignore indexes?