It seems that there are a number of conditions that cause CHECKSUM_AGG
to return 0 that I wouldn't have expected. I've only been able to find one discussed, which is that duplicate values will cause it. That can be solved via a DISTINCT
or GROUP BY
.
I've also found a couple more scenarios that make less sense to me. One was provided by my boss and one I found MSDN. These I don't know how to explain. Here is some SQL that demonstrates the scenarios:
SELECT
CHECKSUM_AGG(T.Number) AS ChecksumAgggregate
FROM
(
VALUES
(2)
, (3)
, (4)
, (5)
)AS T(Number)
DECLARE @t TABLE
(
Category VARCHAR(15),
Value VARCHAR(10)
)
INSERT @t
(
Category
, Value
)
VALUES
('OneCharacter','a')
,('OneCharacter','b')
,('OneCharacter','c')
,('OneCharacter','d')
,('TwoCharacters','aa')
,('TwoCharacters','bb')
,('TwoCharacters','cc')
,('TwoCharacters','dd')
,('ThreeCharacters','aaa')
,('ThreeCharacters','bbb')
,('ThreeCharacters','ccc')
,('ThreeCharacters','ddd')
,('SixCharacters','aaaaaa')
,('SixCharacters','bbbbbb')
,('SixCharacters','cccccc')
,('SixCharacters','dddddd')
,('AllValues','a')
,('AllValues','b')
,('AllValues','c')
,('AllValues','d')
,('AllValues','aa')
,('AllValues','bb')
,('AllValues','cc')
,('AllValues','dd')
,('AllValues','aaa')
,('AllValues','bbb')
,('AllValues','ccc')
,('AllValues','ddd')
,('AllValues','aaaaaa')
,('AllValues','bbbbbb')
,('AllValues','cccccc')
,('AllValues','dddddd')
select
Category, CHECKSUM_AGG(CHECKSUM(Value))
from @t
group by Category
select Category, Value, CHECKSUM(Value) ValueChecksum
from @t
It's nothing but 0's in these examples from CHECKSUM_AGG
from these queries. The last query shows that none of the CHECKSUM
values that are getting input into the CHECKSUM_AGG
call are duplicated.
I'm hoping that whatever answer describes what causes CHECKSUM_AGG
to return 0 will explain these situations as well.