pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Joseph Shraibman <jks@selectacast.net>
Cc: Bruce Momjian <pgman@candle.pha.pa.us>
Cc: pgsql-sql@postgresql.org <pgsql-sql@postgresql.org>
Subject: Re: counting distinct values
Date: Wed, 07 Jun 2000 22:46:35 -0400
Message-ID: <14104.960432395@sss.pgh.pa.us> (raw)
In-Reply-To: <393EF10A.7CAEAB64@selectacast.net>
References: <200006080015.UAA04389@candle.pha.pa.us>
	<393EF10A.7CAEAB64@selectacast.net>

Joseph Shraibman <jks@selectacast.net> writes:
>>>> Using the example from
>>>> http://www.postgresql.org/docs/aw_pgsql_book/node59.html, what would I
>>>> do if I wanted to know the number of different cities where I had a
>>>> friend in each state?  select count(city) group by state; would not work
>>>> because if you had two friends in the same city it would be counted
>>>> twice.

Er, what's wrong with select count(distinct city) group by state?

> ... now suppose I want to have the number of distictive b's in the
> results as well. I try:

> playpen=> select a, count(m.oid), avg(m.n), sum(m.n), (select
> count(mm.oid) from tablem mm where m.a = mm.a group by mm.b ) from
> tablem m group by a;
> ERROR:  More than one tuple returned by a subselect used as an
> expression.
> playpen=> 

> ... even though the subselect should only return one tuple.

Not unless there's only one b value for any one a value --- otherwise
the sub-select will return one row per b group.  The error message looks
correct to me.

			regards, tom lane



view thread (5+ messages)  latest in thread

Message-ID: <14104.960432395@sss.pgh.pa.us>
Permalink:  ../14104.960432395@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/14104.960432395@sss.pgh.pa.us

 · 

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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: tgl@sss.pgh.pa.us, jks@selectacast.net, pgman@candle.pha.pa.us
  Subject: Re: counting distinct values
  In-Reply-To: <14104.960432395@sss.pgh.pa.us>
  References: <200006080015.UAA04389@candle.pha.pa.us> <393EF10A.7CAEAB64@selectacast.net> <14104.960432395@sss.pgh.pa.us>

* 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