How to find maximum avg

前端 未结 15 2170
长情又很酷
长情又很酷 2020-12-15 19:17

I am trying to display the maximum average salary; however, I can\'t seem to get it to work.

I can get a list of the average salaries to display with:



        
相关标签:
15条回答
  • 2020-12-15 20:18

    Here is how to also get the worker_id, inspired by previous answers:

    SELECT worker_id, MAX(avg_salary)
    FROM (SELECT worker_id, AVG(salary) AS avg_salary
          FROM workers
          GROUP BY worker_id);
    
    0 讨论(0)
  • 2020-12-15 20:20

    You can in this way that the first row is sorted in descending based on average find

    select top 1  worker_id, avg(salary) as avgsalary 
          from workers
          group by worker_id 
          order by avgsalary desc
    
    0 讨论(0)
  • 2020-12-15 20:22

    You can fix the query by adding a column alias to the column within the sub-query, like so:

    select max(avg_salary)
    from (select worker_id, avg(salary) avg_salary
          from workers
          group by worker_id);
    

    However, if worker_id uniquely identifies records on the workers table, this is functionally equivalent to (can be simplified to):

    select max(salary) from workers;
    
    0 讨论(0)
提交回复
热议问题