X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id 802ED11F6292 for ; Tue, 4 Apr 2006 23:49:26 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 80147-09 for ; Tue, 4 Apr 2006 23:49:26 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from tigger.fuhr.org (tigger.fuhr.org [63.214.45.158]) by postgresql.org (Postfix) with ESMTP id 4DCCD11F6256 for ; Tue, 4 Apr 2006 23:49:23 -0300 (ADT) Received: from winnie.fuhr.org (winnie.fuhr.org [10.1.0.1]) by tigger.fuhr.org (8.13.3/8.13.3) with ESMTP id k352nHB0025970 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Tue, 4 Apr 2006 20:49:20 -0600 (MDT) (envelope-from mfuhr@winnie.fuhr.org) Received: from winnie.fuhr.org (localhost [127.0.0.1]) by winnie.fuhr.org (8.13.4/8.13.4) with ESMTP id k352nGCL075605; Tue, 4 Apr 2006 20:49:16 -0600 (MDT) (envelope-from mfuhr@winnie.fuhr.org) Received: (from mfuhr@localhost) by winnie.fuhr.org (8.13.4/8.13.4/Submit) id k352nG3S075604; Tue, 4 Apr 2006 20:49:16 -0600 (MDT) (envelope-from mfuhr) Date: Tue, 4 Apr 2006 20:49:16 -0600 From: Michael Fuhr To: Richard Broersma Jr Cc: pgsql-sql@postgresql.org Subject: Re: query to return hourly snapshot Message-ID: <20060405024916.GA75532@winnie.fuhr.org> References: <20060405005826.45346.qmail@web31814.mail.mud.yahoo.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20060405005826.45346.qmail@web31814.mail.mud.yahoo.com> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.121 required=5 tests=[AWL=0.121] X-Spam-Score: 0.121 X-Spam-Level: X-Archive-Number: 200604/15 X-Sequence-Number: 24824 On Tue, Apr 04, 2006 at 05:58:26PM -0700, Richard Broersma Jr wrote: > I am look for help in developing a query that will return the nearest > process record that was logged at or after each hour in a day (i.e. > hourly snapshot). Are you looking for something like this? SELECT p.process, date_trunc('hour', p.tstamp) AS hour FROM process AS p JOIN ( SELECT date_trunc('hour', tstamp), min(tstamp) FROM process WHERE date_trunc('day', tstamp) = '2005-10-26' GROUP BY date_trunc('hour', tstamp) ) AS s ON s.min = p.tstamp ORDER BY hour; Or, using PostgreSQL's non-standard DISTINCT ON clause: SELECT DISTINCT ON (date_trunc('hour', tstamp)) process, date_trunc('hour', tstamp) AS hour FROM process WHERE date_trunc('day', tstamp) = '2005-10-26' ORDER BY date_trunc('hour', tstamp), tstamp; -- Michael Fuhr