I am rather new to PostgreSQL and I am really stuck with an apparently simple task. I have a table with music albums and a table with bands features, including how many albums such band released in a specific time, let say between 1990 and 1995. So I need to create a trigger that keeps the bands tabe updated whenever I insert, delete or updaste the albums table. These are the tables:
Album
id_album id_band year
1 1 1995
2 1 1985
3 2 1993
4 3 1998
Band
id_band num_albums9095
1 1
2 1
3 0
So I have created the following function and trigger:
CREATE FUNCTION update_num_nineties()
RETURNS trigger AS $$ BEGIN
UPDATE band
SET num_albums_eighties = (SELECT COUNT (*) FROM album
where album.year between 1990 and 1995
GROUP BY album.id_band);
END;
$$LANGUAGE plpgsql;
CREATE TRIGGER update_nineties_album_mod_album AFTER UPDATE ON album FOR EACH row EXECUTE PROCEDURE update_num_nineties();
But I would get a subquery used as an expression returned more than one row message anytime I try to update any value to test it. Would anyone be so kind to help me see why I am goind in the wrong direction?