繁体   English   中英

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

[英]Aggregate across multiple tables in SQL using Oracle

我对SQL相当陌生,并且一直在使用聚合函数来处理复杂的多重联接。

这是我要搜索的结果集:

编写查询以显示具有薪级(最低),最低薪水和平均佣金的部门名称。 对于零佣金的部门,您应该显示0。(薪级表可用于获取薪水等级)。

我有三个表:

  • emp-代表员工,他们的姓名,相关部门和薪水
  • 部门-代表部门,其相关的职务和位置
  • 薪金-代表薪金的多个范围

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

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

我已经写了以下查询,但是我在努力计算每个部门的最低工资。 我将如何实现?

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

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的了解并理解任何响应。 在此先感谢您的帮助! :)

您需要使用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

备注:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM