Re: Group by and aggregates

From: Sam Mason <mason(at)f2s(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: Group by and aggregates
Date: 2004-11-04 17:25:08
Message-ID: 20041104172507.GG25775@sam.samason.me.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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
GROUP BY partno) y
WHERE x.partno = y.partno
GROUP BY x.partno, x.status, y.tot

Cheers,
Sam

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Michael Fuhr 2004-11-04 17:31:53 Re: Group by and aggregates
Previous Message SZŰCS Gábor 2004-11-04 17:19:13 Delayed result from another connection