在他的部门找到薪水最高的员工

Find the employee with max salary in his department

Table结构如下

id  dept    salary
1   30  2000
2   20  5500
3   30  6700
4   30  8900
5   30  9900
6   10  1120
7   20  8900
8   10  2400
9   30  2600
10  10  2999

我需要输出有两列:

身份证和工资

id 应该是唯一的,salary 应该是最高薪水

如果同一部门内没有其他人的薪水更高,则对 return 雇员使用 NOT EXISTS

select * from table t1
where not exists (select 1 from table t2
                  where t2.dept = t1.dept
                    and t2.salary > t1.salary)

核心 SQL-99,将与 Oracle 和 Sybase 一起工作!

备选方案:

select * from table
where (dept, salary) = (select dept, max(salary)
                        from table
                        group by dept)

在 Core SQL-2003 之外使用以下功能:F641,"Row and table constructors"。 (这里不知道Sybase和Oracle支持什么。。。)

SELECT ID,Salary FROM TABLE_NAME WHERE SALARY =(SELECT MAX(SALARY) FROM TABLE_NAME)
SELECT a.Id, a.Salary
FROM table a
WHERE a.Salary = (SELECT MAX(Salary) FROM table
                  WHERE Id = a.Id)
select * from (select * from dep order by salary desc, dep, id) t
group by t.dep

使用单次扫描:

WITH SALARIES AS (
    SELECT 1  ID, 30 DEPT, 2000 SALARY FROM DUAL UNION ALL
    SELECT 2  ID, 20 DEPT, 5500 SALARY FROM DUAL UNION ALL
    SELECT 3  ID, 30 DEPT, 6700 SALARY FROM DUAL UNION ALL
    SELECT 4  ID, 30 DEPT, 8900 SALARY FROM DUAL UNION ALL
    SELECT 5  ID, 30 DEPT, 9900 SALARY FROM DUAL UNION ALL
    SELECT 6  ID, 10 DEPT, 1120 SALARY FROM DUAL UNION ALL
    SELECT 7  ID, 20 DEPT, 8900 SALARY FROM DUAL UNION ALL
    SELECT 8  ID, 10 DEPT, 2400 SALARY FROM DUAL UNION ALL
    SELECT 9  ID, 30 DEPT, 2600 SALARY FROM DUAL UNION ALL
    SELECT 10 ID, 10 DEPT, 2999 SALARY FROM DUAL
)
SELECT
    MAX(ID) KEEP (DENSE_RANK LAST ORDER BY SALARY) ID,
    MAX(SALARY) SALARY
FROM
    SALARIES
GROUP BY
    DEPT;

以下三个查询均有效:

select t.dept, t.salary
from
(select dept,max(salary) as m_sal from tempdb..test
group by dept)x, tempdb..test t
where x.dept = t.dept
and x.m_sal = t.salary

select dept, id, salary as m_sal 
from tempdb..test t1
where salary = (select max(salary) from tempdb..test t2 where t2.dept = t1.dept)


select t1.dept,t1.salary from tempdb..test t1 
inner join
(select dept,max(salary) as m_sal from tempdb..test 
group by dept)x on
t1.salary=x.m_sal and
t1.dept=x.dept

虽然数据结构有点奇怪,但下面的查询会给出想要的结果:

 create table #tmp(
      id numeric(10,0),
      dept int,
      salary numeric(10,0))

    insert #tmp select 1,   30,  2000
    insert #tmp select 2,   20,  5500
    insert #tmp select 3,   30,  6700
    insert #tmp select 4,   30,  8900
    insert #tmp select 5,   30,  9900
    insert #tmp select 6,   10,  1120
    insert #tmp select 7,   20,  8900
    insert #tmp select 8,   10,  2400
    insert #tmp select 9,   30,  2600
    insert #tmp select 10,  10,  2999

    select * from #tmp order by dept  

    id           dept        salary       
    ------------ ----------- ------------ 
               6          10         1120 
               8          10         2400 
              10          10         2999 
               2          20         5500 
               7          20         8900 
               1          30         2000 
               9          30         2600 
               3          30         6700 
               4          30         8900 
               5          30         9900 

    select id, salary
      from #tmp t1, (select dept=dept, salaryMax=max(salary) from #tmp group by dept) t2
     where t1.dept = t2.dept  
       and t1.salary = t2.salaryMax

id           salary       
------------ ------------ 
           7         8900 
          10         2999 
           5         9900