Re: staggered query?

From: Greg Stark <gsstark(at)mit(dot)edu>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: staggered query?
Date: 2004-04-23 04:37:59
Message-ID: 871xmf48d4.fsf@stark.xeocode.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-sql

Federico Pedemonte <fepede(at)email(dot)it> writes:

> On Wed, Apr 21, 2004 at 10:53:16AM +0800, Vincent Ladlad wrote:
> >
> > the table contains hundreds of thousands of records.
> > i need to get all the entries/records at every 10 seconds
> > interval. example, given a table:
> >
> > hh/mm/ss | data
> > ---------------
> > 00:00:00 1
> > 00:00:01 2
> > 00:00:02 3
> > 00:00:03 4
> > 00:00:04 5
> > 00:00:05 6
> > 00:00:06 7
> > 00:00:07 8
> > ..
> > ..
> >
> > my query should return:
> > 00:00:10
> > 00:00:20
> > 00:00:30
> > (etc)
>
> If I understood your problem, the only solution i found was write a
> simple plpgsql function (read at the end of the mail).

There are plenty of solutions for this using standard SQL or non-standard but
still plain SQL queries.

Do you have exactly one sample for every second? And do you want precisely the
first second of the ten second interval? If so then all you really need are
every row where the seconds are divisible by 10.

select * from table where hhmmss::abstime::integer % 10 = 0;

(there are probably more standard ways of testing if the seconds are divisible
by 10, but this is the first way that came to mind)

If you don't always have a sample for every second and just want the first
sample from each ten second interval you could do something like:

select distinct on (hhmmss::abstime::integer / 10) hhmmss order by hhmmss::abstime::integer / 10;

but i expect that would be slower since it would have to do a big sort.

--
greg

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Philipp Buehler 2004-04-23 05:35:36 Re: 7.3.4 on Linux: UPDATE .. foo=foo+1 degrades massivly
Previous Message Shawn Harrison 2004-04-23 04:36:17 OT: FreeBSD help (was Re: Installing postgres)

Browse pgsql-sql by date

  From Date Subject
Next Message Stijn Vanroye 2004-04-23 07:14:55 Re: Order by YYYY MM DD in reverse chrono order trouble
Previous Message Tom Lane 2004-04-23 02:34:16 Re: Is there an easy way to normalize-space with given string functions