403

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?

Christian Oudard
  • 45,305
  • 24
  • 64
  • 69

12 Answers12

696

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.

rstackhouse
  • 2,020
  • 23
  • 25
Noah Goodrich
  • 24,292
  • 13
  • 64
  • 95
216

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]
Crayons
  • 1,668
  • 1
  • 11
  • 27
Paul James
  • 2,209
  • 1
  • 11
  • 2
30

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;
xchiltonx
  • 1,796
  • 3
  • 18
  • 18
29

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
/
David Aldridge
  • 50,423
  • 8
  • 67
  • 94
13
SELECT COUNT(DISTINCT column_name) FROM table as column_name_count;

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

Pete Karl II
  • 3,850
  • 3
  • 20
  • 27
12
select count(*) from 
(
SELECT distinct column1,column2,column3,column4 FROM abcd
) T

This will give count of distinct group of columns.

gipinani
  • 13,294
  • 11
  • 53
  • 82
11
select Count(distinct columnName) as columnNameCount from tableName 
gipinani
  • 13,294
  • 11
  • 53
  • 82
Wayne
  • 36,577
  • 4
  • 38
  • 49
1

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

select count(distinct(Column_Name)) from TableName
Asclepius
  • 49,954
  • 14
  • 144
  • 128
Nilesh Shinde
  • 439
  • 5
  • 9
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
Asclepius
  • 49,954
  • 14
  • 144
  • 128
Alper
  • 41
  • 2
  • 5
1

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.

Asclepius
  • 49,954
  • 14
  • 144
  • 128
0
select count(distinct(column_name)) AS columndatacount from table_name where somecondition=true

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

Asclepius
  • 49,954
  • 14
  • 144
  • 128
Nitika Chopra
  • 1,121
  • 15
  • 21
-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]

  • 3
    No, this is *not* correct. `count(field)` returns the number of lines where `field` is `not null`. – Antti29 Mar 26 '15 at 07:22