Re: how do i get differences between rows - Mailing list pgsql-sql

From Bruno Wolff III
Subject Re: how do i get differences between rows
Date
Msg-id 20031224033156.GB16470@wolff.to
Whole thread Raw
In response to how do i get differences between rows  (teknokrat <teknokrat@yahoo.com>)
Responses Re: how do i get differences between rows
List pgsql-sql
On Thu, Dec 18, 2003 at 12:21:35 +0000, teknokrat <teknokrat@yahoo.com> 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.


pgsql-sql by date:

Previous
From: Tom Lane
Date:
Subject: Re: Distributed keys
Next
From: "Iain"
Date:
Subject: Re: how to preserve \n in select statement