SQL CHECKSUM_AGG Function

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 Function 1

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.

-- SQL Server CHECKSUM_AGG in SQL Server
USE [SQL Tutorial]
GO
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 Function 2

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 SQL DISTINCT Keyword is used to remove the Duplicates from the specified column Name in SQL Server

-- CHECKSUM_AGG in SQL Server
USE [SQL Tutorial]
GO
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 Function 3

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 SQL GROUP BY Clause to understand the grouping techniques

-- CHECKSUM_AGG in SQL Server
USE [SQL Tutorial]
GO
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
SQL CHECKSUM_AGG Function 4