SQL para encontrar el número de valores distintos en una columna


Puedo seleccionar todos los valores distintos en una columna de las siguientes maneras:

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

Pero, ¿cómo obtengo el recuento de filas de esa consulta? ¿Se requiere una subconsulta?

 277
Author: Stat-R, 2008-09-26

8 answers

Puede usar la palabra clave DISTINCT dentro del COUNT función agregada:

SELECT COUNT(DISTINCT column_name) AS some_alias FROM table_name

Esto contará solo los valores distintos para esa columna.

 511
Author: Noah Goodrich,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-08-28 13:46:29

Esto le dará TANTO los valores de columna distintos como el recuento de cada valor. Normalmente encuentro que quiero conocer ambas piezas de información.

select distinct columnName, count(columnName) as CountOf from tableName group by columnName
 136
Author: Paul James,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-10-27 15:45:30

Tenga en cuenta que Count() ignora los valores null, por lo que si necesita permitir null como su propio valor distinto, puede hacer algo complicado como:

select count(distinct my_col)
       + count(distinct Case when my_col is null then 1 else null end)
from my_table
/
 24
Author: David Aldridge,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-09-26 21:32:04
SELECT COUNT(DISTINCT column_name) FROM table as column_name_count;

Tienes que contar ese col distinto, luego darle un alias.

 9
Author: Pete Karl II,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-09-26 19:55:38

Una suma sql de los valores únicos de column_name y ordenados por la frecuencia:

SELECT column_name, COUNT(*) FROM table_name GROUP BY column_name ORDER BY 2 DESC;
 9
Author: xchiltonx,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-05-20 00:04:51
select count(*) from 
(
SELECT distinct column1,column2,column3,column4 FROM abcd
) T

Esto dará cuenta de distintos grupos de columnas.

 9
Author: gipinani,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-03-23 08:27:44
select Count(distinct columnName) as columnNameCount from tableName 
 6
Author: Wayne,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-03-23 08:27:58

Count (distinct ({fieldname})) es redundante

Simplemente Count({fieldname}) le da todos los valores distintos en esa tabla. No (como muchos presumen) solo te dará el Conteo de la tabla[es decir, NO es lo mismo que el Conteo(*) de la tabla]

 -6
Author: Paul Pena,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-12-18 02:13:16