Re: [SQL] selecting records X minutes apart

2011-06-13 Thread Gavin Flower
How about this (that does not require special functions nor triggers: DROP TABLE IF EXISTS val; CREATE TABLE val ( id int, ts timestamp ); INSERT INTO val VALUES (0, '1-Jan-2010 20:00'), (1, '1-Jan-2010 20:03'), (1, '1-Jan-2010 20:04'), (0, '1-Jan-2010 20:05'),

Re: [SQL] selecting records X minutes apart

2011-06-05 Thread lists-pgsql
Thank you all who replied!! It looks like Sugawara's recursive solution does the trick. Unfortunately performance is quite poor for the sample dataset I'm working with which is a table of about 5 records. Indeed, there are indexes applied to the table. I believe the recursive select is bein

Re: [SQL] selecting records X minutes apart

2011-06-05 Thread Masaru Sugawara
On Fri, 3 Jun 2011 15:52:53 -0400 lists-pg...@useunix.net wrote: I also think you might want to use WITH RECURSIVE clause. This SQL searches the case of an interval of 5 minutes or more, and sets a relationship between a parent to its child. CREATE TABLE tbl(id integer, ts time) ; INSERT INTO

Re: [SQL] selecting records X minutes apart

2011-06-05 Thread Masaru Sugawara
On Fri, 3 Jun 2011 15:52:53 -0400 lists-pg...@useunix.net wrote: I also think you might want to use WITH RECURSIVE clause. This SQL searches the case of an interval of 5 minutes or more, and sets a relationship between a parent to its child. CREATE TABLE tbl(id integer, ts time) ; INSERT INTO

[SQL] Re: [SQL] selecting records X minutes apart

2011-06-04 Thread Charlie
20:03, which is in. Etc. ----- Reply message - From: lists-pg...@useunix.net Date: Fri, Jun 3, 2011 4:52 pm Subject: [SQL] selecting records X minutes apart To: I have a table that, at a minimum, has ID and timestamp columns. Records are inserted into with random IDs and timestamps. Dupl

Re: [SQL] selecting records X minutes apart

2011-06-04 Thread Richard Broersma
On Sat, Jun 4, 2011 at 6:36 PM, 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

Re: [SQL] selecting records X minutes apart

2011-06-04 Thread lists-pgsql
Let's a take a look at just the input set for ID 0. 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. I was hoping

Re: [SQL] selecting records X minutes apart

2011-06-04 Thread Kevin Crain
My approach would be to add a column for LAST_TS and place a trigger on insert that populates this new column. Then you have something you can put in your WHERE clause to test on. On Fri, Jun 3, 2011 at 12:52 PM, wrote: > I have a table that, at a minimum, has ID and timestamp columns.  Records

Re: [SQL] selecting records X minutes apart

2011-06-04 Thread Kevin Crain
Why is (0,20:10) listed in your expected results when there is a (0,20:08)? On Fri, Jun 3, 2011 at 12:52 PM, wrote: > I have a table that, at a minimum, has ID and timestamp columns.  Records > are inserted into with random IDs and timestamps.  Duplicate IDs are allowed. > > I want to select re

Re: [SQL] selecting records X minutes apart

2011-06-04 Thread lists-pgsql
Did you mean WHERE in place of your first AND? If so I already had something like this but it only returns one set, the oldest group of entries for each ID. On Sat, Jun 04, 2011 at 01:09:39PM -0700, Richard Broersma wrote: > On Sat, Jun 4, 2011 at 12:15 PM, wrote: > >  I want to > > select rec

[SQL] Re: [SQL] selecting records X minutes apart

2011-06-04 Thread Charlie
Subject: [SQL] selecting records X minutes apart To: On Sat, Jun 04, 2011 at 11:45:08AM +, Jasen Betts wrote: > On 2011-06-03, lists-pg...@useunix.net wrote: > > > > ID TS (HH:MM) > > --- > > 0 20:00 > > 0 20:05 > > 0 20:10 > > 1 2

Re: [SQL] selecting records X minutes apart

2011-06-04 Thread Richard Broersma
On Sat, Jun 4, 2011 at 12:15 PM, 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 *

Re: [SQL] selecting records X minutes apart

2011-06-04 Thread lists-pgsql
On Sat, Jun 04, 2011 at 11:45:08AM +, Jasen Betts wrote: > On 2011-06-03, lists-pg...@useunix.net wrote: > > > > ID TS (HH:MM) > > --- > > 0 20:00 > > 0 20:05 > > 0 20:10 > > 1 20:03 > > 1 20:09 > > > > > > Does my question make sense? > > no, why is (1,20:04) exclu

Re: [SQL] selecting records X minutes apart

2011-06-04 Thread lists-pgsql
The TS column type is actually a timestamp with out timezone and yes I want to take seconds into account so both of your entries would be included in the result. On Fri, Jun 03, 2011 at 06:01:53PM -0700, Kevin Crain wrote: > Will you be using a full timestamp with that or are you only concerned >

Re: [SQL] selecting records X minutes apart

2011-06-04 Thread Kevin Crain
It looks like maybe he is trying to fetch records that either have no previous entries or have another record with a timestamp 5 minutes before them at the time they are inserted... On Sat, Jun 4, 2011 at 4:45 AM, Jasen Betts wrote: > On 2011-06-03, lists-pg...@useunix.net wrote: >> >> ID    TS

Re: [SQL] selecting records X minutes apart

2011-06-04 Thread Jasen Betts
On 2011-06-03, lists-pg...@useunix.net wrote: > > IDTS (HH:MM) > --- > 0 20:00 > 0 20:05 > 0 20:10 > 1 20:03 > 1 20:09 > > > Does my question make sense? no, why is (1,20:04) excluded, but (0,20:05) included? both records are 5 minutes from the newest. --

Re: [SQL] selecting records X minutes apart

2011-06-03 Thread Kevin Crain
Will you be using a full timestamp with that or are you only concerned about hours and minutes? If you want a full timestamp do you care about the seconds? For example, do you want to be able to do this for '2011-06-01 23:59:04' and '2011-06-02 00:04:04'? On Fri, Jun 3, 2011 at 12:52 PM, wrote:

[SQL] selecting records X minutes apart

2011-06-03 Thread lists-pgsql
I have a table that, at a minimum, has ID and timestamp columns. Records are inserted into with random IDs and timestamps. Duplicate IDs are allowed. I want to select records grouped by ID, ordered by timestamp that are X minutes apart. In this case X is 5. Note, the intervals are not X minute