Comparing sequential rows in a result

From: "Murray Long" <murray(at)skyrove(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Subject: Comparing sequential rows in a result
Date: 2008-10-29 09:01:39
Message-ID: 56acee400810290201x68cbc8s1bf9231a27999655@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

I'm relatively new to SQL, and am frequently running into the same problem,
How do I compare different rows in a result?

for example:
If I have a table of events consisting of a time stamp and the event type:

timestamp, event_type
12:00 a
12:10 b
12:20 a
...

I'd like to be able to select all the 'a' type events and calculate the time
since the previous 'a' event, to get:
timestamp, event_type, time_since_last
12:00 a 0:20
12:20 a NULL

What's the best way to to accomplish this?

Thanks in advance,
Murray

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Murray Long 2008-10-29 09:25:23 Re: Comparing sequential rows in a result
Previous Message Michael Higgins 2008-10-28 19:56:28 join table?