Writing SQL query for getting maximum occurrence of a value in a column

后端 未结 8 546
遇见更好的自我
遇见更好的自我 2021-01-02 02:44

I have an emp table with the records below:

INSERT into emp(EmpId,Emp name, Manager)
Values(1,A,M1)
values(2,B,M1)
values(3,C,M2)
values(4,D,M3)         


        
相关标签:
8条回答
  • 2021-01-02 03:43

    If you are using Oracle Database, you can simply use stats_mode function this will return single value with highest occurrences.

    select stats_mode(manager) from emp;
    

    This is very easy to use function instead of writing multiple lines of sql query.

    0 讨论(0)
  • 2021-01-02 03:48
    SELECT
        count(e.last_name) count,
        d.last_name
    FROM
        employees e
    LEFT OUTER JOIN employees d ON e.manager_id = d.employee_id
    GROUP BY
        d.last_name
    ORDER BY
        count DESC;
    
    0 讨论(0)
提交回复
热议问题