标签:
SQL> --集合操作: 并集、交集、差
SQL> -- union union all intersect minus
SQL> select detpno,job,sal,sum(sal) from emp group by rollup(deptno,job);
select detpno,job,sal,sum(sal) from emp group by rollup(deptno,job)
*
第 1 行出现错误:
ORA-00904: "DETPNO": 标识符无效
SQL> select deptno,job,sal from emp group by rollup(deptno,job);
select deptno,job,sal from emp group by rollup(deptno,job)
*
第 1 行出现错误:
ORA-00979: 不是 GROUP BY 表达式
SQL> select deptno,job,sal from emp group by rollup(deptno,job,sal);
DEPTNO JOB SAL
---------- ------------------ ----------
10 CLERK 1300
10 CLERK
10 MANAGER 2450
10 MANAGER
10 PRESIDENT 5000
10 PRESIDENT
10
20 CLERK 800
20 CLERK 1100
20 CLERK
20 ANALYST 3000
DEPTNO JOB SAL
---------- ------------------ ----------
20 ANALYST
20 MANAGER 2975
20 MANAGER
20
30 CLERK 950
30 CLERK
30 MANAGER 2850
30 MANAGER
30 SALESMAN 1500
30 SALESMAN 1600
30 SALESMAN 1250
DEPTNO JOB SAL
---------- ------------------ ----------
30 SALESMAN
30
已选择25行。
SQL> break on deptno skip 2;
SQL> /
DEPTNO JOB SAL
---------- ------------------ ----------
10 CLERK 1300
CLERK
MANAGER 2450
MANAGER
PRESIDENT 5000
PRESIDENT
20 CLERK 800
CLERK 1100
DEPTNO JOB SAL
---------- ------------------ ----------
20 CLERK
ANALYST 3000
ANALYST
MANAGER 2975
MANAGER
30 CLERK 950
CLERK
MANAGER 2850
DEPTNO JOB SAL
---------- ------------------ ----------
30 MANAGER
SALESMAN 1500
SALESMAN 1600
SALESMAN 1250
SALESMAN
已选择25行。
SQL> select deptno,job,sal,sum(sal) from emp group by rollup(deptno,job,sal);
DEPTNO JOB SAL SUM(SAL)
---------- ------------------ ---------- ----------
10 CLERK 1300 1300
CLERK 1300
MANAGER 2450 2450
MANAGER 2450
PRESIDENT 5000 5000
PRESIDENT 5000
8750
20 CLERK 800 800
CLERK 1100 1100
DEPTNO JOB SAL SUM(SAL)
---------- ------------------ ---------- ----------
20 CLERK 1900
ANALYST 3000 6000
ANALYST 6000
MANAGER 2975 2975
MANAGER 2975
10875
30 CLERK 950 950
CLERK 950
MANAGER 2850 2850
DEPTNO JOB SAL SUM(SAL)
---------- ------------------ ---------- ----------
30 MANAGER 2850
SALESMAN 1500 1500
SALESMAN 1600 1600
SALESMAN 1250 2500
SALESMAN 5600
9400
29025
已选择25行。
SQL> select deptno,job,sum(sal) from emp group by rollup(deptno,job);
DEPTNO JOB SUM(SAL)
---------- ------------------ ----------
10 CLERK 1300
MANAGER 2450
PRESIDENT 5000
8750
20 CLERK 1900
ANALYST 6000
MANAGER 2975
10875
DEPTNO JOB SUM(SAL)
---------- ------------------ ----------
30 CLERK 950
MANAGER 2850
SALESMAN 5600
9400
29025
已选择13行。
SQL> -- 查询10部门号20号部门的员工信息
SQL> select *
2 from emp
3 where deptno=10 or deptno=20;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- -------------- ---------- ---------- ----------
7369 SMITH CLERK 7902 17-12月-80 800 20
7566 JONES MANAGER 7839 02-4月 -81 2975
7782 CLARK MANAGER 7839 09-6月 -81 2450 10
7788 SCOTT ANALYST 7566 19-4月 -87 3000 20
7839 KING PRESIDENT 17-11月-81 5000 10
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- -------------- ---------- ---------- ----------
7876 ADAMS CLERK 7788 23-5月 -87 1100 20
7902 FORD ANALYST 7566 03-12月-81 3000
7934 MILLER CLERK 7782 23-1月 -82 1300 10
已选择8行。
SQL> select * from emp where deptno = 10
2 union
3 select * from emp where deptno = 20;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- -------------- ---------- ---------- ----------
7369 SMITH CLERK 7902 17-12月-80 800 20
7566 JONES MANAGER 7839 02-4月 -81 2975
7782 CLARK MANAGER 7839 09-6月 -81 2450 10
7788 SCOTT ANALYST 7566 19-4月 -87 3000 20
7839 KING PRESIDENT 17-11月-81 5000 10
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- -------------- ---------- ---------- ----------
7876 ADAMS CLERK 7788 23-5月 -87 1100 20
7902 FORD ANALYST 7566 03-12月-81 3000
7934 MILLER CLERK 7782 23-1月 -82 1300 10
已选择8行。
SQL> select deptno,job,sum(sal) from emp group by rollup(deptno,job);
DEPTNO JOB SUM(SAL)
---------- ------------------ ----------
10 CLERK 1300
MANAGER 2450
PRESIDENT 5000
8750
20 CLERK 1900
ANALYST 6000
MANAGER 2975
10875
DEPTNO JOB SUM(SAL)
---------- ------------------ ----------
30 CLERK 950
MANAGER 2850
SALESMAN 5600
9400
29025
已选择13行。
SQL> select deptno,job,sum(sal) from emp group by deptno,job;
DEPTNO JOB SUM(SAL)
---------- ------------------ ----------
20 CLERK 1900
30 SALESMAN 5600
20 MANAGER 2975
30 CLERK 950
DEPTNO JOB SUM(SAL)
---------- ------------------ ----------
10 PRESIDENT 5000
30 MANAGER 2850
10 CLERK 1300
MANAGER 2450
DEPTNO JOB SUM(SAL)
---------- ------------------ ----------
20 ANALYST 6000
已选择9行。
SQL> union
SP2-0042: 未知命令 "union" - 其余行忽略。
SQL> select deptno,sum(sal) from emp group by detpno;
select deptno,sum(sal) from emp group by detpno
*
第 1 行出现错误:
ORA-00904: "DETPNO": 标识符无效
SQL> union
SP2-0042: 未知命令 "union" - 其余行忽略。
SQL> select deptno,job,sum(sal) from emp group by deptno,job;
DEPTNO JOB SUM(SAL)
---------- ------------------ ----------
20 CLERK 1900
30 SALESMAN 5600
20 MANAGER 2975
30 CLERK 950
DEPTNO JOB SUM(SAL)
---------- ------------------ ----------
10 PRESIDENT 5000
30 MANAGER 2850
10 CLERK 1300
MANAGER 2450
DEPTNO JOB SUM(SAL)
---------- ------------------ ----------
20 ANALYST 6000
已选择9行。
SQL> union
SP2-0042: 未知命令 "union" - 其余行忽略。
SQL> select deptno,sum(sal) from emp group by deptno;
DEPTNO SUM(SAL)
---------- ----------
30 9400
20 10875
10 8750
SQL> union
SP2-0042: 未知命令 "union" - 其余行忽略。
SQL> -- 集合操作需要注意的事项:
SQL> -- 1. 集合的所有列应该数量相同,类型相同
SQL> --2. 集合操作的结果以第一条sql语句的表头为表头
SQL>
SQL> select deptno,job,sum(sal) from emp group by deptno,job;
DEPTNO JOB SUM(SAL)
---------- ------------------ ----------
20 CLERK 1900
30 SALESMAN 5600
20 MANAGER 2975
30 CLERK 950
DEPTNO JOB SUM(SAL)
---------- ------------------ ----------
10 PRESIDENT 5000
30 MANAGER 2850
10 CLERK 1300
MANAGER 2450
DEPTNO JOB SUM(SAL)
---------- ------------------ ----------
20 ANALYST 6000
已选择9行。
SQL> union
SP2-0042: 未知命令 "union" - 其余行忽略。
SQL> select deptno,to_char(null),sum(sal) from emp group by deptno;
DEPTNO T SUM(SAL)
---------- - ----------
30 9400
20 10875
10 8750
SQL> union
SP2-0042: 未知命令 "union" - 其余行忽略。
SQL>
SQL> select deptno,job,sum(sal) from emp group by deptno,job
2 union
3 select deptno,to_char(null),sum(sal) from emp group by deptno
4 union
5 select to_number(null),to_char(null),sum(sal) from emp;
DEPTNO JOB SUM(SAL)
---------- ------------------ ----------
10 CLERK 1300
MANAGER 2450
PRESIDENT 5000
8750
20 ANALYST 6000
CLERK 1900
MANAGER 2975
10875
DEPTNO JOB SUM(SAL)
---------- ------------------ ----------
30 CLERK 950
MANAGER 2850
SALESMAN 5600
9400
29025
已选择13行。
SQL> -- 集合操作尽量少用
SQL> -- 打开oracle sql执行的时间统计功能
SQL> set timming on
SP2-0158: 未知的 SET 选项 "timming"
SQL> set timing on
SQL> select deptno,job,sum(sal) from emp group by rollup(deptno,job);
DEPTNO JOB SUM(SAL)
---------- ------------------ ----------
10 CLERK 1300
MANAGER 2450
PRESIDENT 5000
8750
20 CLERK 1900
ANALYST 6000
MANAGER 2975
10875
DEPTNO JOB SUM(SAL)
---------- ------------------ ----------
30 CLERK 950
MANAGER 2850
SALESMAN 5600
9400
29025
已选择13行。
已用时间: 00: 00: 00.10
SQL> select deptno,job,sum(sal) from emp group by deptno,job
2 union
3 select deptno,to_char(null),sum(sal) from emp group by deptno
4 union
5 select to_number(null),to_char(null),sum(sal) from emp;
DEPTNO JOB SUM(SAL)
---------- ------------------ ----------
10 CLERK 1300
MANAGER 2450
PRESIDENT 5000
8750
20 ANALYST 6000
CLERK 1900
MANAGER 2975
10875
DEPTNO JOB SUM(SAL)
---------- ------------------ ----------
30 CLERK 950
MANAGER 2850
SALESMAN 5600
9400
29025
已选择13行。
已用时间: 00: 00: 00.08
SQL> select deptno,job,sum(sal) from emp group by deptno,job
2 union
3 select deptno,to_char(null),sum(sal) from emp group by deptno
4 union
5 select to_number(null),to_char(null),sum(sal) from emp;
DEPTNO JOB SUM(SAL)
---------- ------------------ ----------
10 CLERK 1300
MANAGER 2450
PRESIDENT 5000
8750
20 ANALYST 6000
CLERK 1900
MANAGER 2975
10875
DEPTNO JOB SUM(SAL)
---------- ------------------ ----------
30 CLERK 950
MANAGER 2850
SALESMAN 5600
9400
29025
已选择13行。
已用时间: 00: 00: 00.09
SQL> select deptno,job,sum(sal) from emp group by deptno,job
2 union
3 select deptno,to_char(null),sum(sal) from emp group by deptno
4 union
5 select to_number(null),to_char(null),sum(sal) from emp;
DEPTNO JOB SUM(SAL)
---------- ------------------ ----------
10 CLERK 1300
MANAGER 2450
PRESIDENT 5000
8750
20 ANALYST 6000
CLERK 1900
MANAGER 2975
10875
DEPTNO JOB SUM(SAL)
---------- ------------------ ----------
30 CLERK 950
MANAGER 2850
SALESMAN 5600
9400
29025
已选择13行。
已用时间: 00: 00: 00.14
SQL> select deptno,job,sum(sal) from emp group by deptno,job
2 union
3 select deptno,to_char(null),sum(sal) from emp group by deptno
4 union
5 select to_number(null),to_char(null),sum(sal) from emp;
DEPTNO JOB SUM(SAL)
---------- ------------------ ----------
10 CLERK 1300
MANAGER 2450
PRESIDENT 5000
8750
20 ANALYST 6000
CLERK 1900
MANAGER 2975
10875
DEPTNO JOB SUM(SAL)
---------- ------------------ ----------
30 CLERK 950
MANAGER 2850
SALESMAN 5600
9400
29025
已选择13行。
已用时间: 00: 00: 00.07
SQL> select deptno,job,sum(sal) from emp group by deptno,job
2 union
3 select deptno,to_char(null),sum(sal) from emp group by deptno
4 union
5 select to_number(null),to_char(null),sum(sal) from emp;
DEPTNO JOB SUM(SAL)
---------- ------------------ ----------
10 CLERK 1300
MANAGER 2450
PRESIDENT 5000
8750
20 ANALYST 6000
CLERK 1900
MANAGER 2975
10875
DEPTNO JOB SUM(SAL)
---------- ------------------ ----------
30 CLERK 950
MANAGER 2850
SALESMAN 5600
9400
29025
已选择13行。
已用时间: 00: 00: 00.07
SQL> select deptno,job,sum(sal) from emp group by deptno,job
2 union
3 select deptno,to_char(null),sum(sal) from emp group by deptno
4 union
5 select to_number(null),to_char(null),sum(sal) from emp;
DEPTNO JOB SUM(SAL)
---------- ------------------ ----------
10 CLERK 1300
MANAGER 2450
PRESIDENT 5000
8750
20 ANALYST 6000
CLERK 1900
MANAGER 2975
10875
DEPTNO JOB SUM(SAL)
---------- ------------------ ----------
30 CLERK 950
MANAGER 2850
SALESMAN 5600
9400
29025
已选择13行。
已用时间: 00: 00: 00.07
SQL> select deptno,job,sum(sal) from emp group by deptno,job
2 union
3 select deptno,to_char(null),sum(sal) from emp group by deptno
4 union
5 select to_number(null),to_char(null),sum(sal) from emp;
DEPTNO JOB SUM(SAL)
---------- ------------------ ----------
10 CLERK 1300
MANAGER 2450
PRESIDENT 5000
8750
20 ANALYST 6000
CLERK 1900
MANAGER 2975
10875
DEPTNO JOB SUM(SAL)
---------- ------------------ ----------
30 CLERK 950
MANAGER 2850
SALESMAN 5600
9400
29025
已选择13行。
已用时间: 00: 00: 00.12
SQL> select deptno,job,sum(sal) from emp group by rollup(deptno,job);
DEPTNO JOB SUM(SAL)
---------- ------------------ ----------
10 CLERK 1300
MANAGER 2450
PRESIDENT 5000
8750
20 CLERK 1900
ANALYST 6000
MANAGER 2975
10875
DEPTNO JOB SUM(SAL)
---------- ------------------ ----------
30 CLERK 950
MANAGER 2850
SALESMAN 5600
9400
29025
已选择13行。
已用时间: 00: 00: 00.09
SQL> select deptno,job,sum(sal) from emp group by rollup(deptno,job);
DEPTNO JOB SUM(SAL)
---------- ------------------ ----------
10 CLERK 1300
MANAGER 2450
PRESIDENT 5000
8750
20 CLERK 1900
ANALYST 6000
MANAGER 2975
10875
DEPTNO JOB SUM(SAL)
---------- ------------------ ----------
30 CLERK 950
MANAGER 2850
SALESMAN 5600
9400
29025
已选择13行。
已用时间: 00: 00: 00.11
SQL> select deptno,job,sum(sal) from emp group by rollup(deptno,job);
DEPTNO JOB SUM(SAL)
---------- ------------------ ----------
10 CLERK 1300
MANAGER 2450
PRESIDENT 5000
8750
20 CLERK 1900
ANALYST 6000
MANAGER 2975
10875
DEPTNO JOB SUM(SAL)
---------- ------------------ ----------
30 CLERK 950
MANAGER 2850
SALESMAN 5600
9400
29025
已选择13行。
已用时间: 00: 00: 00.08
SQL> select deptno,job,sum(sal) from emp group by rollup(deptno,job);
DEPTNO JOB SUM(SAL)
---------- ------------------ ----------
10 CLERK 1300
MANAGER 2450
PRESIDENT 5000
8750
20 CLERK 1900
ANALYST 6000
MANAGER 2975
10875
DEPTNO JOB SUM(SAL)
---------- ------------------ ----------
30 CLERK 950
MANAGER 2850
SALESMAN 5600
9400
29025
已选择13行。
已用时间: 00: 00: 00.07
SQL> select deptno,job,sum(sal) from emp group by rollup(deptno,job);
DEPTNO JOB SUM(SAL)
---------- ------------------ ----------
10 CLERK 1300
MANAGER 2450
PRESIDENT 5000
8750
20 CLERK 1900
ANALYST 6000
MANAGER 2975
10875
DEPTNO JOB SUM(SAL)
---------- ------------------ ----------
30 CLERK 950
MANAGER 2850
SALESMAN 5600
9400
29025
已选择13行。
已用时间: 00: 00: 00.23
SQL> select deptno,job,sum(sal) from emp group by rollup(deptno,job);
DEPTNO JOB SUM(SAL)
---------- ------------------ ----------
10 CLERK 1300
MANAGER 2450
PRESIDENT 5000
8750
20 CLERK 1900
ANALYST 6000
MANAGER 2975
10875
DEPTNO JOB SUM(SAL)
---------- ------------------ ----------
30 CLERK 950
MANAGER 2850
SALESMAN 5600
9400
29025
已选择13行。
已用时间: 00: 00: 00.08
SQL> select deptno,job,sum(sal) from emp group by rollup(deptno,job);
DEPTNO JOB SUM(SAL)
---------- ------------------ ----------
10 CLERK 1300
MANAGER 2450
PRESIDENT 5000
8750
20 CLERK 1900
ANALYST 6000
MANAGER 2975
10875
DEPTNO JOB SUM(SAL)
---------- ------------------ ----------
30 CLERK 950
MANAGER 2850
SALESMAN 5600
9400
29025
已选择13行。
已用时间: 00: 00: 00.07
SQL> select deptno,job,sum(sal) from emp group by rollup(deptno,job);
DEPTNO JOB SUM(SAL)
---------- ------------------ ----------
10 CLERK 1300
MANAGER 2450
PRESIDENT 5000
8750
20 CLERK 1900
ANALYST 6000
MANAGER 2975
10875
DEPTNO JOB SUM(SAL)
---------- ------------------ ----------
30 CLERK 950
MANAGER 2850
SALESMAN 5600
9400
29025
已选择13行。
已用时间: 00: 00: 00.07
SQL> select deptno,job,sum(sal) from emp group by rollup(deptno,job);
DEPTNO JOB SUM(SAL)
---------- ------------------ ----------
10 CLERK 1300
MANAGER 2450
PRESIDENT 5000
8750
20 CLERK 1900
ANALYST 6000
MANAGER 2975
10875
DEPTNO JOB SUM(SAL)
---------- ------------------ ----------
30 CLERK 950
MANAGER 2850
SALESMAN 5600
9400
29025
已选择13行。
已用时间: 00: 00: 00.07
SQL> set timing off
SQL> select deptno,job,sum(sal) from emp group by rollup(deptno,job);
DEPTNO JOB SUM(SAL)
---------- ------------------ ----------
10 CLERK 1300
MANAGER 2450
PRESIDENT 5000
8750
20 CLERK 1900
ANALYST 6000
MANAGER 2975
10875
DEPTNO JOB SUM(SAL)
---------- ------------------ ----------
30 CLERK 950
MANAGER 2850
SALESMAN 5600
9400
29025
已选择13行。
SQL> spool off;
标签:
原文地址:http://www.cnblogs.com/877599949yan/p/5312950.html