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 4A65BD1B467 for ; Wed, 24 Dec 2003 03:32:38 +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 85886-08 for ; Tue, 23 Dec 2003 23:31:52 -0400 (AST) Received: from wolff.to (wolff.to [66.93.249.74]) by svr1.postgresql.org (Postfix) with SMTP id 20751D1B481 for ; Tue, 23 Dec 2003 23:31:47 -0400 (AST) Received: (qmail 21055 invoked by uid 500); 24 Dec 2003 03:31:56 -0000 Date: Tue, 23 Dec 2003 21:31:56 -0600 From: Bruno Wolff III To: teknokrat Cc: pgsql-sql@postgresql.org Subject: Re: how do i get differences between rows Message-ID: <20031224033156.GB16470@wolff.to> Mail-Followup-To: teknokrat , pgsql-sql@postgresql.org References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.4i X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200312/168 X-Sequence-Number: 16334 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.