Prueba algo como esto:
SELECT id, unnest(array300) as val, ntile(100) OVER (PARTITION BY id) as bucket_num
FROM your_table
Este SELECT
le dará 300 registros por array300
con el mismo id
y asignándoles el bucket_num
(1 para los 3 primeros elementos, 2 para los 3 siguientes, y así sucesivamente).
Luego use esta selección para obtener el avg
de elementos en el cubo:
SELECT id, avg(val) as avg_val
FROM (...previous select here...)
GROUP BY id, bucket_num
Siguiente:simplemente agregue el avg_val
en matriz:
SELECT id, array_agg(avg_val) as array100
FROM (...previous select here...)
GROUP BY id
Detalles:unnest , ntile , array_agg , SOBRE (PARTICIÓN POR )
UPD:Prueba esta función:
CREATE OR REPLACE FUNCTION public.array300_to_100 (
p_array300 numeric []
)
RETURNS numeric [] AS
$body$
DECLARE
dim_start int = array_length(p_array300, 1); --size of input array
dim_end int = 100; -- size of output array
dim_step int = dim_start / dim_end; --avg batch size
tmp_sum NUMERIC; --sum of the batch
result_array NUMERIC[100]; -- resulting array
BEGIN
FOR i IN 1..dim_end LOOP --from 1 to 100.
tmp_sum = 0;
FOR j IN (1+(i-1)*dim_step)..i*dim_step LOOP --from 1 to 3, 4 to 6, ...
tmp_sum = tmp_sum + p_array300[j];
END LOOP;
result_array[i] = tmp_sum / dim_step;
END LOOP;
RETURN result_array;
END;
$body$
LANGUAGE 'plpgsql'
IMMUTABLE
RETURNS NULL ON NULL INPUT;
Se necesita un array300
y genera un array100
. Para usarlo:
SELECT id, array300_to_100(array300)
FROM table1;
Si tiene algún problema para entenderlo, solo pregúnteme.