Skip to content

Distinct Count ​

Count the number of unique values in a field using field.count.

How It Works ​

TPL has two forms of count:

SyntaxMeaningSQL Equivalent
n or countRow countCOUNT(*)
field.countDistinct count of field valuesCOUNT(DISTINCT field)

When you bind count to a field (e.g., occupation.count), it counts the number of unique values of that field within each group. Standalone count or n counts the total number of rows.

Interactive Example ​

Try It
TABLE ROWS education * (n | occupation.count);
Dataset:samplesoccupation, education, gender, income
Try Variations

Query Breakdown ​

  • n - Row count: how many rows exist for each education level
  • occupation.count - Distinct count: how many unique occupations appear in each education level
  • The row count will always be >= the distinct count

This is useful for understanding data cardinality, e.g.:

  • How many unique users per region
  • How many distinct products per category
  • How many unique event types per session

Ordering by Distinct Count ​

You can order and limit rows by distinct count, just like any other aggregate:

sql
-- Top 5 education levels by number of distinct occupations
TABLE ROWS education[-5@occupation.count] * occupation.count;

-- All education levels ordered by distinct occupation count (descending)
TABLE ROWS education DESC@occupation.count * occupation.count;

Distinct Count with Percentages ​

Combine distinct count with ACROSS to see each group's share of unique values:

sql
TABLE ROWS education * (occupation.count ACROSS);

Powered by Malloy and DuckDB