From: Sam Mason <mason@f2s.com>
To: pgsql-sql@postgresql.org
Subject: Re: Group by and aggregates
Date: Thu, 4 Nov 2004 17:25:08 +0000
Message-ID: <20041104172507.GG25775@sam.samason.me.uk> (raw)
In-Reply-To: <20041104165430.GD94770@mich2.itxmarket.com>
References: <20041104165430.GD94770@mich2.itxmarket.com>
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: mason@f2s.com
Subject: Re: Group by and aggregates
In-Reply-To: <20041104172507.GG25775@sam.samason.me.uk>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox