本文介绍了从每个DEPT中选择具有最大SAL的EMP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在进行SQL查询时遇到了麻烦.我正在使用oracle默认表:

I´m having a bad time with a SQL query. I´m using oracle default tables:

"EMP"表

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

AND

演示"表

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

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

I wan´t to get the employee with the highest salary in each department.

我正在使用SQLPLUS.

I´m using SQLPLUS.

提前谢谢!

其他代码:

    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);        

推荐答案

经典最大的n个分组查询.这就是您想要的:

Classic greatest-n-per-group query. Here is what you want:

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

Here is a working fiddle: http://sqlfiddle.com/#!4/7147b/6

此外,您可能想签出其他方法.在这里查看( SQL仅选择带有列上的最大值),以查看有关该主题的有趣答案.

Additionally, you might want to checkout a different approach. Look here (SQL Select only rows with Max Value on a Column) to see an interesting answer on the topic.

这篇关于从每个DEPT中选择具有最大SAL的EMP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 19:23