Oracle_集合运算查询
2013-07-16 16:44
411 查看
一:并集(union/union all)
查询10和20号部门的员工
方法1. select * from emp where deptno in (10,20);
方法2. select * from emp where deptno=10 or deptno=20;
方法3. 集合运算
select * from emp where deptno=10
union
select * from emp where deptno=20;
利用集合运算实现group by的增强
SQL> select deptno,job,sum(sal) from emp group by deptno,job
2 union
3 select deptno,sum(sal) from emp group by deptno
4 union
5 select sum(sal) from emp;
结果错误。因为要遵循下面的4点注意
注意
1. 参与运算的各个集合必须列数相同 且类型一致
2. 采用第一个集合的表头作为最后的表头
3. 如果排序,必须在每个集合后使用相同的order by
4. 可以使用括号
select deptno,job,sum(sal) from emp group by deptno,job
union
select deptno,to_char(null),sum(sal) from emp group by deptno
union
select to_number(null),to_char(null),sum(sal) from emp;
在第二行和第三行分别补上to_char(null),to_number(null)
来保证:参与运算的各个集合列数相同 类型一致
SQL> break on deptno skip 2
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;
SQL> break on null
SQL> select deptno,job,sum(sal) from emp group by rollup(deptno,job);
--打开sql执行时间-------重点记住:用于查看sql语句性能对比
SQL> set timing on
SQL> select deptno,job,sum(sal) from emp group by rollup(deptno,job);
已用时间: 00: 00: 00.00
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;
已用时间: 00: 00: 00.01
-----可以看出第一句比第二句的集合运算性能要好
SQL> set timing off--------关闭时间
二:交集(INTERSECT)
SQL> select ename,sal from emp
2 where sal between 700 and 1300
3 INTERSECT
4 select ename,sal from emp
5 where sal between 1201 and 1400;
三:差集(MINUS)
SQL> select ename,sal from emp
2 where sal between 700 and 1300
3 minus
4 select ename,sal from emp
5 where sal between 1201 and 1400;
查询10和20号部门的员工
方法1. select * from emp where deptno in (10,20);
方法2. select * from emp where deptno=10 or deptno=20;
方法3. 集合运算
select * from emp where deptno=10
union
select * from emp where deptno=20;
利用集合运算实现group by的增强
SQL> select deptno,job,sum(sal) from emp group by deptno,job
2 union
3 select deptno,sum(sal) from emp group by deptno
4 union
5 select sum(sal) from emp;
结果错误。因为要遵循下面的4点注意
注意
1. 参与运算的各个集合必须列数相同 且类型一致
2. 采用第一个集合的表头作为最后的表头
3. 如果排序,必须在每个集合后使用相同的order by
4. 可以使用括号
select deptno,job,sum(sal) from emp group by deptno,job
union
select deptno,to_char(null),sum(sal) from emp group by deptno
union
select to_number(null),to_char(null),sum(sal) from emp;
在第二行和第三行分别补上to_char(null),to_number(null)
来保证:参与运算的各个集合列数相同 类型一致
SQL> break on deptno skip 2
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;
SQL> break on null
SQL> select deptno,job,sum(sal) from emp group by rollup(deptno,job);
--打开sql执行时间-------重点记住:用于查看sql语句性能对比
SQL> set timing on
SQL> select deptno,job,sum(sal) from emp group by rollup(deptno,job);
已用时间: 00: 00: 00.00
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;
已用时间: 00: 00: 00.01
-----可以看出第一句比第二句的集合运算性能要好
SQL> set timing off--------关闭时间
二:交集(INTERSECT)
SQL> select ename,sal from emp
2 where sal between 700 and 1300
3 INTERSECT
4 select ename,sal from emp
5 where sal between 1201 and 1400;
三:差集(MINUS)
SQL> select ename,sal from emp
2 where sal between 700 and 1300
3 minus
4 select ename,sal from emp
5 where sal between 1201 and 1400;
相关文章推荐
- oracle多表联合查询,统计查询,组函数,order by,having,子查询,集合运算
- Oracle学习笔记 -- day05 多表查询、连接查询、子查询、分页、行转列、集合运算
- oracle 多表查询,子查询,集合运算,分页查询
- 【oracle学习】3.子查询和集合运算练习
- oracle 多表联合查询,统计查询,组函数,order by,having,子查询,集合运算
- 【Java EE 学习 28 上】【oracle学习第二天】【子查询】【集合运算】【几种数据库对象】
- oracle 多表联合查询,统计查询,组函数,order by,having,子查询,集合运算
- oracle 多表联合查询,统计查询,组函数,order by,having,子查询,集合运算
- 源码-Oracle数据库管理-第九章-SQL查询-Part 4(集合运算和子查询)
- oracle--子查询和集合运算
- Oracle的子查询和集合运算
- oracle高级查询之连接查询、集合运算
- oracle多表联合查询,统计查询,组函数,order by,having,子查询,集合运算
- oracle 多表联合查询,统计查询,组函数,order by,having,子查询,集合运算,
- oracle多表联合查询,统计查询,组函数,order by,having,子查询,集合运算
- Orcale:子查询、集合运算、随堂练习相关子查询、创建和管理表、其他数据库对象
- oracle心得4--集合查询@oracle中的多表连接@案例分析
- ORACLE 数据查询集合即:查询结果的集合操作 并集 交集 差集
- oracle 数据库查询关联运算
- Oracle 基本操作二:内连接,左连接,右连接,自连接,集合查询(union)