From: | Thalis Kalfigkopoulos <tkalfigo(at)gmail(dot)com> |
---|---|
To: | rod(at)iol(dot)ie |
Cc: | ochaussavoine <olivier(dot)chaussavoine(at)gmail(dot)com>, pgsql-general(at)postgresql(dot)org |
Subject: | Re: obtain the difference between successive rows |
Date: | 2012-10-20 13:06:27 |
Message-ID: | CAEkCx9FF3ukNwMxh4VK705bVeDHi5rrKQNYnCDGPk+O2WiGZ+Q@mail.gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
On Sat, Oct 20, 2012 at 8:02 AM, Raymond O'Donnell <rod(at)iol(dot)ie> wrote:
> On 20/10/2012 11:54, ochaussavoine wrote:
>> Hi,
>> I have a table 'tmvt' with a field 'created' in the row, and would like to
>> compute the difference between successive rows. The solution I found is:
>>
>
> I think you can do it with a window function.
>
> http://www.postgresql.org/docs/9.2/static/tutorial-window.html
> http://www.postgresql.org/docs/9.2/static/functions-window.html
>
> Ray.
In particular you're looking probably for the lag() window function.
For example if you have a timestamp column "ts" that's increasing
monotonically and you want to check the difference of each row's
timestamp with the chronologically previous row's timestamp you'd do
something like:
$ SELECT id, ts, lag(ts) OVER (order by ts) AS prev_ts FROM mytable;
This will display as third column the previous row's ts.
You may find reading this introduction to window fuctions useful:
https://www.pgcon.org/2009/schedule/attachments/98_Windowing%20Functions.pdf
best tregards,
Thalis
From | Date | Subject | |
---|---|---|---|
Next Message | Berend Tober | 2012-10-20 15:21:47 | Re: obtain the difference between successive rows |
Previous Message | Raymond O'Donnell | 2012-10-20 11:02:17 | Re: obtain the difference between successive rows |