Error de redondeo de SQL Server, dando diferentes valores
Tengo un procedimiento almacenado que realiza muchos cálculos y almacena los resultados en varias tablas temporales. Finalmente, calcule la suma y redondee a dos decimales y almacene en una tabla temporal y seleccione eso.
Toda la tabla temporal intermedia y final tiene un tipo de datos flotante para la columna en cuestión.
Escenario 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 prueba :
DECLARE @test float = 7585.225;
SELECT ROUND(@test,2) AS Result; --> results 7585.23
SELECT ROUND(7585.225,2) AS Result --> results 7585.23
Insertó valores individuales en una tabla temporal y luego calculó la suma
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;
¿Alguna idea / sugerencia de por qué estoy obteniendo una diferencia de 0.01 en mi escenario original, mientras obtengo valores exactos en todos mis testcases? Gracias por adelantado.