pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Bruno Wolff III <bruno@wolff.to>
To: teknokrat <teknokrat@yahoo.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: how do i get differences between rows
Date: Tue, 23 Dec 2003 21:31:56 -0600
Message-ID: <20031224033156.GB16470@wolff.to> (raw)
In-Reply-To: <brs64d$1u47$1@news.hub.org>
References: <brs64d$1u47$1@news.hub.org>

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.



view thread (3+ messages)

Message-ID: <20031224033156.GB16470@wolff.to>
Permalink:  ../20031224033156.GB16470@wolff.to/
Also on:    postgresql.org/message-id/20031224033156.GB16470@wolff.to

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: bruno@wolff.to, teknokrat@yahoo.com
  Subject: Re: how do i get differences between rows
  In-Reply-To: <20031224033156.GB16470@wolff.to>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox