Received: from localhost (wm.hub.org [200.46.204.128]) by postgresql.org (Postfix) with ESMTP id 034969FA12B for ; Mon, 13 Nov 2006 13:05:22 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.128]) (amavisd-new, port 10024) with ESMTP id 21420-03 for ; Mon, 13 Nov 2006 17:05:17 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey- Received: from fep06.ttnet.net.tr (mail.ttnet.net.tr [212.175.13.129]) by postgresql.org (Postfix) with ESMTP id 321EB9FA0B9 for ; Mon, 13 Nov 2006 13:05:15 -0400 (AST) Received: from (unknown) by AVGW-I-7.ttnet.net.tr with smtp id 4bab_21d69fc0_7339_11db_9aea_001143376e77; Mon, 13 Nov 2006 19:05:15 +0200 Received: from alamut ([85.97.155.105]) by fep06.ttnet.net.tr with ESMTP id <20061113170514.ZLTD28414.fep06.ttnet.net.tr@alamut>; Mon, 13 Nov 2006 19:05:14 +0200 Received: from (dsl.dynamic8597155105.ttnet.net.tr [85.97.155.105]) by AVGW-I-5.ttnet.net.tr with smtp id 2429_20e8e514_7339_11db_9e0b_001422143cb8; Mon, 13 Nov 2006 19:05:13 +0200 Date: Mon, 13 Nov 2006 19:03:58 +0200 From: Volkan YAZICI To: Erik Jones Cc: pgsql-sql@postgresql.org Subject: Re: COALESCE and GROUP BY and AGGREGATES Message-ID: <20061113170358.GA1403@alamut> References: <4558A232.6050801@myemma.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <4558A232.6050801@myemma.com> User-Agent: Mutt/1.4.2.1i X-NAI-Spam-Rules: 1 Rules triggered BAYES_00=-2.5 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=2.615 tagged_above=0 required=5 tests=BAYES_20, DNS_FROM_RFC_ABUSE, DNS_FROM_RFC_POST, DNS_FROM_RFC_WHOIS, SPF_PASS, UNPARSEABLE_RELAY, UPPERCASE_25_50 X-Spam-Level: ** X-Archive-Number: 200611/132 X-Sequence-Number: 26784 On Nov 13 10:49, Erik Jones wrote: > 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. What about such a schema design: CREATE TABLE sale_types ( id serial PRIMARY KEY, name text NOT NULL DEFAULT 'setup' ); CREATE UNIQUE INDEX sales_types_typ_idx ON sales_types (typ); CREATE TABLE sales_table ( typ bigint REFERENCES sale_types (id), amount numeric NOT NULL, sdate timestamp without time zone DEFAULT CURRENT_TIMESTAMP ); SELECT TYP.name, COALESCE(SUM(TBL.amount), 0) FROM sale_types AS TYP LEFT OUTER JOIN sales_table AS TBL ON (TYP.id = TBL.typ) WHERE TBL.sale_date LIKE '2006-11%' GROUP BY TYP.name; I didn't try the above SQL queries, but I hope you understand what I meant. Regards.