-- Function update_student_gpa_o n_create_update_trip -- DROP FUNCTION

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
-- Function: update_student_gpa_on_create_update_trip()
-- DROP FUNCTION update_student_gpa_on_create_update_trip();
CREATE OR REPLACE FUNCTION update_student_gpa_on_create_update_trip()
RETURNS trigger AS
$BODY$
BEGIN
UPDATE "Students"
SET gpa = (SELECT AVG(rating) as rating_sum FROM "Trips" WHERE student_id = NEW.student_id)
WHERE "id"=NEW.student_id;
RETURN NEW;
END;
$BODY$
LANGUAGE plpgsql VOLATILE STRICT
COST 100;
ALTER FUNCTION update_student_gpa_on_create_update_trip()
OWNER TO postgres;