Átomo
41Cd
Agregação · SQL

COUNT DISTINCT

Abrir na tabela

Definição

Conta valores únicos. Em tabelas muito grandes existe a versão aproximada, bem mais barata.

Aproximado: APPROX_COUNT_DISTINCT (BigQuery, Snowflake, Databricks, Oracle, SQL Server).

Exemplos

1. Clientes únicos
SELECT COUNT(DISTINCT customer_id)
FROM sales;
2. Por período
SELECT month, COUNT(DISTINCT customer_id) AS customers
FROM sales
GROUP BY month;
3. Aproximado em grande volume
SELECT APPROX_COUNT_DISTINCT(user_id)
FROM events;

No mesmo grupo