Avg of float inconsistency

后端 未结 1 434
故里飘歌
故里飘歌 2020-12-02 01:31

The select returns right at 23,000 rows
The except will return between 60 to 200 rows (and not the same rows)
The except should return 0 as it is select a except sel

相关标签:
1条回答
  • 2020-12-02 02:07

    This is very similiar to: SELECT SUM(...) is non-deterministic when adding the column-values of datatype float.

    The problem is that with inaccurate datatype (FLOAT/REAL) the order of of arithmetic operations on floating point matters. Demo from connect:

    DECLARE @fl FLOAT = 100000000000000000000
    DECLARE @i SMALLINT = 0
    WHILE (@i < 100)
    BEGIN
        SET @fl = @fl + CONVERT(float, 5000)
        SET @i = @i + 1
    END
    SET @fl = @fl - 100000000000000000000
    SELECT CONVERT(NVARCHAR(40), @fl, 2)
    -- 0.000000000000000e+000
    
    
    DECLARE @fl FLOAT = 0
    DECLARE @i SMALLINT = 0
    WHILE (@i < 100)
    BEGIN
        SET @fl = @fl + CONVERT(float, 5000)
        SET @i = @i + 1
    END
    SET @fl = @fl + 100000000000000000000
    SET @fl = @fl - 100000000000000000000
    SELECT @fl
    -- 507904
    

    LiveDemo

    Possible solutions:

    • CAST all arguments to accurate datatype like DECIMAL/NUMERIC
    • alter table and change FLOAT to DECIMAL
    • you can try to force query optimizer to calculate the sum with the same order.

    The good news is that when a stable query result matters to your application, you can force the order to be the same by preventing parallelism with OPTION (MAXDOP 1).


    It looks like intial link is dead. WebArchive

    0 讨论(0)
提交回复
热议问题