Erro de arredondamento do SQL Server, dando valores diferentes
Eu tenho um procedimento armazenado que faz um monte de cálculo, armazena os resultados em vários tabela temporária. Finalmente, calculando a soma e arredondamento para dois decimais e armazena em uma tabela temporária e seleciona isso.
Toda a tabela temporária intermediária e final tem o tipo de dados flutuante para a coluna de interesse.
Cenário original:
Declare @Intermediate table
{
--several other columns
Labor float
--several other columns
};
---Lots of calculation ---xx-----
Declare @Final table
{
--several other columns
LaborTotal float
--several other columns
};
INSERT INTO @Final SELECT ROUND(ISNULL((SELECT SUM([Labor]) FROM @Intermediate ),0),2) AS LaborTotal;
SELECT * FROM @Final;
Result: 7585.22 --> when rounded //Here is the error Expecting 7585.23
7585.225 --> when not rounded
Casos de teste :
DECLARE @test float = 7585.225;
SELECT ROUND(@test,2) AS Result; --> results 7585.23
SELECT ROUND(7585.225,2) AS Result --> results 7585.23
Inseriu valores individuais em uma tabela temporária e, em seguida, calculou a soma
DECLARE @TmpTable table
(
MaterialAmount float
,LaborAmount float
);
INSERT INTO @TmpTable VALUES (12.10,1218.75);
INSERT INTO @TmpTable VALUES (12.10,1090.125);
INSERT INTO @TmpTable VALUES (12.10,900);
INSERT INTO @TmpTable VALUES (12.10,1632.6);
INSERT INTO @TmpTable VALUES (12.10,1625);
INSERT INTO @TmpTable VALUES (12.10,1118.75);
SELECT ROUND(ISNULL((SELECT SUM(MaterialAmount) FROM @TmpTable), 0),2) AS MatSum,
ISNULL((SELECT SUM(LaborAmount) FROM @TmpTable), 0) AS LabSumUnrounded, --> 7585.225
ROUND(ISNULL((SELECT SUM(LaborAmount) FROM @TmpTable), 0),2) AS LabSum; --> 7585.23
SELECT ROUND(SUM(MaterialAmount),2),
ROUND(SUM(LaborAmount),2) ---> 7585.23
FROM @TmpTable;
Alguma idéia / sugestão porque eu estou recebendo 0,01 diferença no meu cenário original, ao obter valores exatos em todos os meus testcases? Desde já, obrigado.