Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id BBB0D9FB20F for ; Mon, 13 Nov 2006 12:50:04 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 62220-02 for ; Mon, 13 Nov 2006 12:50:01 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from myemma.com (db1.e2ma.net [66.179.147.163]) by postgresql.org (Postfix) with ESMTP id 3C7049FB255 for ; Mon, 13 Nov 2006 12:50:00 -0400 (AST) Received: from localhost (localhost.localdomain [127.0.0.1]) by myemma.com (Postfix) with ESMTP id C211023CDE for ; Mon, 13 Nov 2006 10:49:59 -0600 (CST) Received: from myemma.com ([127.0.0.1]) by localhost (velma [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 09250-05 for ; Mon, 13 Nov 2006 10:49:59 -0600 (CST) Received: from [192.168.1.11] (c-71-228-218-92.hsd1.tn.comcast.net [71.228.218.92]) by myemma.com (Postfix) with ESMTP id 5468023CD4 for ; Mon, 13 Nov 2006 10:49:59 -0600 (CST) Message-ID: <4558A232.6050801@myemma.com> Date: Mon, 13 Nov 2006 10:49:54 -0600 From: Erik Jones User-Agent: Thunderbird 1.5.0.7 (Windows/20060909) MIME-Version: 1.0 To: Postgres SQL language list Subject: COALESCE and GROUP BY and AGGREGATES Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at myemma.com X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200611/131 X-Sequence-Number: 26783 Ok, here's a sample table for the question I have: CREATE TABLE sales_table ( sale_type varchar default 'setup' not null, sale_amount numeric not null sale_date timestamp without timezone default now()); So, let's say there are 3 different sale_types: 'setup', 'layaway', 'the_hookup' and I want to get totals for each type in a given month: SELECT sale_type, SUM(sale_amount) FROM sales_table WHERE sale_date LIKE '2006-11%' GROUP BY sale_type; If there hasn't been a sale of a given type in that month there won't be a row in the result set for that type. I want a row for each type with a default of 0 if there haven't been any sales for that type yet that month. I've tried: SELECT sale_type, (COALESCE(SUM(sale_amount), 0) FROM sales_table WHERE sale_date LIKE '2006-11%' GROUP BY sale_type; but, no dice. Any ideas? I know I can break this out into separate queries for each type and the COALESCE will work, but in my real-world situation I have a lot more than three types and that'd be ugly. Thanks, -- erik jones software development emma(r)