X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 368EB5398A for ; Mon, 4 Apr 2005 11:46:20 +0100 (BST) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 60837-08 for ; Mon, 4 Apr 2005 10:46:16 +0000 (GMT) Received: from quasar.skima.is (quasar.skima.is [212.30.200.205]) by svr1.postgresql.org (Postfix) with ESMTP id 8DCEB5375C for ; Mon, 4 Apr 2005 11:46:14 +0100 (BST) Received: from gnari ([157.157.159.208] [157.157.159.208]) by quasar.skima.is with ESMTP; Mon, 4 Apr 2005 10:46:16 Z Subject: Re: select & group by From: Ragnar =?ISO-8859-1?Q?Hafsta=F0?= To: "Michael L. Hostbaek" Cc: pgsql-sql@postgresql.org In-Reply-To: <20050404101647.GJ23648@mich2.itxmarket.com> References: <20050404101647.GJ23648@mich2.itxmarket.com> Content-Type: text/plain Date: Mon, 04 Apr 2005 10:47:10 +0000 Message-Id: <1112611630.19933.6.camel@localhost.localdomain> Mime-Version: 1.0 X-Mailer: Evolution 2.0.2 Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.001 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200504/42 X-Sequence-Number: 20935 On Mon, 2005-04-04 at 12:16 +0200, Michael L. Hostbaek wrote: > I've got a problem selecting some specific data from my table. Imagine > the following rows: > > part | mfg | qty | price | eta > --------------------------------------- > TEST1 ABC 10 100 (No ETA, as item is in stock) > TEST1 ABC 12 120 04/04 > TEST2 CBA 17 10 05/05 > TEST2 CBA 10 20 (No ETA, as item is in stock) > > I'd like my selection to produce the following result: > > part | mfg | qty | qty incoming | highest price | eta > ------------------------------------------------------------- > TEST1 ABC 10 12 120 04/04 > TEST2 CBA 10 17 20 05/05 > > Any clues on how to do this ? I kow the group by part, mfg, max(price) - > but I do not know how to deal with the splitting up qty and stock qty > and incoming qty. use CASE. for example, something like: select part,mfg, sum(CASE WHEN eta is NULL then qty ELSE 0 END) as qty, sum(CASE WHEN eta is NULL then 0 ELSE qty END) as "qty incoming", max(price) as "highest price", min(eta) as eta group by part,mfg; gnari