我试图在SQL SELECT语句的Postgres中将整数数组中的值截断为8个小数,然后取回清理后的数组。
[1.030411088488374,1.0985247734044379,1.0863039797613594]
答案 0 :(得分:0)
您需要取消嵌套该数组,截断每个元素,然后将这些元素聚合回一个数组:
select array_agg(trunc(n, 8) order by idx)
from unnest(array[1.030411088488374,1.0985247734044379,1.0863039797613594]::numeric[]) with ordinality as t(n,idx);
如果您需要记录日志,请考虑创建一个函数:
create function trunc_elements(p_numbers numeric[], p_num_digits int)
returns numeric[]
as
$$
select array_agg(trunc(n, p_num_digits) order by idx)
from unnest(p_numbers) with ordinality as t(n,idx);
$$
language sql;