使用Oracle在SQL中跨多个表进行聚合

时间:2014-05-10 08:19:06

标签: sql oracle join aggregate-functions

我是SQL的新手,并且一直在努力使用聚合函数进行复杂的多重连接。

以下是我正在寻找的结果集:

  

编写查询以显示工资等级(最低)的部门名称,   最低工资和平均佣金。对于null的部门   佣金,你应该显示0.(salgrade表可以用于   获得工资等级。)

我有三张桌子:

  • emp - 代表员工,他们的名字,相关部门和 薪金
  • dept - 代表部门及其相关标题和位置
  • salgrade - 代表多个工资范围

以下是表格说明和数据集:

SQL> desc emp
 Name                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 EMPNO                     NOT NULL NUMBER(4)
 ENAME                          CHAR(10)
 JOB                            CHAR(9)
 MGR                            NUMBER(4)
 HIREDATE                       DATE
 SAL                            NUMBER(7,2)
 COMM                           NUMBER(7,2)
 DEPTNO                    NOT NULL NUMBER(2)

SQL> select empno, ename, sal, deptno from emp;

     EMPNO ENAME         SAL     DEPTNO
---------- ---------- ---------- ----------
      7839 KING         5000     10
      7698 BLAKE        2850     30
      7782 CLARK        2450     10
      7566 JONES        2975     20
      7654 MARTIN       1250     30
      7499 ALLEN        1600     30
      7844 TURNER       1500     30
      7900 JAMES         950     30
      7521 WARD         1250     30
      7902 FORD         3000     20
      7369 SMITH         800     20

     EMPNO ENAME         SAL     DEPTNO
---------- ---------- ---------- ----------
      7788 SCOTT        3000     20
      7876 ADAMS        1100     20
      7934 MILLER       1300     10
      1456 JOHN SMITH       3000     20

15 rows selected.

SQL> desc dept
 Name                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 DEPTNO                    NOT NULL NUMBER(2)
 DNAME                          CHAR(14)
 LOC                            CHAR(13)

SQL> select * from dept
  2  ;

    DEPTNO DNAME      LOC
---------- -------------- -------------
    50 TRAINING   SAN FRANCISCO
    10 ACCOUNTING     NEW YORK
    20 RESEARCH   DALLAS
    30 SALES      CHICAGO
    40 OPERATIONS     BOSTON

SQL> desc salgrade;
 Name                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 GRADE                          NUMBER
 LOSAL                          NUMBER
 HISAL                          NUMBER

SQL> select * from salgrade;

     GRADE  LOSAL      HISAL
---------- ---------- ----------
     1    700       1200
     2   1201       1400
     3   1401       2000
     4   2001       3000
     5   3001       9999

Salgrade表的DDL:

CREATE TABLE SALGRADE ( 
GRADE NUMBER, 
LOSAL NUMBER, 
HISAL NUMBER); 

INSERT INTO SALGRADE VALUES (1,700,1200); 
INSERT INTO SALGRADE VALUES (2,1201,1400); 
INSERT INTO SALGRADE VALUES (3,1401,2000); 
INSERT INTO SALGRADE VALUES (4,2001,3000); 
INSERT INTO SALGRADE VALUES (5,3001,9999); 

我写了以下查询,但我正在努力计算每个部门的最低salgrade。我怎么做到这一点?

这是我到目前为止(我已经复制了MIN()函数来提供dumby数据以返回没有错误的结果):

SELECT  d.dname         AS "DEPARTMENT",
    MIN(NVL(e.sal,0))   AS "SALARY GRADE",
    MIN(NVL(e.sal,0))   AS "MINIMUM_SALARY",
    AVG(NVL(e.comm,0))  AS "AVERAGE COMMISSION"
FROM    dept d
FULL JOIN   emp e
ON      d.deptno = e.deptno
GROUP BY
    d.dname, d.deptno
ORDER BY d.deptno ASC

DEPARTMENT     SALARY GRADE MINIMUM_SALARY AVERAGE COMMISSION
-------------- ------------ -------------- ------------------
ACCOUNTING         1300       1300          0
RESEARCH        800        800          0
SALES           950        950     366.666667
OPERATIONS        0      0          0
TRAINING          0      0          0

请提供简要说明,以便我可以增加对SQL的了解并理解任何响应。在此先感谢您的帮助! :)

1 个答案:

答案 0 :(得分:1)

您需要使用BETWEEN加入SALGRADE表:

SELECT dt.*,
    sg.GRADE AS "SALARY GRADE"
FROM
 (
   SELECT  d.dname         AS "DEPARTMENT",
       MIN(NVL(e.sal,0))   AS "MINIMUM_SALARY",
       AVG(NVL(e.comm,0))  AS "AVERAGE COMMISSION"
   FROM    dept d
   LEFT JOIN   emp e
   ON      d.deptno = e.deptno
   GROUP BY
       d.dname, d.deptno
 ) dt
JOIN SALGRADE sg 
ON "MINIMUM_SALARY" BETWEEN sg.LOWSAL AND sg.HIGHSAL
ORDER BY dt.deptno ASC

说明:

  • 您不需要完全加入,LEFT就足够了
  • 如果薪水低于最低LOWSAL范围,您可以将第二次加入更改为LEFT