我有一张员工表,其中包含部门、员工和薪水。我想要一个查询来列出部门明智的最高薪水和具有该薪水的员工的姓名。
我知道这很简单。我用谷歌搜索但找到了这样的答案,其中只列出了部门和薪水
SELECT dept, SUM (salary)
FROM employee
GROUP BY dept;
我有一张员工表,其中包含部门、员工和薪水。我想要一个查询来列出部门明智的最高薪水和具有该薪水的员工的姓名。
我知道这很简单。我用谷歌搜索但找到了这样的答案,其中只列出了部门和薪水
SELECT dept, SUM (salary)
FROM employee
GROUP BY dept;
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
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
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)
这会做到的。
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
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