Skip to main content

COUNT_DISTINCT

Aggregate function.

The count(distinct ...) function calculates the uniq value of a set of values.

To obtain an estimated result from large data sets with little memory and time, consider using APPROX_COUNT_DISTINCT.

caution

NULL values are not counted.

Syntax

COUNT(distinct arguments ...)
UNIQ(arguments)

Arguments

ArgumentsDescription
expressionAny expression, size of the arguments is [1, 32]

Return Type

UInt64

Examples

SELECT count(distinct number % 3) FROM numbers(1000);
+------------------------------+
| count(distinct (number % 3)) |
+------------------------------+
| 3 |
+------------------------------+

SELECT uniq(number % 3, number) FROM numbers(1000);
+----------------------------+
| uniq((number % 3), number) |
+----------------------------+
| 1000 |
+----------------------------+


SELECT uniq(number % 3, number) = count(distinct number %3, number) FROM numbers(1000);
+---------------------------------------------------------------------+
| (uniq((number % 3), number) = count(distinct (number % 3), number)) |
+---------------------------------------------------------------------+
| true |
+---------------------------------------------------------------------+