sql-server - SQL Server:Sum 函数返回错误值
问题描述
所以这是我的查询
select a.ERSDataValues_ERSCommodity_ID,c.ersGeographyDimension_country,
b.ERSTimeDimension_Year,
sum(a.ERSDataValues_AttributeValue) as Total
from cosd.ERSDataValues a ,cosd.ERSTimeDimension_LU
b,cosd.ERSGeographyDimension_LU c
where a.ERSDataValues_ERSCommodity_ID in (SELECT
ERSBusinessLogic_InputDataSeries
FROM [AnimalProductsCoSD].[CoSD].[ERSBusinessLogic]
where ERSBusinessLogic_InputGeographyDimensionID = 7493
and ERSBusinessLogic_InputTimeDimensionValue = 'all months'
and ERSBusinessLogic_Type = 'time aggregate')
and a.ERSDataValues_ERSTimeDimension_ID = b.ERSTimeDimension_ID
and c.ersGeographyDimension_country != 'WORLD'
and a.ERSDataValues_ERSGeography_ID=c.ERSGeographyDimension_ID
group by b.ERSTimeDimension_Year,a.ERSDataValues_ERSCommodity_ID,
c.ersGeographyDimension_country
order by b.ERSTimeDimension_Year,a.ERSDataValues_ERSCommodity_ID
这是更新后的查询,我知道它很长,但我面临同样的问题,当我手动求和时总和与值不匹配
解决方案
这很可能是您的 JOIN 的问题(您在技术上并未使用)。尝试删除 SUM 和 GROUP 函数,看看您是否得到重复的行(如果这是一个大型数据集,可能会限制您的时间范围以获取更少的行):
SELECT b.TimeDimension_Year ,
c.GeographyDimension_country ,
a.DataValues_AttributeValue
FROM cosd.DataValues a ,
cosd.TimeDimension_LU b ,
cosd.GeographyDimension_LU c
WHERE a.DataValues_Commodity_ID = 2257
AND a.DataValues_TimeDimension_ID = b.TimeDimension_ID
AND c.GeographyDimension_ID = 7493
AND b.TimeDimension_Year = 2015;
推荐阅读
- cron - 从 cPanel Cronjob 运行 PHP 脚本
- apache-flink - 如何在 Apache Flink 中合并两个 DataStream
- ios - 如何使用第一个视图控制器中的按钮更改第二个视图控制器中的图像
- android - Android Koin DI - ViewModel 注入
- json - 使用脚本将 JSON 文件导入 SQL 表
- python - 将 Keras 用于带有自定义 Y 参数的 sklearn AdaBoost
- c# - 使用 Automapper 和 Autofac 时为不可访问的实例指定构造函数
- amazon-web-services - 在 aws cli ssm 中的 powershell 命令中传递单引号字符串
- javascript - 修补现有的 js 函数但仍然显示
- html - 与 mat-date-range-input 一起使用时,即使设置了错误,mat-error 也不会显示