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 D1C71539A6 for ; Mon, 4 Apr 2005 11:40:04 +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 56728-10 for ; Mon, 4 Apr 2005 10:40:00 +0000 (GMT) Received: from anchor-post-34.mail.demon.net (anchor-post-34.mail.demon.net [194.217.242.92]) by svr1.postgresql.org (Postfix) with ESMTP id 20F00539C2 for ; Mon, 4 Apr 2005 11:39:56 +0100 (BST) Received: from mwynhau.demon.co.uk ([193.237.186.96] helo=mainbox.archonet.com) by anchor-post-34.mail.demon.net with esmtp (Exim 4.42) id 1DIOzt-0005O5-Df; Mon, 04 Apr 2005 10:39:58 +0000 Received: from [192.168.1.17] (client17.archonet.com [192.168.1.17]) by mainbox.archonet.com (Postfix) with ESMTP id CC9271640E; Mon, 4 Apr 2005 11:39:55 +0100 (BST) Message-ID: <4251197C.1060904@archonet.com> Date: Mon, 04 Apr 2005 11:39:56 +0100 From: Richard Huxton User-Agent: Mozilla Thunderbird 1.0 (X11/20041206) X-Accept-Language: en-us, en MIME-Version: 1.0 To: "Michael L. Hostbaek" Cc: pgsql-sql@postgresql.org Subject: Re: select & group by References: <20050404101647.GJ23648@mich2.itxmarket.com> In-Reply-To: <20050404101647.GJ23648@mich2.itxmarket.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.06 tagged_above=0 required=5 tests=AWL, FORGED_RCVD_HELO X-Spam-Level: X-Archive-Number: 200504/41 X-Sequence-Number: 20934 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. How about something like: SELECT aa.part, aa.mfg, aa.qty, bb.qty AS qty_incoming, CASE WHEN aa.price > bb.price THEN aa.price ELSE bb.price END AS highest_price, aa.eta FROM ( SELECT part,mfg,qty,price FROM mytable WHERE eta IS NOT NULL ) aa, ( SELECT part,mfg,qty,price FROM mytable WHERE eta IS NULL ) bb WHERE aa.part = bb.part AND aa.mfg=bb.mfg ; This is assuming you only have one row with "eta" set for each (part,mfg). If not, you'll have to identify which row you want. -- Richard Huxton Archonet Ltd