X-Original-To: pgsql-general-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id 48F1BD1CC93 for ; Fri, 23 Apr 2004 01:38:02 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 51422-06 for ; Fri, 23 Apr 2004 01:38:01 -0300 (ADT) Received: from smtp.istop.com (dci.doncaster.on.ca [66.11.168.194]) by svr1.postgresql.org (Postfix) with ESMTP id 7763CD1BB7B for ; Fri, 23 Apr 2004 01:38:00 -0300 (ADT) Received: from stark.xeocode.com (gsstark.mtl.istop.com [66.11.160.162]) by smtp.istop.com (Postfix) with ESMTP id 26C4F17C31A; Fri, 23 Apr 2004 00:38:00 -0400 (EDT) Received: from localhost ([127.0.0.1] helo=stark.xeocode.com) by stark.xeocode.com with smtp (Exim 3.36 #1 (Debian)) id 1BGsRr-0003tj-00; Fri, 23 Apr 2004 00:38:00 -0400 To: pgsql-general@postgresql.org Subject: Re: staggered query? References: <002901c4274b$cb7ace30$643b1cac@Titanium> <20040422200954.GF1044@noquarter.fepede.org> In-Reply-To: <20040422200954.GF1044@noquarter.fepede.org> From: Greg Stark Organization: The Emacs Conspiracy; member since 1992 Date: 23 Apr 2004 00:37:59 -0400 Message-ID: <871xmf48d4.fsf@stark.xeocode.com> Lines: 53 User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Virus-Scanned: by amavisd-new at postgresql.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200404/880 X-Sequence-Number: 60122 Federico Pedemonte 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