Re: Need to subtract values between previous and current row

From: Ragnar <gnari(at)hive(dot)is>
To: Partha Guha Roy <partha(dot)guha(dot)roy(at)gmail(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Need to subtract values between previous and current row
Date: 2006-12-15 13:01:50
Message-ID: 1166187710.6369.164.camel@localhost.localdomain
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On fös, 2006-12-15 at 18:27 +0600, Partha Guha Roy wrote:
> Hi,
>
> I have a table that contains data like this:
>
> ID ATIME (MM/dd/yyyy)
> == ====
> 1 10/12/2006
> 2 10/14/2006
> 3 10/18/2006
> 4 10/22/2006
> 5 10/30/2006
>
> Now I need a query that will subtract atime of row 1 from row 2, row2
> from row3 and so on...

if there are no missing IDs you can use a self join
SELECT t1.atime,t2,atime
FROM t AS t1
JOIN t AS t2 ON (t1.ID=t2.ID+1)

otherwise, you could use a loop in a pl/pgpsql function

gnari

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Joe 2006-12-15 13:05:35 Re: Need to subtract values between previous and current row
Previous Message Partha Guha Roy 2006-12-15 12:27:35 Need to subtract values between previous and current row