从每个DEPT中选择具有最大SAL的EMP

时间:2013-05-07 21:01:05

标签: sql oracle greatest-n-per-group

我的SQL查询时间很糟糕。我正在使用oracle默认表:

'EMP'TABLE

http://imageshack.us/photo/my-images/850/sinttuloxps.png/

'DEPT'TABLE

http://imageshack.us/photo/my-images/19/assayp.png/


我不想让每个部门薪水最高的员工。

我正在使用SQLPLUS。

提前致谢!

ALSO CODE:

    CREATE TABLE dept (
    deptno numeric(2,0) NOT NULL,
    dname character varying(14),
    loc character varying(13),
    CONSTRAINT dept_pkey PRIMARY KEY (deptno)
);

CREATE TABLE emp (
    empno numeric(4,0) NOT NULL,
    ename character varying(10),
    job character varying(9),
    mgr numeric(4,0),
    hiredate date,
    sal numeric(7,2),
    comm numeric(7,2),
    deptno numeric(2,0),
    CONSTRAINT emp_pkey PRIMARY KEY (empno),
    CONSTRAINT emp_deptno_fkey FOREIGN KEY (deptno) REFERENCES dept(deptno),
    CONSTRAINT emp_mgr_fkey FOREIGN KEY (mgr) REFERENCES emp(empno)
);

INSERT INTO dept (deptno, dname, loc) VALUES (10, 'ACCOUNTING', 'NEW YORK');
INSERT INTO dept (deptno, dname, loc) VALUES (20, 'RESEARCH', 'DALLAS');
INSERT INTO dept (deptno, dname, loc) VALUES (30, 'SALES', 'CHICAGO');
INSERT INTO dept (deptno, dname, loc) VALUES (40, 'OPERATIONS', 'BOSTON');

insert into emp values (7839, 'KING'   ,    'PRESIDENT',   NULL, to_date('17/11/81','dd/mm/yy') ,      5000,       NULL,     10);        
insert into emp values (7566, 'JONES'  ,    'MANAGER'  ,   7839, to_date('02/04/81','dd/mm/yy') ,      2975,       NULL,     20);        
insert into emp values (7902, 'FORD'   ,    'ANALYST'  ,   7566, to_date('03/12/81','dd/mm/yy') ,      3000,       NULL,     20);        
insert into emp values (7369, 'SMITH'  ,    'CLERK'    ,   7902, to_date('17/12/80','dd/mm/yy') ,       800,       NULL,     20);
insert into emp values (7698, 'BLAKE'  ,    'MANAGER'  ,   7839, to_date('01/05/81','dd/mm/yy') ,      2850,       NULL,     30);        
insert into emp values (7499, 'ALLEN'  ,    'SALESMAN' ,   7698, to_date('20/02/81','dd/mm/yy') ,      1600,        300,     30);        
insert into emp values (7521, 'WARD'   ,    'SALESMAN' ,   7698, to_date('22/02/81','dd/mm/yy') ,      1250,        500,     30);        
insert into emp values (7654, 'MARTIN' ,    'SALESMAN' ,   7698, to_date('28/09/81','dd/mm/yy') ,      1250,       1400,     30);        
insert into emp values (7782, 'CLARK'  ,    'MANAGER'  ,   7839, to_date('09/06/81','dd/mm/yy') ,      2450,       NULL,     10);        
insert into emp values (7788, 'SCOTT'  ,    'ANALYST'  ,   7566, to_date('09/12/82','dd/mm/yy') ,      3000,       NULL,     20);        
insert into emp values (7844, 'TURNER' ,    'SALESMAN' ,   7698, to_date('08/09/81','dd/mm/yy') ,      1500,          0,     30);        
insert into emp values (7876, 'ADAMS'  ,    'CLERK'    ,   7788, to_date('12/01/83','dd/mm/yy') ,      1100,       NULL,     20);        
insert into emp values (7900, 'JAMES'  ,    'CLERK'    ,   7698, to_date('03/12/81','dd/mm/yy') ,       950,       NULL,     30);        
insert into emp values (7934, 'MILLER' ,    'CLERK'    ,   7782, to_date('23/01/82','dd/mm/yy') ,      1300,       NULL,     10);        

5 个答案:

答案 0 :(得分:5)

您还可以使用分析RANK()函数:

SELECT * FROM (
  SELECT
    Dept.DeptNo,
    Dept.DName,
    Emp.EName,
    Emp.Sal,
    RANK() OVER (PARTITION BY Dept.DeptNo ORDER BY Emp.Sal DESC) AS DeptSalRank
  FROM Emp
  INNER JOIN Dept ON Emp.DeptNo = Dept.DeptNo
)
WHERE DeptSalRank = 1

答案 1 :(得分:2)

经典查询。这是你想要的:

select dept.dname, emp.empno, emp.ename, emp.sal
from emp
inner join dept on emp.deptno = dept.deptno
inner join
(
select emp.deptno, max(emp.sal) sal
from emp
group by emp.deptno
) ss on emp.deptno = ss.deptno and emp.sal = ss.sal
order by emp.sal desc

这是一个工作小提琴:http://sqlfiddle.com/#!4/7147b/6

此外,您可能想要检查其他方法。请看这里(SQL Select only rows with Max Value on a Column),看看有关该主题的有趣答案。

答案 2 :(得分:1)

以下查询将省略重复值

SELECT DEPTNO, MAX(SAL) FROM EMP GROUP BY DEPTNO;

以下查询将包含重复值

SELECT * FROM EMP WHERE (DEPTNO,SAL) IN 
( SELECT DEPTNO, MAX(SAL) FROM EMP GROUP BY DEPTNO)

答案 3 :(得分:0)

简短问题:SELECT DeptID, MAX(Salary) FROM Employees GROUP BY DeptID

答案 4 :(得分:0)

经过测试。这是查询:

this.heroService.getHeroesFromWebAPI().subscribe( r => {this.HeroesWebAPI=r; 
          console.log("Final hit:"+  [this.HeroesWebAPI])});