The SQL CHECKSUM_AGG is one of the Aggregate Function, which is used to return the checksum of the values in a Group. Or we can say, CHECKSUM_AGG will detect the changes in a column. For this CHECKSUM_AGG demo, We are going to use the below-shown data
SQL CHECKSUM_AGG Example
The following example uses the CHECKSUM_AGG Function to detect the changes in Employee Id, Dept Id, and Manager Id columns.
SELECT CHECKSUM_AGG(CAST([EmployeeID] AS INT)) AS [Emp ID] ,CHECKSUM_AGG(CAST(DeptID AS INT)) AS [Department ID] ,CHECKSUM_AGG(CAST([ManagerID] AS INT)) AS [Manager ID] FROM [MyEmployees]
SQL CHECKSUM_AGG with Distinct Keyword
In this example, we are going to find the checksum value of the distinct records using CHECKSUM_AGG function. The DISTINCT keyword is used to remove the Duplicates from the specified column Name in SQL Server
SELECT CHECKSUM_AGG(CAST([EmployeeID] AS INT)) AS [Emp ID] ,CHECKSUM_AGG(DISTINCT CAST(DeptID AS INT)) AS [Unique Department ID] ,CHECKSUM_AGG(DISTINCT CAST([ManagerID] AS INT)) AS [Unique Manager ID] FROM [MyEmployees]
SQL CHECKSUM_AGG with Group By
This Aggregate Function example will show you, How to use the CHECKSUM_AGG function along with Group By Clause. Please refer GROUP BY Clause to understand the grouping techniques
SELECT Occupation ,CHECKSUM_AGG(CAST([EmployeeID] AS INT)) AS [Emp ID] ,CHECKSUM_AGG(DISTINCT CAST(DeptID AS INT)) AS [Unique Department ID] ,CHECKSUM_AGG(DISTINCT CAST([ManagerID] AS INT)) AS [Unique Manager ID] FROM [MyEmployees] GROUP BY Occupation