使用Join和Window函数获取结果

时间:2019-07-17 12:54:20

标签: sql sql-server

Dept

Id   Salary  Dept
1     1000    A
2     2000    B
3     5000    A
4     2500    C
5     3000    D

所以输出

Id   Salary Dept
 1   6000   A

需要使用Join或Window函数获取薪金总和以及哪个部门作为最高薪金

2 个答案:

答案 0 :(得分:1)

您似乎想要:

select d.dept, sum(d.salary)
from dept d
group by d.dept
order by max(d.salary) desc
fetch first 1 row only

答案 1 :(得分:0)

如果您正在使用MSSQL,则可以尝试

select top 1 d.Dept, Sum(d.Salary) as Salary from Dept d group by d.Dept order by Sum(d.Salary) desc