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 1EF4052814 for ; Mon, 6 Jun 2005 13:22:02 -0300 (ADT) 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 22493-09 for ; Mon, 6 Jun 2005 16:21:52 +0000 (GMT) Received: from wolff.to (wolff.to [66.93.197.194]) by svr1.postgresql.org (Postfix) with SMTP id F029F52805 for ; Mon, 6 Jun 2005 13:21:51 -0300 (ADT) Received: (qmail 31069 invoked by uid 500); 6 Jun 2005 16:21:00 -0000 Date: Mon, 6 Jun 2005 11:21:00 -0500 From: Bruno Wolff III To: Alain Reymond Cc: pgsql-sql@postgresql.org Subject: Re: How do write a query... Message-ID: <20050606162100.GA30453@wolff.to> Mail-Followup-To: Bruno Wolff III , Alain Reymond , pgsql-sql@postgresql.org References: <429DDBB1.7000204@skynet.be> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <429DDBB1.7000204@skynet.be> User-Agent: Mutt/1.5.6i X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.008 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200506/86 X-Sequence-Number: 21687 On Wed, Jun 01, 2005 at 18:00:49 +0200, Alain Reymond wrote: > Hello, > > I have the following problem : > > I have a table like > Id Num Date AValue > 1 10 01/01/2005 50 > 2 10 31/05/2005 60 > 3 25 02/02/2005 55 > 4 25 15/03/2005 43 > 5 25 28/05/2005 62 > etc.. > > Id is unique, Num is an identification number with duplicates possible, > date is a ... date and Avalue... a value! > > If we have > Id Num Date AValue > Id1 Num1 Date1 AValue1 > Id2 Num1 Date2 AValue2 > > The table is ordered on Num+Date. > What I would like to calculate is (AValue2-AValue1) for a given Num > (here num1). > > In this case, I would have to calculate > 60-50 for Num 10 > and > 43-55, 62-43 for Num 25. > > Do you have any idea if it can be done simply with a request... I would suggest using a different design. Probably the easiest is to put the start and end dates and values in one record. Otherwise you could write a function or have your application read entries for a particular Num value ordered by date and treat them as pairs. If any intervals overlap then there really isn't a fix.