Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 844101337B88 for ; Tue, 14 Dec 2010 01:11:55 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 20922-06 for ; Tue, 14 Dec 2010 05:11:48 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-qy0-f174.google.com (mail-qy0-f174.google.com [209.85.216.174]) by mail.postgresql.org (Postfix) with ESMTP id D69F21337B93 for ; Tue, 14 Dec 2010 01:11:47 -0400 (AST) Received: by qyj19 with SMTP id 19so4368258qyj.19 for ; Mon, 13 Dec 2010 21:11:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from :user-agent:mime-version:to:subject:x-enigmail-version:content-type :content-transfer-encoding; bh=1iS9OpKKUhiw2whkPaV9iXZ6tbw7FhWqEKl9AR9CQYA=; b=hPz47zesInNCVEOh2cp9GpPBLS5a6Gg3iFdQkyz2Z9VfOg2KKmviXsoLiiglOxT1YN H5H/dU+KYhijLwZsGuxjvMDszh4mYqIN9tQqo5Yp4OkzzcZQyPbYQGk5Ji19UQN9k/tN InudjHqY1MkaqpNKfyzApmo1kjWAX9U9LMF10= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:user-agent:mime-version:to:subject :x-enigmail-version:content-type:content-transfer-encoding; b=FtA+B37avTW56EBaVq7/S03s76wXqlVZB7kY16dJs+QJZVqiWjETKKhXCnRpV0+HEO uf1gwSYyrVwaSVStQGVnt8VAwYFE8N8CvpERIOlsSRW/8MIoW+SO8+hoIdEZ7Ra+hg1v B1gXlEeyFz9H1qQ+9mOtJWcE4ZT5rcHODG42A= Received: by 10.229.96.70 with SMTP id g6mr4415943qcn.294.1292303507210; Mon, 13 Dec 2010 21:11:47 -0800 (PST) Received: from [146.96.186.170] ([146.96.186.170]) by mx.google.com with ESMTPS id u2sm4563748qcq.43.2010.12.13.21.11.33 (version=TLSv1/SSLv3 cipher=RC4-MD5); Mon, 13 Dec 2010 21:11:46 -0800 (PST) Message-ID: <4D06FC75.1050209@gmail.com> Date: Tue, 14 Dec 2010 00:11:17 -0500 From: Lee Hachadoorian User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.13) Gecko/20101208 Thunderbird/3.1.7 MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: Aggregating by unique values X-Enigmail-Version: 1.1.2 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.589 tagged_above=-5 required=5 tests=BAYES_00=-1.9, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7, T_TO_NO_BRKTS_FREEMAIL=0.01 X-Spam-Level: X-Archive-Number: 201012/64 X-Sequence-Number: 34900 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