Why does CHECKSUM_AGG () return the same value for completely different input values?

I really don't understand how it works CHECKSUM_AGG(), although I realized that it was somehow built using XOR. This explains why it returns 0when you pass equal integers.

But why am I getting the same aggregate checksum in the following SQL where the input values ​​are unique?

DECLARE @test1 TABLE (chksum INT)
INSERT INTO @test1 VALUES (2147473855), (2147473343)
SELECT CHECKSUM_AGG(chksum)
FROM @test1

DECLARE @test2 TABLE (chksum INT)
INSERT INTO @test2 VALUES (2147474831), (2147472271)
SELECT CHECKSUM_AGG(chksum)
FROM @test2

An explanation would be very helpful. Thank!

+4
source share
1 answer

Known issues with SQL Server implementations CHECKSUM and CHECKSUM_AGG: CHECKSUM failure

Use HASHBYTES instead: Using HASHBYTES to compare columns

Microsoft: , . , . CHECKSUM , , . HashBytes. MD5, , HashBytes , , CHECKSUM.

HASHBYTES - .

, HASBYTES:

DECLARE @test1 TABLE (chksum INT) 
DECLARE @test2 TABLE (chksum INT)

INSERT INTO @test1 VALUES (50), (3), (26)
INSERT INTO @test2 VALUES (45), (0), (6)

SELECT [Values]    = '50, 3, 26', 
       [Checksum]  = CHECKSUM_AGG(chksum),
       -- HashBytes is limited to 8000 bytes only
       [Hashbytes] = HashBytes('md5',convert(varbinary(max),(SELECT * FROM @test1 FOR XML AUTO)))
FROM @test1

UNION ALL
SELECT  [Values]    = '45, 0, 6',      
        [Checksum]  = CHECKSUM_AGG(chksum),
        -- HashBytes is limited to 8000 bytes only
        [Hashbytes] = HashBytes('md5',convert(varbinary(max),(SELECT * FROM @test2 FOR XML AUTO)))
FROM @test2

CHECKSUM_AGG vs HASHBYTES

+3

All Articles