From: | Filip Rembiałkowski <filip(dot)rembialkowski(at)gmail(dot)com> |
---|---|
To: | Lee Hachadoorian <lee(dot)hachadoorian(at)gmail(dot)com> |
Cc: | pgsql-sql(at)postgresql(dot)org |
Subject: | Re: Aggregating by unique values |
Date: | 2010-12-14 08:23:03 |
Message-ID: | AANLkTinwDzmxMG92_YeG+9+3=J_z1+vwU2DkfspQCia4@mail.gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-sql |
try
select zip, count(distinct id) from customer_service_date group by zip;
2010/12/14 Lee Hachadoorian <lee(dot)hachadoorian(at)gmail(dot)com>
> Hello,
>
> I'm trying to count customers who have received services by ZIP code,
> but I want to count each customer only once even though customers may
> have received services on multiple dates, and therefore appear in the
> table multiple times. There *is* a separate customers table, but because
> of dirty data, I cannot rely on it.
>
> The best I can come up with is:
>
> SELECT
> zip, count(*) AS count_serviced
> FROM
> (SELECT DISTINCT zip, id FROM customer_service_date) a
> GROUP BY
> zip
> ;
>
> The table (with some irrelevant fields dropped) is:
>
> CREATE TABLE customer_service_date
> (
> id integer,
> address character varying,
> city character varying,
> state character varying,
> zip character varying,
> service_date date
> )
> ;
>
> The table is missing a primary key field, but it would be (id,
> service_date) if it had one.
>
> Any suggestions to improve this?
>
> Thanks,
> --Lee
>
> --
> Lee Hachadoorian
> PhD Student, Geography
> Program in Earth & Environmental Sciences
> CUNY Graduate Center
>
>
> --
> Sent via pgsql-sql mailing list (pgsql-sql(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-sql
>
From | Date | Subject | |
---|---|---|---|
Next Message | Trinath Somanchi | 2010-12-14 08:53:03 | Re: Using count on a join, group by required? |
Previous Message | emaratiyya | 2010-12-14 07:19:16 | Re: Using count on a join, group by required? |