# SELF JOIN to compare two successive rows

**URL:** <https://www.ruby-forum.com/t/self-join-to-compare-two-successive-rows/161246>\
**Category:** Rails\
**Created:** [February 24, 2009, 6:00pm UTC](https://www.ruby-forum.com/t/self-join-to-compare-two-successive-rows/161246 "2009-02-24T18:00:56Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![martin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/73ab20/32.png) [@martin](https://www.ruby-forum.com/u/martin)\
**Post date:** [February 24, 2009, 6:00pm UTC](https://www.ruby-forum.com/t/self-join-to-compare-two-successive-rows/161246/1 "2009-02-24T18:00:56Z")

</div>

Hi,

I have a DB that different computers add entries to roughly every 10  
seconds.  
Later on I want to check “gaps”, when two entries from one computer  
are further than 20 seconds apart and other things that have to do  
with comparing two rows.  
In MYSQL I added a new column “succ\_id” for this which I increse on a  
per-computer basis.  
I did this to be able to find two successive rows via a JOIN-query in  
MYSQL.  
So I would find the rows by  
SELECT t1._, t2.t_ FROM computerdata t1 inner join computerdata t2 on  
t1.succ\_id=t2.succ\_id-1 and t1.seconds\<t2.seconds-20;  
Now I’m trying to figure out how to do this “the rails way”.  
The act as tree is a little bit overweighted I think and I would need  
to restructure my DB as I don’t have the references to the have  
antecessor.  
Can anybody give me a hint?  
Thanks in advance,

Martin
