Note
This function is available starting from V4.6.0.
Syntax
UNIQ(expr)
Purpose
This function is used to approximately calculate the number of distinct values in a column.
Notice
This is a ClickHouse-compatible function. By default, it is not enabled. To use it, you must set the sql_func_extension_mode parameter to enable it. For more information, see sql_func_extension_mode.
Examples
Enable the ClickHouse extension function mode.
obclient> ALTER SYSTEM SET sql_func_extension_mode = "ClickHouse";Create a test table
test_tbl1.obclient> CREATE TABLE test_tbl1 (col1 INT PRIMARY KEY, col2 VARCHAR(50), col3 INT);Insert data into the table
test_tbl1.obclient> INSERT INTO test_tbl1 VALUES(1, 'a1', 6), (2, 'a2', 8), (3, 'a2', 8);Use the
UNIQfunction to calculate the number of distinct values in thecol3column.obclient> SELECT UNIQ(col3) FROM test_tbl1;The returned result is as follows:
+------------+ | UNIQ(col3) | +------------+ | 2 | +------------+ 1 row in set
