Selecting the most common value from relation - SQL statement

后端 未结 3 1591
无人及你
无人及你 2020-12-30 08:24

I have a table within my database that has many records, some records share the same value for one of the columns. e.g.

|  id  |  name  |  software  |
______         


        
相关标签:
3条回答
  • 2020-12-30 08:49

    You can do it in many ways but the simplest way would be this

    SELECT item  
    FROM (SELECT item FROM your_table GROUP BY item ORDER BY COUNT(*) desc)  
    WHERE ROWNUM<=1;
    
    0 讨论(0)
  • 2020-12-30 08:58
    select top 1 software 
    from your_table 
    group by software
    order by count(*) desc 
    
    0 讨论(0)
  • 2020-12-30 09:13

    It depends if you want to use standard SQL or vendor specific extensions (another poster has a "top N" query which is not standard). A standard solution would be.

    SELECT software, COUNT(1) 
    FROM tablename
    GROUP BY software
    HAVING COUNT(1) = (
      SELECT MAX(sc) FROM (
        SELECT software, COUNT(1) sc
        FROM tablename
        GROUP BY software
      )
    )
    

    Note: this may return multiple rows if several pieces of software are tied for the most occurrences.

    0 讨论(0)
提交回复
热议问题