Received: from localhost (maia-3.hub.org [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id 244949FA304 for ; Wed, 20 Dec 2006 12:32:27 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-new, port 10024) with ESMTP id 75195-02 for ; Wed, 20 Dec 2006 12:32:04 -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 738199F9FB2 for ; Wed, 20 Dec 2006 12:32:04 -0400 (AST) Received: from localhost (localhost.localdomain [127.0.0.1]) by myemma.com (Postfix) with ESMTP id C8A5F23CE7; Wed, 20 Dec 2006 10:32:03 -0600 (CST) Received: from myemma.com ([127.0.0.1]) by localhost (velma [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 28934-04; Wed, 20 Dec 2006 10:31:59 -0600 (CST) Received: from [10.0.23.60] (unknown [209.149.59.218]) by myemma.com (Postfix) with ESMTP id AF1FD23CEB; Wed, 20 Dec 2006 10:31:59 -0600 (CST) Message-ID: <45896580.5090500@myemma.com> Date: Wed, 20 Dec 2006 10:32:00 -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> <45895AEB.20501@myemma.com> <200612201620.07863.gary.stainburn@ringways.co.uk> In-Reply-To: <200612201620.07863.gary.stainburn@ringways.co.uk> 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: 200612/160 X-Sequence-Number: 27116 Gary Stainburn wrote: >> 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. >> > > The order_details view is a join of the orders table to the other tables. > > The order table contains the orders. > > Each order has a business partner which is in a separate table. p_id is the > key, p_name is the name of the partner. > > Each order is for a single vehicle, which must be of a specific mode. The > models are also stored in a separate table. o_model is the attribute in the > orders table that contains the key to the models table. > > The output I'm getting is below, which is what I want. For each partner I get > a total followed by a breakdown by model the orders for the current month. > > key|p_id| p_name | total | ka | focus | c_max | s_max | fiesta | > fusion | mondeo | galaxy | ranger | connect | transit | fiesta_van > ---+----+----------------+-------+----+-------+-------+-------+--------+--------+--------+--------+--------+---------+---------+------------ > 40| 40|rrrrrrrrrrrrrrr | 2 |0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 |0 > 30| 30|rrrrr Group Plc | 2 |0 | 2 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |0 > 78| 78|rrrrr rrrrrrrrr | 2 |0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 1 | 0 |0 > 46| 46|rrrrrrrrrrrr ) | 3 |0 | 1 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 1 | 0 |0 > 3| 3|eeeee | 8 |0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 2 | 2 | 4 |0 > 9| 9|eee | 3 |0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 2 |0 > 12| 12|qqqqqqqqqqqq |13 |0 | 0 | 0 | 0 | 0 | 0 | 2 | 0 | 0 | 5 | 6 |0 > 10| 10|rr Motor | 7 |0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 2 | 5 |0 > 34| 34|ffffff fffffff | 3 |0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 2 |0 > 102| 102|xxxxxxxx xxxxxxx| 1 |0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 |0 > Ok, then, yeah, I read those count statements wrong and the crosstab contrib is what you're looking for. -- erik jones software development emma(r)