Re: Group by and aggregates - Mailing list pgsql-sql

From Sam Mason
Subject Re: Group by and aggregates
Date
Msg-id 20041104172507.GG25775@sam.samason.me.uk
Whole thread Raw
In response to Group by and aggregates  ("Michael L. Hostbaek" <mich@the-lab.org>)
List pgsql-sql
Michael L. Hostbaek wrote:
>Now, I need the first line to say "15.00" in the cmup field. That is,
>stock and incoming are obviously not being grouped, but since it's the
>same partno I'd like somehow to show the highest cmup. Is there some
>black SQL voodoo that'll achieve this ?

I think you need to join the table back on itself to get the total
for each part.  Not a very impressive incantation, but I'd do it 
in SQL like this:
 SELECT x.partno, x.status, y.tot, sum(x.qty) FROM my_table x, (   SELECT partno, MAX(cmup) as tot   FROM my_table
GROUPBY partno) y WHERE x.partno = y.partno GROUP BY x.partno, x.status, y.tot
 

Cheers, Sam


pgsql-sql by date:

Previous
From: Mischa Sandberg
Date:
Subject: Re: CREATE TRIGGER ... FOR EACH STATEMENT
Next
From: faivrem@gmail.com (Mickael Faivre-Macon)
Date:
Subject: Oracle to Postgres