Re: selecting records X minutes apart

From: Richard Broersma <richard(dot)broersma(at)gmail(dot)com>
To: lists-pgsql(at)useunix(dot)net
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: selecting records X minutes apart
Date: 2011-06-05 02:10:46
Message-ID: BANLkTikWBFevHv9igfGVKNpx792PuKY+KA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Sat, Jun 4, 2011 at 6:36 PM, <lists-pgsql(at)useunix(dot)net> wrote:

> 0       20:00
> 0       20:05
> 0       20:08
> 0       20:10
>
> I want records, starting from the oldest record (20:00), that are at least 5
> minutes apart.  So 20:00, 20:05, 20:10 but 20:08 - 20:05 is only 3 minutes so
> it is to be ignored.

Here is my next guess: --although I guess my use of WITH Recursive is correct

WITH RECURSIVE Accum_ts( id, ts ) AS
( SELECT id, MIN( ts )
FROM Yourtable
GROUP BY id )
UNION ALL ( SELECT B.id, MIN( B.ts )
FROM Yourtable AS B
WHERE B.id = Accum_ts.id
AND B.ts >= Accum_ts.ts - INTERVAL '5 MINUTES'
GROUP BY B.id
HAVING MIN( B.ts ) <= MAX( B.ts ))

SELECT id, ts
FROM Accum_ts
ORDER BY id, ts;

--
Regards,
Richard Broersma Jr.

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Charlie 2011-06-05 04:02:40 Re: [SQL] selecting records X minutes apart
Previous Message lists-pgsql 2011-06-05 01:36:23 Re: selecting records X minutes apart