I'm creating a function and I need to get the largest and smallest results from an average score calculation. So far it looks like this:
CREATE OR REPLACE FUNCTION public.my_func()
RETURNS NUMERIC
LANGUAGE plpgsql
AS $function$
DECLARE
all_user_average_scores record;
max_avg numeric;
min_avg numeric;
BEGIN
SELECT user_uuid, AVG(score) as avg_score
INTO all_user_average_scores
FROM user_scores
GROUP BY user_uuid;
SELECT max(avg_score) INTO max_raw FROM all_user_average_scores;
SELECT min(avg_score) INTO min_raw FROM all_user_average_scores;
-- Do some more stuff here
RETURN final_result;
END;
$function$;
But I'm getting this error:
ERROR: relation "all_user_average_scores" does not exist
Any ideas? I've tried the :=
syntax as well and I get the same result