Re: Comparing sequential rows in a result - Mailing list pgsql-sql

From Murray Long
Subject Re: Comparing sequential rows in a result
Date
Msg-id 56acee400810290225s36f4d3d1r7a3c079daa31ba7f@mail.gmail.com
Whole thread Raw
In response to Comparing sequential rows in a result  ("Murray Long" <murray@skyrove.com>)
List pgsql-sql
Here's one solution:<br /><br />create temp sequence tsec;<br />create temp table ttab as select nextval('tsec'), *
from(select * from events where event_type='a' order by timestamp desc) as troz;<br />select ttab.*,
ttab2.timestamp-ttab.timestampfrom ttab join ttab as ttab2 on ttab2.nextval = ttab.nextval+1;<br /><br />This works,
butseems a very messy way to accomplish somehting quite simple.<br /><br /><br /><div class="gmail_quote">On Wed, Oct
29,2008 at 11:01 AM, Murray Long <span dir="ltr"><<a
href="mailto:murray@skyrove.com">murray@skyrove.com</a>></span>wrote:<br /><blockquote class="gmail_quote"
style="border-left:1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">I'm relatively new to
SQL,and am frequently running into the same problem, How do I compare different rows in a result?<br /><br />for
example:<br/>If I have a table of  events consisting of a time stamp and the event type:<br /><br />timestamp,    
event_type<br/>12:00                 a<br />12:10                 b<br />12:20                 a<br />...<br /><br
/>I'dlike to be able to select all the 'a' type events and calculate the time since the previous 'a' event, to get:<br
/>timestamp,     event_type,     time_since_last<br /> 12:00                 a                             0:20<br />
12:20                a                             NULL<br /><br />What's the best way to to accomplish this? <br /><br
/><br/>Thanks in advance,<br /><font color="#888888">Murray<br /><br /><br /><br /></font></blockquote></div><br /> 

pgsql-sql by date:

Previous
From: "Murray Long"
Date:
Subject: Comparing sequential rows in a result
Next
From: "Kevin Duffy"
Date:
Subject: simple SQL query