APPROX_COUNT_DISTINCT is an aggregate function in SQL Server which returns the approximate number of distinct values. This is very efficient with large number of rows.
Syntax
APPROX_COUNT_DISTINCT (expression)
The return types of this function is bigint. And expression type can be of any type, except image, sql_variant, ntext, or text - as per Microsoft documentation.
Let's look into an example,
SELECT APPROX_COUNT_DISTINCT([ProductID]) AS Approx_Distinct_Product FROM [AdventureWorks2019].[Production].[WorkOrder];
The above statement returns the approximate number of different products from WorkOrder table.
This is very powerful with billions of rows without any high memory utilization or performance issues.
Comments