简体   繁体   English

仅按最大值选择名称

[英]select only name by maximal value

I have a request 我有个请求

select
    w.Departament_Id, avg(w.Salary) as avgSal,
    dep.Name as Name
from
    Employee w
join
    Departament dep
on
    dep.Id = w.Departament_Id 
group by
    w.Departament_Id,
    dep.Name

this request retuns a table contains all avg salary for each departament. 此请求重新调整一个表,其中包含每个部门的所有平均薪水。 Next the target is to select Name of departament with a maximal avgSal value. 接下来的目标是选择具有最大avgSal值的部门Name How to solve it? 怎么解决呢?

sort by the aggregate and take the top 1: 按汇总排序,并采用前1名:

select TOP 1
    w.Departament_Id, 
    avg(w.Salary) as avgSal,
    dep.Name as Name
from Employee w
join Departament dep
    on dep.Id = w.Departament_Id 
group by
    w.Departament_Id,
    dep.Name
ORDER BY avg(w.Salary) DESC

The syntax may be slightly different depending on your server software (some will allow ORDER BY avgSal , some will not; some will use LIMIT instead of TOP ), but that's the general idea. 语法可能会略有不同,具体取决于您的服务器软件(有些将允许ORDER BY avgSal ,有些则不允许;有些将使用LIMIT而不是TOP ),但这是一般的想法。

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

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