2

我有一张员工表,其中包含部门、员工和薪水。我想要一个查询来列出部门明智的最高薪水和具有该薪水的员工的姓名。

我知道这很简单。我用谷歌搜索但找到了这样的答案,其中只列出了部门和薪水

SELECT dept, SUM (salary)
FROM employee
GROUP BY dept; 
4

5 回答 5

4
SELECT e1.*
FROM employee e1
JOIN (SELECT dept, MAX(salary) FROM employee GROUP BY dept) e2 ON
    e1.dept = e2.dept AND e1.salary = e2.salary
于 2013-03-21T05:09:49.027 回答
3

SQL Server 2008 支持Window Functions帮助您获得所需的内容。

WITH recordList
AS
(
    SELECT  dept, employeeName, salary,
            DENSE_RANK() OVER (PARTITION BY dept ORDER BY salary DESC) rn
    FROM    employee
)
SELECT dept, employeeName, salary
FROM   recordList
WHERE  rn = 1
于 2013-03-21T05:06:13.600 回答
3
  SELECT e.*, d.deptname
    FROM employee e
    JOIN department d ON e.deptid = d.deptid
   WHERE EXISTS (SELECT 1
                   FROM employee e_in
                   JOIN department d_in ON e_in.deptid = d_in.deptid
                  WHERE d_in.deptid = d.deptid
               GROUP BY d_in.deptid
                 HAVING MAX(e_in.salary) = e.salary)
于 2013-03-21T05:07:53.380 回答
3

这会做到的。

SELECT E1.DEPT, E2.ENAME, E1.HIGHEST_SALARY
FROM
(SELECT DEPT, MAX(SALARY) HIGHEST_SALARY
FROM EMPLOYEE
GROUP BY DEPT) E1
INNER JOIN EMPLOYEE E2 ON E1.HIGHEST_SALARY = E2.SALARY
AND E1.DEPT = E2.DEPT
于 2013-03-21T05:17:49.197 回答
-1
select * from (select salary,last_name,dense_rank()
over (order by salary desc)
sal_rank from employees) where sal_rank <=3;

就是这样......这是下面的输出:

SALARY LAST_NAME                   SAL_RANK
---------- ------------------------- ----------
 24000 King                               1
 17000 Kochhar                            2
 17000 De Haan                            2
 14000 Russell                            3
于 2015-03-26T04:26:45.270 回答