如何用*标记薪水最高的员工? -SQL Server

时间:2018-09-06 15:33:24

标签: sql sql-server

我遇到问题-这是我的NULL桌子:

Employee

现在我想要这样的结果:

Name  | Department  | Salary
------+-------------+-------------
Ram   |IT           |  1000    
Shyam |HR           |  2000    
Sita  |Management   |  5000  
Joe   |IT           |  6000    
Lacy  |HR           |  2000    
Gen   |Management   |  4000  
Maria |IT           |  2000    
Antony|HR           |  3000    
Raman |Management   |  50000  

Department | Salary | -------------+------------+ IT | 20000 HR | 30000 Management | 50000 | * 将用于标记任何部门的最高薪水。我尝试了许多失败的尝试,例如:

*

2 个答案:

答案 0 :(得分:5)

您想要一个窗口功能:

Select Department, max(Salary) as salary,
       (case When max(Salary) = max(Max(Salary)) over ()
             Then '*' 
        End)
from tblEmployees
group by department;

答案 1 :(得分:1)

结果中的IT部门应该是6000。

select Department,
       salary =         max_sal_dep,
       highest_salary = IIF(max_sal_dep = max_sal_all, '*', null)
from
(
    select Department,
           max_sal_all = MAX(Salary) over (),
           max_sal_dep = MAX(Salary) over (partition by Department order by Department),
           the_row = row_number () over (partition by Department order by Salary desc)
    from @t
) x where x.the_row = 1
order by x.max_sal_dep;