下面是我的表架构(我正在使用PostgreSQL 9.6.1)
my_table
------------
table_id SERIAL PRIMRY KEY,
A1 INTEGER,
A2 INTEGER,
A3 INTEGER,
A4 INTEGER,
A5 INTEGER,
A6 INTEGER,
A7 INTEGER,
A8 INTEGER,
A9 INTEGER,
A10 INTEGER,
A11 INTEGER,
A12 INTEGER,
A13 INTEGER,
A14 INTEGER,
A15 INTEGER,
A16 DOUBLE PRECISION,
A17 DOUBLE PRECISION
我试图在查询下面运行以创建一个新表。
CREATE TABLE my_table_2 AS
SELECT
B1 AS C1
B3 AS C2,
B5 AS C3,
B7 AS C4 ,
B9 AS C5,
B11 AS C6 ,
B1 / CAST( NULLIF(B3, 0) AS FLOAT) AS C7,
B1 / CAST( NULLIF(B5, 0) AS FLOAT) AS C8,
B1 / CAST( NULLIF(B7, 0) AS FLOAT) AS C9,
B1 / CAST( NULLIF(B9, 0) AS FLOAT) AS C10,
B1 / CAST( NULLIF(B11, 0) AS FLOAT) AS C11,
B3 / CAST( NULLIF(B5, 0) AS FLOAT) AS C12,
B5 / CAST( NULLIF(B7, 0 )* NULLIF(B9,0) AS FLOAT) AS C13,
B2 AS C14 ,
B4 AS C15,
B6 AS C16,
B8 AS C17,
B10 AS C18,
B12 AS C19,
B2 / CAST(NULLIF(B4, 0) AS FLOAT) AS C20,
B2 / CAST(NULLIF(B6, 0) AS FLOAT) AS C21,
B2 / CAST(NULLIF(B8, 0) AS FLOAT) AS C22,
B2 / CAST(NULLIF(B10, 0) AS FLOAT) AS C23,
B2 / CAST(NULLIF(B12 , 0) AS FLOAT) AS C24
B4 / CAST(NULLIF(B6 , 0) AS FLOAT) AS C25
B6 / CAST(NULLIF(B8,0) * NULLIF( B10,0) AS FLOAT) AS C26
0.0 AS C27,
0.0 AS C28,
0.0 AS C29,
0.0 AS C30,
0.0 AS C31,
0.0 AS C32,
0.0 AS C33,
0.0 AS C34,
0.0 AS C35,
0.0 AS C36,
0.0 AS C37,
0.0 AS C38,
0.0 AS C39,
B13 AS C40
B14 AS C41,
B15 AS C42
FROM (
SELECT
table_id,
A1 - A2 + A3 AS B1,
A1 AS B2,
A4 - A5 AS B3,
A4 AS B4,
A6 - A7 AS B5,
A6 AS B6,
A8 - A9 AS B7,
A8 AS B8,
A10 - A11 AS B9,
A10 AS B10,
A12 - A13 AS B11,
A12 AS B12,
A14 AS B13,
A15 AS B14,
coalesce(coalesce(A16 * 100, 0) / NULLIF(A17, 0), 0) AS B15
FROM my_table
) v1;
我使用psql
命令将上述查询作为后台进程运行
使用以下命令
sudo -u postgres psql My_DB -X -a -f /tmp/test.sql > result.out 2>result.err &
因为my_table是一张非常庞大的桌子。但是有一段时间我检查result.err文件。它只有一行说
错误:整数超出范围
对于WHere子句表中的一些示例id生成没有任何问题,但是当我为整个表运行它时,错误就会出现。
任何人都可以解释为什么会发生这种情况以及如何解决这个问题?
答案 0 :(得分:0)
任何+
或-
操作都可能导致此错误,但最可能的原因是A16 * 100
。
您可以通过避免导致溢出的操作来解决问题,也可以选择具有更宽范围值的numeric
或bigint
数据类型。
答案 1 :(得分:0)
检查乘法
NULLIF(B7, 0 )* NULLIF(B9,0)
和
NULLIF(B8,0) * NULLIF( B10,0)
尝试替换计算顺序,第一个除法,然后乘法
( B5 / NULLIF(B7, 0) ) * NULLIF(B9,0) AS B15
( B6 / NULLIF(B8, 0) ) * NULLIF(B10,0) AS C26