SQL SERVER 2014 SUM函数不返回正确的值

时间:2016-10-26 11:03:47

标签: sql sql-server tsql

我的数据如下,当我总计Wage_Value时,它应该给我0值

enter image description here

但是,SQL SERVER SUM函数没有返回正确的值(请参阅下面的结果)。

Wage_Value的数据类型为十进制(18,2)

select [Employee Code], Wage_Type, sum(Wage_Value) 'Total_Wage_Value'
from [SSO_PAYROLL_20151231]
where [Employee Code] = '3870299'
group by [Employee Code], Wage_Type

enter image description here

1 个答案:

答案 0 :(得分:8)

我不相信你。我认为Wage_Value的类型是浮点数,而不是小数。 SQL Server非常清楚sum()的输出类型。对于十进制输入,输出为十进制。请参阅documentation

另外,因为这些值有时会输出一个小数位,有时输出为2(“852.9”和“-317.07”),我认为该表示是一个浮点数。

这很容易检查。做一个明确的转换:

select [Employee Code], Wage_Type,
       sum(cast(Wage_Value as decimal(18, 2)) as Total_Wage_Value
from [SSO_PAYROLL_20151231]
where [Employee Code] = '3870299'
group by [Employee Code], Wage_Type