I have a table that has 2 integer columns and 2 integer array columns that keep scores in them. For example, my row would look like the follow:
{
physical_ed: 40,
music: 90,
first_term: {10,23,43},
second_term: {1,5,5,7}
}
The array fields are declared as an integer[].
I need to generate a score column that sums up all of these fields. So far I have tried:
ALTER TABLE scores DROP IF EXISTS score;
ALTER TABLE scores add COLUMN total_score integer GENERATED ALWAYS AS (physical_ed::integer +
first_term[3]::integer + second_term[1]::integer + second_term[2]::integer + second_term[3]::integer)
STORED;
The problem I have with the above, is it does not account for varying values in the array but sometimes that field could have 5 different values instead of just 3.
I have tried running a select statement and I can calculate the sum of each array in a select statement normally like so:
SELECT *, (SELECT SUM(s) FROM UNNEST(first_term) s) as total_first_term from scores;
Unfortunately, this does not work inside a generated column query and we do need it to be part of our generated total_score sum.