Encontrar el valor más frecuente en la columna SQL
¿Cómo puedo encontrar el valor más frecuente en una columna dada en una tabla SQL?
Por ejemplo, para esta tabla debe devolver two
ya que es el valor más frecuente:
one
two
two
three
7 answers
SELECT `column`,
COUNT(`column`) AS `value_occurrence`
FROM `my_table`
GROUP BY `column`
ORDER BY `value_occurrence` DESC
LIMIT 1;
Sustitúyanse column
y my_table
. Aumente 1
si desea ver los valores N
más comunes de la columna.
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
2018-07-25 20:55:37
Intenta algo como:
SELECT `column`
FROM `your_table`
GROUP BY `column`
ORDER BY COUNT(*) DESC
LIMIT 1;
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-09-02 11:42:02
Consideremos el nombre de la tabla como tblperson
y el nombre de la columna como city
. Quiero recuperar la ciudad más repetida de la columna de la ciudad:
select city,count(*) as nor from tblperson
group by city
having count(*) =(select max(nor) from
(select city,count(*) as nor from tblperson group by city) tblperson)
Aquí nor
es un nombre de alias.
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-03 04:17:57
La siguiente consulta parece funcionar bien para mí en la base de datos de SQL Server:
select column, COUNT(column) AS MOST_FREQUENT
from TABLE_NAME
GROUP BY column
ORDER BY COUNT(column) DESC
Resultado:
column MOST_FREQUENT
item1 highest count
item2 second highest
item3 third higest
..
..
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-01-29 09:48:58
Para usar con SQL Server.
Ya que no hay soporte de comando de límite en eso.
Puede usar el comando top 1 para encontrar el valor máximo que ocurre en la columna en particular en este caso (valor)
SELECT top1
`value`,
COUNT(`value`) AS `value_occurrence`
FROM
`my_table`
GROUP BY
`value`
ORDER BY
`value_occurrence` DESC;
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-04-07 18:40:20
Suponiendo que la Tabla es 'SalesLT.Customer
' y la Columna que está tratando de averiguar es 'CompanyName
' y AggCompanyName
es un Alias.
Select CompanyName, Count(CompanyName) as AggCompanyName from SalesLT.Customer
group by CompanyName
Order By Count(CompanyName) Desc;
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-12-28 07:34:58
Si no puede usar LIMIT o LIMIT no es una opción para su herramienta de consulta. Puede usar "ROWNUM" en su lugar, pero necesitará una sub consulta:
SELECT FIELD_1, ALIAS1
FROM(SELECT FIELD_1, COUNT(FIELD_1) ALIAS1
FROM TABLENAME
GROUP BY FIELD_1
ORDER BY COUNT(FIELD_1) DESC)
WHERE ROWNUM = 1
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-11-24 16:59:39