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-04 20:09:39
Message-ID: BANLkTi=3nSFEc_k1QRMCQyws1ZKCH+gQ4Q@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Sat, Jun 4, 2011 at 12:15 PM, <lists-pgsql(at)useunix(dot)net> wrote:
>  I want to
> select records grouped by ID, ordered by timestamp, in ascending order so I'm
> starting with the oldest, that are at least X minutes apart.

Here my guess:

SELECT id, ts
FROM Yourtable AS A
AND NOT EXISTS ( SELECT *
FROM Yourtable AS B
WHERE B.id = A.id
AND B.ts > A.ts - INTERVAL '5 MINUTES'
AND B.tx < A.ts )

ORDER BY id, ts;

--
Regards,
Richard Broersma Jr.

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Charlie 2011-06-04 20:42:33 Re: [SQL] selecting records X minutes apart
Previous Message lists-pgsql 2011-06-04 19:15:59 Re: selecting records X minutes apart