Re: [GENERAL] Any ideas why this doesn't work or how to rewrite it?

From: Herouth Maoz <herouth(at)oumail(dot)openu(dot)ac(dot)il>
To: "Brett W(dot) McCoy" <bmccoy(at)lan2wan(dot)com>
Cc: Aaron Holtz <aholtz(at)bright(dot)net>, pgsql-general(at)postgreSQL(dot)org
Subject: Re: [GENERAL] Any ideas why this doesn't work or how to rewrite it?
Date: 1999-04-29 15:09:09
Message-ID: l03130309b34e25d54cfd@[147.233.159.109]
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

At 21:04 +0300 on 29/04/1999, Brett W. McCoy wrote:

> I think, Aaron, you could get a count of distinct customer names like this:
>
> SELECT DISTINCT customer_username, COUNT(*) FROM customerdata
> GROUP BY customer_username;
>
> This will give you 2 columns, one with the distinct customer_usernames
> and the second with the count of each. The GROUP BY caluse is important
> here. This looks like what you wanted in your original query.

No, Brett. COUNT( DISTINCT ...) is supposed to count the number of distinct
names in a table. Here, I created a test table:

testing=> select * from test;
customer
--------
moshe
david
hanna
david
sarah
moshe
suzanne
moshe
moshe
(9 rows)

The distinct names are:

testing=> select distinct customer
testing-> from test;
customer
--------
david
hanna
moshe
sarah
suzanne
(5 rows)

So clearly, the datum he wanted was "5" - there are five distinct customers
here.

Your query, however, gives the following:

testing=> select distinct customer, count(*)
testing-> from test
testing-> group by customer;
customer|count
--------+-----
david | 2
hanna | 1
moshe | 4
sarah | 1
suzanne | 1
(5 rows)

Which shows him the number of REPETITIONS on each distinct name.

My ugly query gives:

testing=> select count(*)
testing-> from test t1
testing-> where int( oid ) = (
testing-> SELECT min( int( t2.oid ) )
testing-> FROM test t2
testing-> WHERE t2.customer = t1.customer
testing-> );
count
-----
5
(1 row)

And this is the exact number of distinct names in the table.

Herouth

--
Herouth Maoz, Internet developer.
Open University of Israel - Telem project
http://telem.openu.ac.il/~herutma

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Margarita Barvinok 1999-04-29 15:32:22 RE: [GENERAL] LIMIT QUESTION
Previous Message Jun Zhang 1999-04-29 14:54:18 Re: [GENERAL] UnixWare chokes --- Resolution