SQL to find the number of distinct values in a column
Asked Answered
D

14

440

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

  • SELECT DISTINCT column_name FROM table_name;
  • SELECT column_name FROM table_name GROUP BY column_name;

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

Dunnock answered 26/9, 2008 at 19:52 Comment(1)
E
742

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.

Errolerroll answered 26/9, 2008 at 19:54 Comment(4)
Neat, i didn't know you could put the distinct keyword there.Dunnock
also works on groups select A,COUNT(DISTINCT B) from table group by AFennec
can you expand this example to distinct on multiple columns?Greenock
How do you store this some_alias in a new column because I want to use where condition on this some_alias but unable without creating a new column. when I try to add it in a new column it says Error: aggregate functions are not allowed in UPDATEHartebeest
H
241

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]
Hamby answered 27/10, 2012 at 15:45 Comment(0)
A
38

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;
Affectional answered 20/5, 2015 at 0:4 Comment(0)
P
31

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
/
Perrone answered 26/9, 2008 at 21:32 Comment(4)
I really think your case statement was meant to say: case when my_col is null then 1 else my_col endExpiration
For clarity: SELECT my_col, COUNT(my_col) + COUNT(CASE WHEN my_col IS NULL THEN 1 ELSE NULL END) as CountOf from my_Table GROUP BY my_colPremium
count(*) includes nullsGalata
@Galata I guess so, but there's not such a thing as a null row, only a null value, and count(*) is specifically a count of rows.Perrone
C
15
SELECT COUNT(DISTINCT column_name) FROM table as column_name_count;

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

Couldst answered 26/9, 2008 at 19:55 Comment(0)
D
14
select count(*) from 
(
SELECT distinct column1,column2,column3,column4 FROM abcd
) T

This will give count of distinct group of columns.

Dewitt answered 4/11, 2008 at 9:37 Comment(0)
B
12
select Count(distinct columnName) as columnNameCount from tableName 
Badajoz answered 26/9, 2008 at 19:54 Comment(0)
F
2

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.

Farman answered 3/6, 2021 at 20:38 Comment(0)
F
1

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

select count(distinct(Column_Name)) from TableName
Fabled answered 26/12, 2018 at 2:7 Comment(0)
P
1

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
Paraffin answered 19/2, 2020 at 5:57 Comment(0)
F
1

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
Flagman answered 10/4, 2023 at 19:8 Comment(0)
S
0
select count(distinct(column_name)) AS columndatacount from table_name where somecondition=true

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

Sliest answered 5/6, 2019 at 10:51 Comment(0)
D
0

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)
Dibrin answered 13/7, 2022 at 13:16 Comment(0)
D
-10

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]

Dorrie answered 18/12, 2014 at 2:13 Comment(1)
No, this is not correct. count(field) returns the number of lines where field is not null.Overhang

© 2022 - 2024 — McMap. All rights reserved.