Received: from localhost (maia-4.hub.org [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id E8D129FA173 for ; Wed, 20 Dec 2006 11:47:03 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-new, port 10024) with ESMTP id 35445-08 for ; Wed, 20 Dec 2006 11:46:55 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from myemma.com (db1.e2ma.net [66.179.147.163]) by postgresql.org (Postfix) with ESMTP id 1EDA69F9C35 for ; Wed, 20 Dec 2006 11:46:55 -0400 (AST) Received: from localhost (localhost.localdomain [127.0.0.1]) by myemma.com (Postfix) with ESMTP id 54F0823CEB; Wed, 20 Dec 2006 09:46:53 -0600 (CST) Received: from myemma.com ([127.0.0.1]) by localhost (velma [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 20064-04; Wed, 20 Dec 2006 09:46:51 -0600 (CST) Received: from [10.0.23.60] (unknown [209.149.59.218]) by myemma.com (Postfix) with ESMTP id B7A1823CEF; Wed, 20 Dec 2006 09:46:51 -0600 (CST) Message-ID: <45895AEB.20501@myemma.com> Date: Wed, 20 Dec 2006 09:46:51 -0600 From: Erik Jones User-Agent: Thunderbird 1.5.0.7 (Windows/20060909) MIME-Version: 1.0 To: Gary Stainburn Cc: pgsql-sql@postgresql.org Subject: Re: join/group/count query. References: <200612201012.25960.gary.stainburn@ringways.co.uk> <1166612996.6369.282.camel@localhost.localdomain> In-Reply-To: <1166612996.6369.282.camel@localhost.localdomain> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at myemma.com X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200612/158 X-Sequence-Number: 27114 Ragnar wrote: > On miš, 2006-12-20 at 10:12 +0000, Gary Stainburn wrote: > >> Hi folks. I have the following query which works for me at the moment. >> However, o_model refers to a table stock_models which has one record for each >> model type. At the moment if I another record to the stock_models I have to >> amend the select. Is it possible to make this automatic by joining the >> stock_models data somehow? >> >> select o_p_id as key, o_p_id as p_id, p_name, count(o_p_id) as total, >> count (case when o_model = 5 then 1 else NULL end) as KA, >> count (case when o_model = 10 then 1 else NULL end) as Focus, >> count (case when o_model = 13 then 1 else NULL end) as C_Max, >> count (case when o_model = 16 then 1 else NULL end) as S_Max, >> count (case when o_model = 20 then 1 else NULL end) as Fiesta, >> count (case when o_model = 25 then 1 else NULL end) as Fusion, >> count (case when o_model = 30 then 1 else NULL end) as Mondeo, >> count (case when o_model = 35 then 1 else NULL end) as Galaxy, >> count (case when o_model = 40 then 1 else NULL end) as Ranger, >> count (case when o_model = 50 then 1 else NULL end) as Connect, >> count (case when o_model = 60 then 1 else NULL end) as Transit, >> count (case when o_model = 70 then 1 else NULL end) as Fiesta_Van >> from order_details >> where date_trunc('month',CURRENT_DATE) = date_trunc('month',o_ord_date) >> group by o_p_id, p_name; >> > > if I understand correctly, you want one column in your output, > for each row in the table table stock_models > > you can do this with the crosstabN function in the contrib > module 'tablefunc', or by making your own procedural language > function. > > gnari > And, I may be missing something, but I'm having a hard time understanding why you have all of those select columns of the form: count (case when o_model = 5 then 1 else NULL end) as KA, Considering that that can only return 1 or 0, the case statement would do. Is it to avoid putting all of the column names in the group by clause? That's hackish and is as much or more typing. With regards to what you are actually trying to do, giving us your table definitions and what you are trying to achieve would help a lot more than just telling us the problem you are having. The column names in your query are in no way descriptive and tell us nothing about your actual table structure. -- erik jones software development emma(r)