Skip site navigation (1) Skip section navigation (2)

Re: join/group/count query.

From: "Hector Villarreal" <HVillarreal(at)mynewplace(dot)com>
To: "Gary Stainburn" <gary(dot)stainburn(at)ringways(dot)co(dot)uk>,<pgsql-sql(at)postgresql(dot)org>
Subject: Re: join/group/count query.
Date: 2006-12-20 18:06:29
Message-ID: 8C5B026B51B6854CBE88121DBF097A866A4A9D@ehost010-33.exch010.intermedia.net (view raw or flat)
Thread:
Lists: pgsql-sql
HI in reading this is it possible what was really intended : 
Sum(case when o_model = 5 then 1 else NULL end) as KA
That would provide a count of all records meeting that condition. 
Otherwise the count( approach will not do that. 


-----Original Message-----
From: pgsql-sql-owner(at)postgresql(dot)org
[mailto:pgsql-sql-owner(at)postgresql(dot)org] On Behalf Of Gary Stainburn
Sent: Wednesday, December 20, 2006 8:20 AM
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: [SQL] join/group/count query.

>
> 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

-- 
Gary Stainburn
 
This email does not contain private or confidential material as it
may be snooped on by interested government parties for unknown
and undisclosed purposes - Regulation of Investigatory Powers Act, 2000


---------------------------(end of broadcast)---------------------------
TIP 4: Have you searched our list archives?

               http://archives.postgresql.org

In response to

Responses

pgsql-sql by date

Next:From: Sandip GDate: 2006-12-21 07:41:33
Subject: NEED URGENT HELP....
Previous:From: Erik JonesDate: 2006-12-20 16:32:00
Subject: Re: join/group/count query.

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group