EDIT: I thought about this a bit and realized that you can't just go from 2 minutes to 5 minutes. It does not add up. I will keep track of this, but the following code works as soon as you have 1 minute data for aggregation!
-
If the data is in the beginning format, you can use the code inside this function or create a function in your database to facilitate access:
CREATE OR REPLACE FUNCTION dev.beginning_datetime_floor(timestamp without time zone, integer) RETURNS timestamp without time zone AS $BODY$ SELECT date_trunc('minute',timestamp with time zone 'epoch' + floor(extract(epoch from $1)/($2*60))*$2*60 * interval '1 second') at time zone 'CST6CDT' $BODY$ LANGUAGE sql VOLATILE;
You just download this integer number of minutes you want to copy (use 1, 2, 3, 4, 5, 6, 10, 12, 15, 20, or 30), here are a couple of results:
select dev.beginning_datetime_floor('2012-01-01 02:02:21',2)
= '2012-01-01 02:02:00'
select dev.beginning_datetime_floor('2012-01-01 02:02:21',5)
= '2012-01-01 02:00:00'
Just test it and add or subtract time to process the start and end timestamps using the built-in timestamp functions .
When you get the right timestamp, do what Craig said and GROUP BY at that timestamp in combination with your desired aggregate functions (probably average).
You can check / configure it with:
date_trunc('minute',timestamp with time zone 'epoch' + floor(extract(epoch from your_datetime)/(interval_minutes*60))*interval_minutes*60 * interval '1 second') at time zone 'CST6CDT'
It may turn out that you want to average the timestamps - if, for example, the length of your interval is unstable. To do this, you can make a similar function that rounds the timestamp, instead of making the floor.