[1,2,3,4] + [2,3,4,5]
했을떄 [ 3,5,7,9] 가되도록 하는 함수나 방법이 있을까요?
없습니다.
직접 만들어야 합니다.
윗 경우라면,
CREATE OR REPLACE FUNCTION public.intarray_element_plus(leftarr integer[], rightarr integer[]) RETURNS integer[] LANGUAGE plpgsql IMMUTABLE AS $function$ declare retarr int4[]; maxlength int4; cnt int; begin retarr := '{}'::int4[]; maxlength := greatest(coalesce(array_upper(leftarr, 1),0), coalesce(array_upper(rightarr, 1),0)); cnt := 1; if maxlength < 1 then raise exception 'could not calculate'; end if; loop retarr := array_append(retarr, coalesce(leftarr[cnt], 0) + coalesce(rightarr[cnt], 0)); cnt := cnt + 1; exit when cnt > maxlength; end loop; return retarr; end; $function$;
-- 배열 크기가 4개 고정이라면. SELECT x , y , CONCAT( '{' , CONCAT_WS( ',' , x[1] + y[1] , x[2] + y[2] , x[3] + y[3] , x[4] + y[4] ), '}')::int[] z FROM (SELECT '{1,2,3,4}'::int[] x , '{2,3,4,5}'::int[] y ) a ;