I'm trying to create a Trigger that adds the average of one column from table A into table B after each insert into table A.
Table A (id (integer), rating (integer))
Table B (id (serial), average (integer))
My Trigger is:
CREATE TRIGGER rat_trig
AFTER INSERT ON a
FOR EACH ROW EXECUTE PROCEDURE upd_rat_func();
My Trigger Function looks like this:
CREATE OR REPLACE FUNCTION upd_rat_func() RETURNS TRIGGER as $rating_update$
BEGIN
INSERT INTO b VALUES (DEFAULT, AVG(rating) FROM a);
RETURN NEW;
END;
$rating_update$ language plpgsql;
Postgres tells me there is an error in my function somewhere near or at 'from', yet I cannot find a way to make it work (The Trigger is not yet created, as I cannot create the function). Any help (or also any advice of where I can find more about the structure of trigger functions as the postgres documentation has been less than helpful) are very welcome.