Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 1A6629FB39B for ; Mon, 13 Nov 2006 14:36:54 -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 11844-01 for ; Mon, 13 Nov 2006 14:36:45 -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 3B6869FBDAE for ; Mon, 13 Nov 2006 14:16:55 -0400 (AST) Received: from localhost (localhost.localdomain [127.0.0.1]) by myemma.com (Postfix) with ESMTP id 951FC23CF0; Mon, 13 Nov 2006 12:16:54 -0600 (CST) Received: from myemma.com ([127.0.0.1]) by localhost (velma [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 02981-05; Mon, 13 Nov 2006 12:16:53 -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 8C9D823CE3; Mon, 13 Nov 2006 12:16:53 -0600 (CST) Message-ID: <4558B694.2020903@myemma.com> Date: Mon, 13 Nov 2006 12:16:52 -0600 From: Erik Jones User-Agent: Thunderbird 1.5.0.7 (Windows/20060909) MIME-Version: 1.0 To: Volkan YAZICI Cc: pgsql-sql@postgresql.org Subject: Re: COALESCE and GROUP BY and AGGREGATES References: <4558A232.6050801@myemma.com> <20061113170358.GA1403@alamut> In-Reply-To: <20061113170358.GA1403@alamut> 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/140 X-Sequence-Number: 26792 Volkan YAZICI wrote: > 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. > Awesome. I didn't (and couldn't) change the schema, but doing a self-outer join on the table did the trick. Thanks! -- erik jones software development emma(r)