两个表和重复组的SQL最小值

时间:2018-05-08 12:32:55

标签: sql database oracle

我做了一些研究,我找不到真正的答案,我发现一点点运作良好,让我感到惊讶,因为它很简单,但仍然没有得到我必须的结果。所以,我有两张桌子,EMP& DEPT,我必须回答问题“显示部门名称以及每个部门的最低和最高工资”。

EMP

EMPNO   ENAME   JOB          MGR    HIREDATE    SAL     COMM    DEPTNO
7782    CLARK   MANAGER     7839    09/JUN/1981 2450    -       10
7934    MILLER  CLERK       7782    23/JAN/1982 1300    -       10
7788    SCOTT   ANALYST     7566    09/DEC/1982 3000    -       20
7876    ADAMS   CLERK       7788    12/JAN/1983 1100    -       20
7369    SMITH   CLERK       7902    17/DEC/1980 800     -       20
7902    FORD    ANALYST     7566    03/DEC/1981 3000    -       20
7566    JONES   MANAGER     7839    02/APR/1981 2975    -       20
7900    JAMES   CLERK       7698    03/DEC/1981 950     -       30
7499    ALLEN   SALESMAN    7698    20/FEB/1981 1600    300     30
7698    BLAKE   MANAGER     7839    01/MAY/1981 2850    -       30
7654    MARTIN  SALESMAN    7698    28/SEP/1981 1200    1250    30
7844    TURNER  SALESMAN    7698    08/SEP/1981 1500    0       30
7521    WARD    SALESMAN    7698    22/FEB/1981 1250    500     30
7839    KING    PRESIDENT    -      17/NOV/1981 5000    -       -

DEPT

DEPTNO  DNAME       LOC
10      ACCOUNTING  NEW YORK
20      RESEARCH    DALLAS
30      SALES       CHICAGO
40      OPERATIONS  BOSTON
50      PURCHASING  MIAMI

任何想法如何让它运作?我用过:

SELECT DEPTNO,MIN(SAL) MINI,MAX(SAL) MAXI FROM EMP 
WHERE DEPTNO IS NOT NULL GROUP BY DEPTNO ORDER BY DEPTNO,MINI,MAXI;

我确实得到了我想要的结果,但我没有部门名称,而是有部门编号。有小费吗?如何在我尝试时使用INNER JOIN

  

ORA-00918:列模糊定义

1 个答案:

答案 0 :(得分:0)

首先,我没有在您的查询中找到任何类型的加入,因此您需要加入join(即INNER JOIN / LEFT JOIN

select d.dname, min(e.sal) as MinSal, max(e.sal) as MaxSal
from emp e 
inner join dept d on d.deptno = e.deptno
group by d.dname;