pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Richard Huxton <dev@archonet.com>
To: PS PS <psus2020@yahoo.com>
To: pgsql-sql@postgresql.org
Subject: Re: One-2-many relation - need distinct counts
Date: Wed, 27 Aug 2003 20:51:26 +0100
Message-ID: <200308272051.26634.dev@archonet.com> (raw)
In-Reply-To: <20030826135458.83369.qmail@web20302.mail.yahoo.com>
References: <20030826135458.83369.qmail@web20302.mail.yahoo.com>

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



view thread (2+ messages)

Message-ID: <200308272051.26634.dev@archonet.com>
Permalink:  ../200308272051.26634.dev@archonet.com/
Also on:    postgresql.org/message-id/200308272051.26634.dev@archonet.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: dev@archonet.com, psus2020@yahoo.com
  Subject: Re: One-2-many relation - need distinct counts
  In-Reply-To: <200308272051.26634.dev@archonet.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox