You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

合并新冠死亡与接种表计算接种总和时触发int类型算术溢出错误求助

错误分析:Msg 8115 算术溢出问题

一、错误信息的具体含义

Msg 8115, Level 16, State 2, Line 77
Arithmetic overflow error converting expression to data type int.
Warning: Null value is eliminated by an aggregate or other SET operation.

  • 主错误(Msg 8115):执行SUM聚合计算时,将表达式转换为int类型的过程中发生了算术溢出——简单说就是计算出的累计值超出了int类型能存储的最大范围。
  • 警告信息:在聚合操作中,new_vaccinations字段的NULL值被自动排除,这不会导致主错误,但提示你需要注意数据中存在缺失值。

二、问题原因

  • int类型范围限制:SQL Server里int类型的取值范围是**-2,147,483,648 到 2,147,483,647**。当按location分区汇总接种数时,部分地区的累计总数超过了这个上限,直接触发溢出错误。
  • 强制转换的风险:你通过cast(vac.new_vaccinations as int)将字段转为int,如果new_vaccinations本身存储的数值已经接近int上限,或者长时间累加后的总和突破阈值,就会引发错误。
  • 数据量级问题:新冠接种数据中,部分国家/地区的累计接种数本身就远大于int的最大值(比如全球很多国家累计接种数都超过20亿),用int存储或计算必然溢出。

临时修复方案

将转换类型改为更大范围的bigint(取值范围:-9,223,372,036,854,775,808 到 9,223,372,036,854,775,807),修改后的SQL如下:

SELECT dea.continent, dea.location, dea.date, dea.population, vac.new_vaccinations,
SUM(CAST(vac.new_vaccinations AS bigint)) OVER (PARTITION BY dea.location) AS RollingPeopleVaccinated
FROM Portfolio_Covid_case..CovidDeaths dea
JOIN Portfolio_Covid_case..CovidVaccinations vac
  ON dea.location = vac.location
  AND dea.date = vac.date 
WHERE dea.continent IS NOT NULL 
ORDER BY 2,3

内容的提问来源于stack exchange,提问作者Brenda_love

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 00:40:41