标签:
注:以下所有sql案例均取自"oracle查询优化改写技巧与案例"丛书。SQL> select * from emp;
SQL> select * from dept t;
SQL> select empno as 编码, ename as 名称, nvl(mgr,deptno) as 上级编码
from emp
union all
select deptno as 编码,dname as 名称,NULL as 上级编码
from dept;SQL> select '' as cl from dual;C
SQL> select empno,ename from emp where empno = 1116 or ename = '林建国';
SQL> select empno,ename from emp where empno = 1116
union all
select empno,ename from emp where ename = '林建国';SQL> select empno,ename from emp where empno = 1116
union
select empno,ename from emp where ename = '林建国';SQL> select distinct deptno
from (select deptno from emp where mgr = 3320
union all
select deptno from emp where job = '后勤')
order by 1;SQL> select empno,deptno from emp where mgr = 3320
union
select empno,deptno from emp where job = '后勤';SQL> select deptno
from
(select ROWID,deptno from emp where mgr = 3320
union
select ROWID,deptno from emp where job = '后勤')
order by 1;SQL> select e.empno,e.ename,d.dname
from emp e
inner join dept d on (e.mgr = d.deptno)
where e.deptno = 30;select e.empno,e.ename,d.dname
from emp e,dept d
where e.mgr = d.deptno
and e.deptno = 30;SQL> create table emp2 as 2 select ename,job,sal,comm from emp where job = '销售';
SQL> select empno,ename,job,sal,deptno
from emp
where (ename,job,sal) in (select ename,job,sal from emp2);SQL> select empno,ename,job,sal,deptno
from emp a
where exists (select null
from emp2 b
where b.ename = a.ename
and b.job = a.job
and b.sal = a.sal); EMPNO ENAME JOB SAL DEPTNOSQL> select a.empno,a.ename,a.job,a.sal,a.deptno
from emp a
inner join emp2 b on (b.ename = a.ename and b.job = a.job and b.sal = a.sal);/*左表*/ create table L as select 'left_1' as str,'1' as v from dual union all select 'left_2','2' as v from dual union all select 'left_3','3' as v from dual union all select 'left_4','4' as v from dual; /*右表*/ create table R as select 'right_3' as str,'3' as v,1 as status from dual union all select 'right_4' as str,'4' as v,0 as status from dual union all select 'right_5' as str,'5' as v,0 as status from dual union all select 'right_6' as str,'6' as v,0 as status from dual;
SQL> select * from L;
SQL> select * from R;
SQL> select L.str as left_str,R.str as right_str
from L
inner join R on L.v = R.v
order by 1,2;SQL> select L.str as left_str,R.str as right_str
from L,R
where L.v = R.v
order by 1,2;SQL> select L.str as left_str,R.str as right_str
from L
left join R on L.v = R.v
order by 1,2;SQL> select L.str as left_str,R.str as right_str
from L,R
where L.v = R.v(+)
order by 1,2;SQL> select L.str as left_str,R.str as right_str
from L
right join R on L.v = R.v
order by 1,2;SQL> select L.str as left_str,R.str as right_str
from L,R
where L.v(+) = R.v
order by 1,2;SQL> select L.str as left_str,R.str as right_str
from L
full join R on L.v = R.v
order by 1,2;create table emp3 as select 7839 as empno,'tom' as ename,1 as mgr from dual union all select 7566 as empno,'jean' as ename,7839 as mgr from dual union all select 7788 as empno,'jack' as ename,7566 as mgr from dual union all select 7902 as empno,'allen' as ename,7788 as mgr from dual union all select 7369 as empno,'adams' as ename,7902 as mgr from dual union all select 7698 as empno,'jeckson' as ename,7839 as mgr from dual;
SQL> select * from emp3;
SQL> select 员工.empno as 职工编码,
员工.ename as 职工姓名,
员工.mgr as 员工表_主管编码,
主管.empno as 主管表_主管编码,
主管.ename as 主管姓名
from emp3 员工
left join emp3 主管 on (员工.mgr = 主管.empno)
order by 1;
SQL> select * from dept where deptno not in (select emp.mgr from emp where emp.m gr is not null);
SQL> select L.str as left_str,R.str as right_str,R.status
from L
left join R on L.v = R.v
order by 1,2;select L.str as left_str,R.str as right_str,R.status
from L
left join R on L.v = R.v
where R.status = 1
order by 1,2;select L.str as left_str,R.str as right_str,R.status
from L
where L.v = R.v(+)
and R.status = 1
order by 1,2;select L.str as left_str,R.str as right_str,R.status
from L
left join R on (L.v = R.v and r.status = 1)
order by 1,2;(+)用法select L.str as left_str,R.str as right_str,R.status
from L,R
where L.v = R.v(+)
and status(+) = 1
order by 1,2;语句也可以像下面这样写,先过滤,再用join,这样会更加清晰。select L.str as left_str,R.str as right_str,R.status
from L
left join (select * from R where R.status = 1)R on (L.v = R.v)
order by 1,2;
create or replace view v as
select * from emp where deptno != 20
union all
select * from emp where ename = '李磊磊';SQL> select rownum,empno,ename from v where ename = '李磊磊';
SQL> select rownum,empno,ename from emp where ename = '李磊磊';
SQL> select v.empno,v.ename,b.empno,b.ename
from v
full join emp b on (b.empno = v.empno)
where (v.empno is null or b.empno is null);SQL> select v.empno,v.ename,v.cnt,emp.empno,emp.ename,emp.cnt
from (select empno,ename,COUNT(*) as cnt from v group by empno,ename) v
full join (select empno,ename,COUNT(*) as cnt from emp group by empno,ename) emp
on (emp.empno = v.empno and emp.cnt = v.cnt)
where (v.empno is null or emp.empno is null);
create table emp_bonus (empno INT,received DATE,type INT); insert into emp_bonus values( 1111,date '2016-5-17',1); insert into emp_bonus values( 1112,date '2016-2-15',2); insert into emp_bonus values( 1115,date '2016-2-15',3);
SQL> select * from emp_bonus;
SQL> select e.empno,e.ename,e.sal
from emp e
where e.deptno = 30;SQL> select e.empno,
e.ename,
e.sal,
(e.sal * case
when eb.type = 1 then 0.1
when eb.type = 2 then 0.2
when eb.type = 3 then 0.3
end) as bonus
from emp e
inner join emp_bonus eb on (e.empno = eb.empno)
where e.deptno = 30
order by 1,2;SQL> select e.deptno,
SUM(e.sal) as total_sal,
SUM(e.sal * case
when eb.type = 1 then 0.1
when eb.type = 2 then 0.2
when eb.type = 3 then 0.3
end) as total_bonus
from emp e
inner join emp_bonus eb on (e.empno = eb.empno)
where e.deptno = 30
group by e.deptno;SQL> select e.deptno,
SUM(e.sal) as total_sal,
SUM(e.sal * eb2.rate) as total_bonus
from emp e
left join (select eb.empno,
SUM(case
when eb.type = 1 then 0.1
when eb.type = 2 then 0.2
when eb.type = 3 then 0.3
end) as rate
from emp_bonus eb
group by eb.empno) eb2 on eb2.empno = e.empno
group by e.deptno
order by 1; DEPTNO TOTAL_SAL TOTAL_BONUS标签:
原文地址:http://blog.csdn.net/acmman/article/details/51062195