SQL 错误 2616 在执行 count(*) 时计算期间的数字溢出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21468115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 00:56:34  来源:igfitidea点击:

ERROR 2616 Numeric overflow during computation while doing count(*)

sqlteradata

提问by user3055262

I am trying to do a select count(*)from table from a table and I am not able to do it because of this error. I am not aware of the number of rows in the table.

我正在尝试select count(*)从表中执行来自表的操作,但由于此错误而无法执行此操作。我不知道表中的行数。

I am not doing any other aggregation in my query apart from this.

除此之外,我没有在我的查询中进行任何其他聚合。

I guess it has something to do with the count value which is too large to be stored in INTEGER.

我想这与太大而无法存储在整数中的计数值有关。

What is the alternative?

什么是替代方案?

回答by dnoeth

When your session runs in Teradata mode the result of a COUNT is INTEGER as you already noticed (in ANSI mode it will be a DECIMAL with at least 15 digits).

当您的会话在 Teradata 模式下运行时,正如您已经注意到的那样,COUNT 的结果是 INTEGER(在 ANSI 模式下,它将是至少 15 位数字的 DECIMAL)。

The workaround is simple, cast it to a bigint:

解决方法很简单,将其转换为 bigint:

SELECT CAST(COUNT(*) AS BIGINT)...

回答by mojave

This is the #2 Google hit for Teradata 2616, so I want to add something. If you're getting 2616 "Numeric overflow occured" from a SUM in Teradata, the solution is to CAST, then SUM. The CAST has to be inside the SUM:

这是 Teradata 2616 的谷歌排名第二,所以我想添加一些东西。如果您从 Teradata 中的 SUM 获得 2616“发生数字溢出”,则解决方案是先 CAST,然后是 SUM。CAST 必须在 SUM 内:

SELECT SUM(CAST(WHATEVER_QTY AS DECIMAL(38,0))) FROM TER_DATABASE.WHATEVER_TABLE ;

SELECT SUM(CAST(WHATEVER_QTY AS DECIMAL(38,0))) FROM TER_DATABASE.WHATEVER_TABLE ;

In my case, DECIMAL(38,0) worked, but BIGINT was 2616. You are welcome to experiment. Here's the link at info.teradata:

就我而言, DECIMAL(38,0) 有效,但 BIGINT 为 2616。欢迎您进行实验。这是 info.teradata 上的链接:

http://www.info.teradata.com/HTMLPubs/DB_TTU_14_00/index.html#page/SQL_Reference/B035_1145_111A/Data_Type_Conversions.098.297.html

http://www.info.teradata.com/HTMLPubs/DB_TTU_14_00/index.html#page/SQL_Reference/B035_1145_111A/Data_Type_Conversions.098.297.html