Christian Oudard
Christian Oudard

Reputation: 49976

SQL to find the number of distinct values in a column

I can select all the distinct values in a column in the following ways:

But how do I get the row count from that query? Is a subquery required?

Upvotes: 447

Views: 812336

Answers (14)

Darryl
Darryl

Reputation: 11

You can do this.

Select distinct PRODUCT_NAME_X 
,count (Product_name) products_# 
from TableX
Group by PRODUCT_NAME

It will return

PRODUCT_NAME    products
XXXXXXXXXX      4760

Upvotes: 1

Deva44
Deva44

Reputation: 93

Without using DISTINCT this is how we could do it-

SELECT COUNT(C)
FROM (SELECT COUNT(column_name) as C
FROM table_name
GROUP BY column_name)

Upvotes: 0

Asclepius
Asclepius

Reputation: 63516

To do this in Presto using OVER:

SELECT DISTINCT my_col,
                count(*) OVER (PARTITION BY my_col
                               ORDER BY my_col) AS num_rows
FROM my_tbl

Using this OVER based approach is of course optional. In the above SQL, I found specifying DISTINCT and ORDER BY to be necessary.

Caution: As per the docs, using GROUP BY may be more efficient.

Upvotes: 2

Alper
Alper

Reputation: 51

After MS SQL Server 2012, you can use window function too.

SELECT column_name, COUNT(column_name) OVER (PARTITION BY column_name) 
FROM table_name
GROUP BY column_name

Upvotes: 1

Nilesh Shinde
Nilesh Shinde

Reputation: 469

Using following SQL we can get the distinct column value count in Oracle 11g.

select count(distinct(Column_Name)) from TableName

Upvotes: 1

Nitika Chopra
Nitika Chopra

Reputation: 1405

select count(distinct(column_name)) AS columndatacount from table_name where somecondition=true

You can use this query, to count different/distinct data.

Upvotes: 0

Paul James
Paul James

Reputation: 2509

This will give you BOTH the distinct column values and the count of each value. I usually find that I want to know both pieces of information.

SELECT [columnName], count([columnName]) AS CountOf
FROM [tableName]
GROUP BY [columnName]

Upvotes: 246

Wayne
Wayne

Reputation: 39928

select Count(distinct columnName) as columnNameCount from tableName 

Upvotes: 12

Vaibhav
Vaibhav

Reputation:

select count(*) from 
(
SELECT distinct column1,column2,column3,column4 FROM abcd
) T

This will give count of distinct group of columns.

Upvotes: 14

xchiltonx
xchiltonx

Reputation: 2031

An sql sum of column_name's unique values and sorted by the frequency:

SELECT column_name, COUNT(*) FROM table_name GROUP BY column_name ORDER BY 2 DESC;

Upvotes: 38

Paul Pena
Paul Pena

Reputation: 1

Count(distinct({fieldname})) is redundant

Simply Count({fieldname}) gives you all the distinct values in that table. It will not (as many presume) just give you the Count of the table [i.e. NOT the same as Count(*) from table]

Upvotes: -10

Noah Goodrich
Noah Goodrich

Reputation: 25263

You can use the DISTINCT keyword within the COUNT aggregate function:

SELECT COUNT(DISTINCT column_name) AS some_alias FROM table_name

This will count only the distinct values for that column.

Upvotes: 748

David Aldridge
David Aldridge

Reputation: 52386

Be aware that Count() ignores null values, so if you need to allow for null as its own distinct value you can do something tricky like:

select count(distinct my_col)
       + count(distinct Case when my_col is null then 1 else null end)
from my_table
/

Upvotes: 31

Pete Karl II
Pete Karl II

Reputation: 4300

SELECT COUNT(DISTINCT column_name) FROM table as column_name_count;

you've got to count that distinct col, then give it an alias.

Upvotes: 15

Related Questions