Received: from magus.postgresql.org (magus.postgresql.org [87.238.57.229]) by mail.postgresql.org (Postfix) with ESMTP id B00F91763BA6 for ; Mon, 21 May 2012 00:11:13 -0300 (ADT) Received: from etc.kandalaya.org ([83.143.86.98] helo=images.kandalaya.org) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1SWJ1Z-0004qC-23 for pgsql-sql@postgresql.org; Mon, 21 May 2012 03:11:12 +0000 Received: from mail.linux-delhi.org (triband-del-59.177.78.140.bol.net.in [59.177.78.140] (may be forged)) by images.kandalaya.org (8.14.3/8.14.3/Debian-9) with ESMTP id q4L3Akkm009319 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Mon, 21 May 2012 08:40:49 +0530 Received: from mail.linux-delhi.org (raju@localhost [127.0.0.1]) by mail.linux-delhi.org (8.14.4/8.14.4/Debian-2) with ESMTP id q4L3ARNx023497 for ; Mon, 21 May 2012 08:40:28 +0530 From: "Raj Mathur (=?utf-8?b?4KSw4KS+4KSc?= =?utf-8?b?IOCkruCkvuCkpeClgeCksA==?=)" Reply-To: raju@linux-delhi.org Organization: Kandalaya To: pgsql-sql@postgresql.org Subject: Re: master/detail Date: Mon, 21 May 2012 08:40:26 +0530 User-Agent: KMail/1.13.7 (Linux/3.2.0-2-686-pae; KDE/4.6.5; i686; ; ) References: <4FB7EEC6.1030004@gmx.net> <4FB8B489.3050701@gmx.net> <4FB97B70.6080604@greenpeace.org> In-Reply-To: <4FB97B70.6080604@greenpeace.org> MIME-Version: 1.0 Content-Type: Text/Plain; charset="iso-8859-15" Content-Transfer-Encoding: 7bit Message-Id: <201205210840.26777.raju@linux-delhi.org> X-Spam-Status: No, score=-7.8 required=5.0 tests=AWL,BAYES_00, DNS_FROM_RFC_BOGUSMX,LOCAL_FROM_RAJU,RCVD_IN_PBL,RCVD_IN_SORBS_DUL, RCVD_IN_XBL,RDNS_NONE autolearn=ham version=3.2.5 X-Spam-Checker-Version: SpamAssassin 3.2.5 (2008-06-10) on images.kandalaya.org X-Virus-Scanned: clamav-milter 0.95.2 at images.kandalaya.org X-Virus-Status: Clean X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201205/56 X-Sequence-Number: 36604 On Monday 21 May 2012, Jan Bakuwel wrote: > Hi, > > I'm trying to get my head around the following question. As an > example take a table with products: > > productid (pk) > name > > and productprice > > productpriceid (pk) > productid (fk) > pricedate > price > > There are multiple records in productprice for each product as prices > (often) go up and (less often) go down. > > I'm looking for a query that returns the following: > > productid, name, pricedate, current_price, difference > > current_price is the latest (ie. most recent date) price of the > product and difference is the difference in price between the latest > price and the price before the latest. > > Any suggestions how to do this with SQL only? I can make it work with > a function (probably less efficient) but think this should be > possible with SQL too... Something like this ought to do it (not tested): select latest.price, latest.price - next.price from (select price from productprice where productid = 1 order by pricedate desc limit 1) latest, (select price from productprice where productid = 1 order by pricedate desc limit 2 offset 1) next; Regards, -- Raj -- Raj Mathur || raju@kandalaya.org || GPG: http://otheronepercent.blogspot.com || http://kandalaya.org || CC68 It is the mind that moves || http://schizoid.in || D17F