SQL Server:Sum函数返回错误的值

时间:2018-06-15 16:54:26

标签: sql-server join

所以这是我的查询

   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

这是更新后的查询,我知道它很长,但我遇到同样的问题,当我手动总结时总和与值不匹配

1 个答案:

答案 0 :(得分:0)

这很可能是您的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;