CREATE OR REPLACE FUNCTION public.suma_porcentajes(jsonb) RETURNS numeric LANGUAGE plpgsql IMMUTABLE AS $$ declare total numeric; begin select coalesce(sum((elem->>'porcentaje')::numeric),0) into total from jsonb_array_elements($1) elem; return total; end; $$;