Re: [GENERAL] Calculating a moving average

2005-01-26 Thread PFC
Make a plpgsql function which will iterate over the rows on which the moving average is to be done (FOR row IN SELECT), of course use the correct order, then use an array as a FIFO, add a row to the moving average and push it, pop the old one and substract it. Roundoff errors will bite your

Re: [GENERAL] Calculating a moving average (Coding style)

2005-01-24 Thread mstory
I personally use 3 seperate triggers on most occasions, depending on how different the action for each seperate action is, it's just easier for me and my people to logically distinguish the functions that way, but the example in the 7.4 documentation for triggers is given using the form that i wrot

Re: [GENERAL] Calculating a moving average (Coding style)

2005-01-24 Thread Russell Smith
On Mon, 24 Jan 2005 08:32 pm, Alban Hertroys wrote: > [EMAIL PROTECTED] wrote: > > CREATE OR REPLACE FUNCTION get_bar_avg() RETURNS TRIGGER AS ' > > DECLARE > > bar_record RECORD; > > x INTEGER; > > y DOUBLE PRECISION := 0; > > BEGIN > > IF TG_OP = ''INSERT'' THEN > > y := y + NEW.ba

Re: [GENERAL] Calculating a moving average (Coding style)

2005-01-24 Thread Alban Hertroys
[EMAIL PROTECTED] wrote: CREATE OR REPLACE FUNCTION get_bar_avg() RETURNS TRIGGER AS ' DECLARE bar_record RECORD; x INTEGER; y DOUBLE PRECISION := 0; BEGIN IF TG_OP = ''INSERT'' THEN y := y + NEW.bar; ... RETURN NEW; ELSIF TG_OP = ''DELETE'' THEN x := 0; ...

Re: [GENERAL] Calculating a moving average

2005-01-21 Thread mstory
Unless I'm grossly misunderstanding the problem i think that a trigger written in PL/pgsql would work fine. Something like this: CREATE TABLE foo ( foo_id SERIAL primary key, foo TEXT); CREATE TABLE bar ( foo_id INTEGER references foo, bar_id SERIAL primary key, bar DOUBLE PRECISION NOT NULL);

Re: [GENERAL] Calculating a moving average

2005-01-21 Thread Greg Stark
"Dann Corbit" <[EMAIL PROTECTED]> writes: > If someone wanted to put arbitrary aggregates into PostgreSQL, I would > suggest something akin to the "RED BRICK" API, or better yet, the ATLAS > API: I also found a good reference for the DB2's SQL2003 Standard OLAP functions: http://publib.boulder.i

Re: [GENERAL] Calculating a moving average

2005-01-20 Thread Dann Corbit
If someone wanted to put arbitrary aggregates into PostgreSQL, I would suggest something akin to the "RED BRICK" API, or better yet, the ATLAS API: http://magna.cs.ucla.edu/atlas/ ---(end of broadcast)--- TIP 8: explain analyze is your friend

Re: [GENERAL] Calculating a moving average

2005-01-20 Thread Greg Stark
"Jim C. Nasby" <[EMAIL PROTECTED]> writes: > If you're feeling adventurous, you might look at Oracle's documentation > on their analytic functions and see if you can come up with something > generic for PostgreSQL. I think the hard part of doing even a simple implementation is precisely the poin

Re: [GENERAL] Calculating a moving average

2005-01-20 Thread Jim C. Nasby
On Fri, Jan 21, 2005 at 12:53:45AM -0500, Greg Stark wrote: > "Vanole, Mike" <[EMAIL PROTECTED]> writes: > > > I need to calculate a moving average and I would like to do it with SQL, > > or a Pg function built for this purpose. I'm on Pg 7.4. Is this possible > > in Pg without a bunch of self joi

Re: [GENERAL] Calculating a moving average

2005-01-20 Thread Greg Stark
"Vanole, Mike" <[EMAIL PROTECTED]> writes: > I need to calculate a moving average and I would like to do it with SQL, > or a Pg function built for this purpose. I'm on Pg 7.4. Is this possible > in Pg without a bunch of self joins, or is there a funtion available? Unfortunately moving averages f

Re: [GENERAL] Calculating a moving average

2005-01-20 Thread Jim C. Nasby
IMO-YMMV > > > > From: [EMAIL PROTECTED] > [mailto:[EMAIL PROTECTED] On Behalf Of Vanole, Mike > Sent: Wednesday, January 19, 2005 1:34 PM > To: pgsql-general@postgresql.org > Subject: [GENERAL] Calculating a moving average > >

Re: [GENERAL] Calculating a moving average

2005-01-20 Thread Dann Corbit
] [mailto:[EMAIL PROTECTED] On Behalf Of Vanole, Mike Sent: Wednesday, January 19, 2005 1:34 PM To: pgsql-general@postgresql.org Subject: [GENERAL] Calculating a moving average   Hi,   I need to calculate a moving average and I would like to do it with SQL, or a Pg function built for this

[GENERAL] Calculating a moving average

2005-01-20 Thread Vanole, Mike
Title: Message Hi,   I need to calculate a moving average and I would like to do it with SQL, or a Pg function built for this purpose. I'm on Pg 7.4. Is this possible in Pg without a bunch of self joins, or is there a funtion available?   Thanks, Mike