我在PostgreSQL(mixbest)中有一个73018行的表。我想要选择的字段是:
sample integer
m integer,
pciv double precision,
aggrec double precision
soil character(1)
我正在尝试SELECT但是我得到以下错误SQLstate:22003数字溢出。我的选择:
SELECT sample, m, 1-(EXP(SUM(LN(1-pciv)))) pciv, avg (aggrec) aggrec_avg, soil
FROM mixbest
GROUP BY sample, m, soil;
我知道问题是EXP(),因为我已尝试使用表达式(SUM(LN(1-pciv))))进行相同的选择,并且我没有得到相同的错误。
我尝试仅在几行中执行选择,并且它可以工作:
SELECT sample, m, 1-(EXP(SUM(LN(1-pciv)))) pciv, avg (aggrec) aggrec_avg, soil
FROM mixbest
WHERE sample< 4492 GROUP BY sample, m, soil;
你有什么建议吗?
答案 0 :(得分:1)
这样的事情,我想:
create or replace function mixbest_avg(out sample int, out m int, out pciv double precision, out aggrec_avg double precision, out soil character(1))
returns setof record as
$$
declare
rec record;
begin
for rec in
SELECT t.sample _sample, t.m _m, SUM(LN(1-t.pciv)) _pciv, avg(t.aggrec) _aggrec, t.soil _soil
FROM mixbest t
GROUP BY t.sample, t.m, t.soil
loop
begin
rec._pciv = 1 - exp(rec._pciv);
exception
when numeric_value_out_of_range then -- here we catch an exception
rec._pciv = 0; -- or other default value
end;
select rec._sample, rec._m, rec._pciv, rec._aggrec, rec._soil into sample, m, pciv, aggrec_avg, soil;
return next;
end loop;
end
$$ language plpgsql;