Re: 'GROUP BY' problem

From: "Peter Gibbs" <peter(at)emkel(dot)co(dot)za>
To: Mariusz Czu³ada <manieq(at)idea(dot)net(dot)pl>, "pgsql-general" <pgsql-general(at)postgresql(dot)org>
Subject: Re: 'GROUP BY' problem
Date: 2003-01-30 10:51:18
Message-ID: 009501c2c84d$84b9c7c0$0b01010a@emkel.co.za
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Mariusz Czulada wrote:

> I'd love to do it this way:
>
> SELECT
> date_trunc('15 minutes',ts),
> min(cpu_busy_pct),
> avg(cpu_busy_pct),
> max(cpu_busy_pct)
> FROM
> tmp_server_perf_sum
> GROUP BY
> date_trunc('15 minutes',ts);

The best I can think of at the moment is:
SELECT

(trunc(date_part('epoch',ts::timestamptz)/900)*900)::int::abstime::timestamp
,
min(cpu_busy_pct),
avg(cpu_busy_pct),
max(cpu_busy_pct)
FROM tmp_server_perf_sum
GROUP BY 1;

i.e. convert to seconds since epoch, truncate to 900 seconds = 15 minutes,
and convert
back to a timestamp.

You could wrap this in a function such as:

create function trunc_quarter_hour(timestamptz) returns timestamp
language plpgsql immutable strict
as '
begin
return (trunc(date_part(''epoch'',$1)/900)*900)::int::abstime;
end
';

and then use:
SELECT trunc_quarter_hour(ts), <etc>

This would allow you to substitute a better calculation into the function
without
changing your queries.

--
Peter Gibbs
EmKel Systems

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Eric Cholet 2003-01-30 11:54:32 Re: index on timestamp performance
Previous Message ayhan 2003-01-30 10:48:58 Re: plperl functions -- can they call each other?