查询有问题

发布于 2024-12-04 10:04:42 字数 2321 浏览 1 评论 0原文

我想包括 dname、deptno、loc、添加 ename 并将列标记为 Number_of_People 总结工资标签列 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

I would like to include dname, deptno, loc, add up ename and label the colum as Number_of_People sum up salary lable the colum Total Salary then join the dept table and I only want to do it for dept 30

Heres the error I get

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>

Table 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.

Table dept;

  SQL> select * from dept;

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月光色 2024-12-11 10:04:42

您需要将 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;

You need the where clause to come before the group by clause.

Try:

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;
蓬勃野心 2024-12-11 10:04:42

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 ;

Where clauses must come before group by clauses. If you need to do filtering after the grouping, use 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 ;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文