Categories:

Data Metric Functions

UNIQUE_COUNT

Returns the total number of unique non-NULL values for the specified columns in a table.

Syntax

SNOWFLAKE.CORE.UNIQUE_COUNT(<query>)
Copy

Arguments

query

Specifies a SQL query on a table or view.

Allowed data types

The referenced columns in the query must have one of the following data types:

  • FLOAT

  • NUMBER

  • TIMESTAMP_LTZ

  • TIMESTAMP_NTZ

  • TIMESTAMP_TZ

  • VARCHAR

Returns

The function returns a scalar value with a NUMBER data type.

Access control requirements

To use a system DMF, choose one of the following access control approaches:

  • Grant the DATA_METRIC_USER database role to the table owner role, which is the role with the OWNERSHIP privilege on the table. This database role has the USAGE privilege on the SNOWFLAKE.CORE schema and the USAGE privilege on all system DMFs in the SNOWFLAKE.CORE schema.

    Additionally, grant the privileges in this table to the table owner role:

    Privilege

    Object

    Notes

    EXECUTE DATA METRIC FUNCTION

    Account

    This privilege enables you to control which roles have access to serverless compute resources to call the system DMF.

    USAGE

    Database, schema

    These objects are the database and schema that contain the referenced table in the query.

  • Grant the privileges in the table to the table owner role and grant these privileges to the table owner role:

  • Use the ACCOUNTADMIN role.

For instructions on creating a custom role with a specified set of privileges, see Creating custom roles.

For general information about roles and privilege grants for performing SQL actions on securable objects, see Overview of Access Control.

Usage notes

When you call a system DMF manually, you don’t need to specify whichever allowed data type you are using. You only need to specify the query for the column that you want to measure. Snowflake matches the allowed data type for the function with the data type for the column.

Example

Measure the number of unique non-NULL values for the SSN column (that is, US Social Security number):

SELECT SNOWFLAKE.CORE.UNIQUE_COUNT(
  SELECT
    ssn
  FROM hr.tables.empl_info
);
Copy
+------------------------------------------------------------------+
| SNOWFLAKE.CORE.UNIQUE_COUNT(SELECT ssn FROM hr.tables.empl_info) |
+------------------------------------------------------------------+
| 42                                                               |
+------------------------------------------------------------------+