简体   繁体   English

在 SQL 列中查找最常见的值

[英]Find most frequent value in SQL column

How can I find the most frequent value in a given column in an SQL table?如何在 SQL 表的给定列中找到最常见的值?

For example, for this table it should return two since it is the most frequent value:例如,对于这个表,它应该返回two ,因为它是最常见的值:

one
two
two
three
SELECT
  <column_name>,
  COUNT(<column_name>) AS `value_occurrence` 

FROM
  <my_table>

GROUP BY 
  <column_name>

ORDER BY 
  `value_occurrence` DESC

LIMIT 1;

Replace <column_name> and <my_table> .替换<column_name><my_table> Increase 1 if you want to see the N most common values of the column.如果要查看列的N个最常见值,则增加1

Try something like:尝试类似:

SELECT       `column`
    FROM     `your_table`
    GROUP BY `column`
    ORDER BY COUNT(*) DESC
    LIMIT    1;

Let us consider table name as tblperson and column name as city .让我们将表名视为tblperson ,将列名视为city I want to retrieve the most repeated city from the city column:我想从城市列中检索重复次数最多的城市:

 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)

Here nor is an alias name.这里nor是别名。

Below query seems to work good for me in SQL Server database:下面的查询在 SQL Server 数据库中似乎对我有用:

select column, COUNT(column) AS MOST_FREQUENT
from TABLE_NAME
GROUP BY column
ORDER BY COUNT(column) DESC

Result:结果:

column          MOST_FREQUENT
item1           highest count
item2           second highest 
item3           third higest
..
..

For use with SQL Server.用于 SQL Server。

As there is no limit command support in that.因为没有限制命令支持。

Yo can use the top 1 command to find the maximum occurring value in the particular column in this case (value)在这种情况下,哟可以使用 top 1 命令找到特定列中出现的最大值(值)

SELECT top1 
    `value`,
    COUNT(`value`) AS `value_occurrence` 
FROM     
    `my_table`
GROUP BY 
    `value`
ORDER BY 
    `value_occurrence` DESC;

Assuming Table is ' SalesLT.Customer ' and the Column you are trying to figure out is ' CompanyName ' and AggCompanyName is an Alias.假设表是“ SalesLT.Customer ”,而您要找出的列是“ CompanyName ”,而AggCompanyName是别名。

Select CompanyName, Count(CompanyName) as AggCompanyName from SalesLT.Customer
group by CompanyName
Order By Count(CompanyName) Desc;

If you can't use LIMIT or LIMIT is not an option for your query tool.如果您不能使用 LIMIT 或 LIMIT 不是您的查询工具的选项。 You can use "ROWNUM" instead, but you will need a sub query:您可以改用“ROWNUM”,但需要一个子查询:

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

If you have an ID column and you want to find most repetitive category from another column for each ID then you can use below query,如果您有一个 ID 列,并且您想从另一个列中为每个 ID 查找最重复的类别,那么您可以使用以下查询,

Table:桌子:

表格内容

Query:询问:

SELECT ID, CATEGORY, COUNT(*) AS FREQ
FROM TABLE
GROUP BY 1,2
QUALIFY ROW_NUMBER() OVER(PARTITION BY ID ORDER BY FREQ DESC) = 1;

Result:结果:

查询结果

Return all most frequent rows in case of tie在平局的情况下返回所有最频繁的行

Find the most frequent value in mysql,display all in case of a tie gives two possible approaches: 在 mysql 中查找最常见的值,如果出现平局,则显示全部给出两种可能的方法:

  1. Scalar subquery:标量子查询:

     SELECT "country", COUNT(country) AS "cnt" FROM "Sales" GROUP BY "country" HAVING COUNT("country") = ( SELECT COUNT("country") AS "cnt" FROM "Sales" GROUP BY "country" ORDER BY "cnt" DESC, LIMIT 1 ) ORDER BY "country" ASC
  2. With the RANK window function, available since MySQL 8+:使用RANK窗口函数,自 MySQL 8+ 起可用:

     SELECT "country", "cnt" FROM ( SELECT "country", COUNT("country") AS "cnt", RANK() OVER (ORDER BY COUNT(*) DESC) "rnk" FROM "Sales" GROUP BY "country" ) AS "sub" WHERE "rnk" = 1 ORDER BY "country" ASC

    This method might save a second recount compared to the first one.与第一次相比,这种方法可能会节省第二次重新计票。

    RANK works by ranking all rows, such that if two rows are at the top, both get rank 1 . RANK通过对所有行进行排名来工作,这样如果两行都在顶部,则都获得排名1 So it basically directly solves this type of use case.所以它基本上直接解决了这种类型的用例。

    RANK is also available on SQLite and PostgreSQL, I think it might be SQL standard, not sure. RANK在 SQLite 和 PostgreSQL 上也可用,我认为它可能是 SQL 标准,不确定。

In the above queries I also sorted by country to have more deterministic results.在上述查询中,我还按country /地区排序以获得更确定的结果。

Tested on SQLite 3.34.0, PostgreSQL 14.3, GitHub upstream .在 SQLite 3.34.0、PostgreSQL 14.3、 GitHub 上游测试。

Most frequent for each GROUP BY group每个GROUP BY组最常见的

One way I like to use is:我喜欢使用的一种方法是:

select *<given_column>*,COUNT(*<given_column>*)as VAR1 from Table_Name

group by *<given_column>*

order by VAR1 desc

limit 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM