X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (neptune.hub.org [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id 42C88D1B466 for ; Wed, 24 Dec 2003 13:32:33 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 46507-05 for ; Wed, 24 Dec 2003 09:31:46 -0400 (AST) Received: from news.hub.org (news.hub.org [200.46.204.72]) by svr1.postgresql.org (Postfix) with ESMTP id 680A3D1B47A for ; Wed, 24 Dec 2003 09:31:43 -0400 (AST) Received: from news.hub.org (news.hub.org [200.46.204.72]) by news.hub.org (8.12.9/8.12.9) with ESMTP id hBODVhU6065639 for ; Wed, 24 Dec 2003 13:31:43 GMT (envelope-from news@news.hub.org) Received: (from news@localhost) by news.hub.org (8.12.9/8.12.9/Submit) id hBODOEih059589 for pgsql-sql@postgresql.org; Wed, 24 Dec 2003 13:24:14 GMT From: teknokrat X-Newsgroups: comp.databases.postgresql.sql Subject: Re: how do i get differences between rows Date: Tue, 23 Dec 2003 13:23:35 +0000 Organization: Hub.Org Networking Services Lines: 31 Message-ID: References: <20031224033156.GB16470@wolff.to> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: usenet@news.hub.org User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.5) Gecko/20031007 X-Accept-Language: en-us, en, ru In-Reply-To: <20031224033156.GB16470@wolff.to> To: pgsql-sql@postgresql.org X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200312/172 X-Sequence-Number: 16338 Bruno Wolff III wrote: > On Thu, Dec 18, 2003 at 12:21:35 +0000, > teknokrat wrote: > >>I am after an sql query that can give the differnces between consecutive >>timestamp values in the resultset. >>anyone know how to do this? > > > I think you can do something like the following (untested) on most systems. > select a.stamp - (select max(stamp) from tablename > where tablename.stamp < a.stamp) from tablename; > > For postgres this (also untested) might be faster if there is an index > on stamp. > select a.stamp - (select stamp from tablename where tablename.stamp < a.stamp > order by stamp desc limit 1) from tablename; > > The above relies on timestamps being unique. The difference for the smallest > timestamp will be null. > > ---------------------------(end of broadcast)--------------------------- > TIP 8: explain analyze is your friend > this may give me difference between current row and oldest row but what I want is row(i).timestamp - row(i-1).timestamp. Also I would like to do it for timestamp values whose hours are within normal working hours i.e 9-5.