Hi all,

I have a table like:

value int,
quarter timestamp

I need an aggregate function that gives back the maximum "value" using 
this algorithm:

AVG of the first hour (first 4 quarters) (AVG0)
same as above, but 1 quarter later (AVG1)
....
same as above, but n quarters later (AVGn)

result: the quarter where AVGn was MAX.

Example:

quarter         value           AVGn

2008-01-01 00:00        10      
2008-01-01 00:15        15
2008-01-01 00:30        5
2008-01-01 00:45        20      -> 12.5 ((10+15+5+20)/4)
2008-01-01 01:15        2       -> 21   ((15+5+20+2)/4)
2008-01-01 01:30        30      -> 14.25 ((5+20+2+30)/4))

the result should be ('2008-01-01 00:15', 21)



It would be very easy if the input to the custom aggregate function was 
ordered (because I would keep 4 internal counters), but I guess there's 
no way of "forcing" the ordering of the input to the function, right?

So I have to cache all the (quarter,value) couples and give back a 
result at the end, right?




        







-- 
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

Reply via email to