Re: One-2-many relation - need distinct counts

From: Richard Huxton <dev(at)archonet(dot)com>
To: PS PS <psus2020(at)yahoo(dot)com>, pgsql-sql(at)postgresql(dot)org
Subject: Re: One-2-many relation - need distinct counts
Date: 2003-08-27 19:51:26
Message-ID: 200308272051.26634.dev@archonet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Joe Conway 2003-08-28 00:40:17 Re: length of array
Previous Message Chris Faulkner 2003-08-27 19:49:12 length of array