Re: Counting unique rows as an aggregate.

From: Jan Otto <asche(at)me(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Counting unique rows as an aggregate.
Date: 2008-09-30 12:26:04
Message-ID: 9BAE6062-2E75-4E87-86CB-5DF2F33DF2CD@me.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

>> I must be missing something, because I don't see why you couldn't do
>> SELECT count(distinct make), count(distinct color) from table WHERE
>> >criteria<;
>
> I didn't explain well, I want the count of each distinct value in a
> column, eg, if the color column has 50 rows, 20x'red', 10x'green',
> 20x'blue' - it will give me those results.
>
> SELECT count(distinct color) would return 3 - which is the count of
> distinct values, which is not what I want.

maybe this simple example helps you:

# create table colors (color text);
CREATE TABLE
# insert into colors select 'red';
INSERT 0 1
# insert into colors select 'red';
INSERT 0 1
# insert into colors select 'green';
INSERT 0 1
# insert into colors select 'yellow';
INSERT 0 1
# insert into colors select 'yellow';
INSERT 0 1
# insert into colors select 'yellow';
INSERT 0 1
# select count(1), color from colors group by color;
count | color
-------+--------
2 | red
3 | yellow
1 | green
(3 rows)

Jan

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Andrew Sullivan 2008-09-30 13:01:30 Re: Re: Is there a parameter to tell postgresql to not attempt to open an IPv6 socket?
Previous Message Tom Lane 2008-09-30 12:08:59 Re: Free Cache Memory (Linux) and Postgresql