SQL引发算术溢出错误

时间:2018-10-09 17:27:22

标签: sql-server error-handling overflow aggregate-functions

运行简单的聚合函数时,出现“将表达式转换为数据类型为int的算术溢出错误”错误(请参见下面的代码)。当我调低日期范围时,我可以看到结果实际上超出了INT数据类型的限制...我试图将整个表达式转换为BIGINT而没有成功。有人知道我该如何解决这个问题?

所有这3个查询都引发相同的上述错误:

Select  (SUM(ACDTalkTimeInSec) + SUM(TotAcwTimeInSec) + SUM(HoldTimeInSec)) as AHT
From ColTelephony.dbo.vwACDSkillCombined acd
Where WorkDte between '1/2/2018' AND '10/5/2018'

Select  CAST(Cast(SUM(ACDTalkTimeInSec) AS BIGINT) + CAST(SUM(TotAcwTimeInSec) AS BIGINT) + CAST(SUM(HoldTimeInSec) AS BIGINT) AS BIGINT) as AHT
From ColTelephony.dbo.vwACDSkillCombined acd
Where WorkDte between '1/2/2018' AND '10/5/2018'

Select  Cast(((SUM(ACDTalkTimeInSec) + SUM(TotAcwTimeInSec) + SUM(HoldTimeInSec))) AS BIGINT) as AHT
From ColTelephony.dbo.vwACDSkillCombined acd
Where WorkDte between '1/2/2018' AND '10/5/2018'

1 个答案:

答案 0 :(得分:2)

SMor在评论中回答了它,但这是sql:

Select SUM(CAST(ACDTalkTimeInSec AS BIGINT)) + SUM(CAST(TotAcwTimeInSec AS BIGINT)) 
    + SUM(CAST(HoldTimeInSec AS BIGINT)) as AHT
From ColTelephony.dbo.vwACDSkillCombined acd
Where WorkDte between '1/2/2018' AND '10/5/2018'

如果SMor发布了他们的答案,请接受。我没有测试代码,所以请原谅任何错字。