2011-09-15 157 views
2

我想包括dname,deptno,loc,加上ename並將colum標記爲Number_of_People總計薪水標記Colum Total Salary然後加入部門表,我只想爲部門30查詢時出現問題

繼承人的錯誤我得到

SQL> select dname,deptno, loc, count(ename) as Number_of_People, sum(sal) as Total_Salary from emp join dept on emp.deptno = dept.deptno group by dname,loc where deptno =30; 
select dname,deptno, loc, count(ename) as Number_of_People, sum(sal) as Total_Salary from emp join dept on emp.deptno = dept.deptno group by dname,loc where deptno =30 

                    * 
ERROR at line 1: 
ORA-00933: SQL command not properly ended 


SQL> 

EMP表;

SQL> select empno, ename, job, mgr, hiredate,comm, deptno from emp; 

EMPNO ENAME  JOB    MGR HIREDATE  COMM  DEPTNO 
---------- ---------- --------- ---------- --------- ---------- ---------- 
    7839 KING  PRESIDENT   17-NOV-81     10 
    7698 BLAKE  MANAGER   7839 01-MAY-81     30 
    7782 CLARK  MANAGER   7839 09-JUN-81     10 
    7566 JONES  MANAGER   7839 02-APR-81     20 
    7654 MARTIN  SALESMAN  7698 28-SEP-81  1400   30 
    7499 ALLEN  SALESMAN  7698 20-FEB-81  300   30 
    7844 TURNER  SALESMAN  7698 08-SEP-81   0   30 
    7900 JAMES  CLERK   7698 03-DEC-81     30 
    7521 WARD  SALESMAN  7698 22-FEB-81  500   30 
    7902 FORD  ANALYST   7566 03-DEC-81     20 
    7369 SMITH  CLERK   7902 17-DEC-80     20 

EMPNO ENAME  JOB    MGR HIREDATE  COMM  DEPTNO 
---------- ---------- --------- ---------- --------- ---------- ---------- 
    7788 SCOTT  ANALYST   7566 09-DEC-82     20 
    7876 ADAMS  CLERK   7788 12-JAN-83     20 
    7934 MILLER  CLERK   7782 23-JAN-82     10 

    14 rows selected. 

表部門;

SQL> select * from dept; 

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

回答

4

您需要where條款來了group by子句之前。

嘗試:

select dname,deptno, loc 
, count(ename) as Number_of_People 
, sum(sal) as Total_Salary 
from emp join dept on emp.deptno = dept.deptno 
where deptno=30 
group by dname,loc; 
+0

我得到一個錯誤,當我按照你擁有的方式運行。第1行出現錯誤:ORA-00918:不明確定義的列 - – user770022

+0

在這種情況下,'emp'和'dept'中定義了'dname','deptno'或'loc'之一。選擇你想要的那一個(例如,如果它是重複的'dname',選擇你想要'emp.dname'還是'dept.dname')。 – 2011-09-15 02:23:45

+0

我已經發布了這兩個表 – user770022

3

Where條款必須出現在group by條款。如果您需要在分組後做過濾,使用having

select dname, emp.deptno as deptno, loc, count(ename) as Number_of_People, 
    sum(sal) as Total_Salary from emp join dept on emp.deptno = dept.deptno 
    where emp.deptno =30 
    group by dname,loc ; 
+0

我得到一個錯誤,當我運行你的方式。第1行的錯誤: ORA-00918:列不明確定義 – user770022

+0

是的。你有兩個deptno列。儘管它們都是相同的值(根據您的連接條件),但您必須告訴SQL您要返回哪一個值。我編輯了聲明。 –

+0

我得到相同的錯誤 – user770022