Re: query to return hourly snapshot - Mailing list pgsql-sql

From Michael Fuhr
Subject Re: query to return hourly snapshot
Date
Msg-id 20060405024916.GA75532@winnie.fuhr.org
Whole thread Raw
In response to query to return hourly snapshot  (Richard Broersma Jr <rabroersma@yahoo.com>)
Responses Re: query to return hourly snapshot
List pgsql-sql
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
BYdate_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


pgsql-sql by date:

Previous
From: Richard Broersma Jr
Date:
Subject: query to return hourly snapshot
Next
From: Richard Broersma Jr
Date:
Subject: Re: query to return hourly snapshot