Re: how do you write aggregate function

From: Martijn van Oosterhout <kleptog(at)svana(dot)org>
To: Justin <justin(at)emproshunts(dot)com>
Cc: btober(at)ct(dot)metrocast(dot)net, pgsql-general(at)postgresql(dot)org
Subject: Re: how do you write aggregate function
Date: 2008-03-11 08:03:45
Message-ID: 20080311080345.GA17969@svana.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Mon, Mar 10, 2008 at 10:01:47AM -0500, Justin wrote:
> i wish that could work but need to keep track of the individual weights
> as its a percentage of the total amount of the weight.

Unless you have a different meaning of weighted average to me, I don't
think you do. AFAIK this would produce exactly the same result as what
your code does:

state_function(state,weight,value)
state[0] = state[0]+weight
state[1] = state[1]+weight*value

final_function(state)
return state[1]/state[0]

The state[0] is the same as your _sumedWeight. Because it's a constant in
the second loop you can do the division after. state[1] is your
_sumedWxV times _sumedWeight.

Have a nice day,
--
Martijn van Oosterhout <kleptog(at)svana(dot)org> http://svana.org/kleptog/
> Please line up in a tree and maintain the heap invariant while
> boarding. Thank you for flying nlogn airlines.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message frant101 2008-03-11 08:38:03 Re: Reindexdb + relation error
Previous Message longlong 2008-03-11 06:47:18 Re: gsoc ideas