oracle高级语言总结

版权声明: https://blog.csdn.net/qq_36421955/article/details/88850831
--分组函数:作用于一组数据,并对一组数据返回一个值
--常用的分组函数,avg,sum,min,max,count,wm_concat(行转列)
SELECT AVG(SAL),SUM(SAL) FROM EMP;
SELECT MIN(SAL),MAX(SAL) FROM EMP;
SELECT COUNT(*) FROM EMP;--*代表所有列  count(empno) 结果一样
SELECT COUNT(DEPTNO) FROM EMP;
SELECT COUNT(distinct DEPTNO) FROM EMP;
--行转列
SELECT DEPTNO,wm_concat(ENAME) ENAMES FROM EMP GROUP BY DEPTNO;
--分组函数和空值
SELECT AVG(SAL) FROM EMP;
SELECT SUM(SAL)/COUNT(*) FROM EMP;
SELECT SUM(SAL)/COUNT(SAL) FROM EMP;
SELECT AVG(COMM) FROM EMP;
SELECT SUM(COMM)/COUNT(*) FROM EMP;
SELECT SUM(COMM)/COUNT(COMM) FROM EMP;
--count(*) 和 count(comm) 不一样
select count(*),count(comm) from emp;--分组函数count(comm)会忽略空值
select count(nvl(comm,0)) from emp;  --nvl函数解决分组函数忽略空值问题
--分组数据
select deptno,avg(sal) from emp group by deptno;
select a,B,avg(x) from emp group by a,B,C;
--a,B不能再组函数里,a,b必须分组,c不一定被筛选出来
select avg(sal) from emp group by deptno;
--多个列的分组
SELECT DEPTNO,JOB,SUM(SAL) FROM EMP GROUP BY DEPTNO,JOB ORDER BY DEPTNO;
--过滤分组
select deptno,avg(sal) from emp group by deptno having avg(sal) > 2000;
--过滤分组函数 having
--where 和 having区别
--where后边不能使用组函数,多行函数但是having可以
select avg(sal) from emp group by deptno having emp.deptno=10;
select avg(sal) from emp where emp.deptno=10 group by deptno;
--没有组函数使用where效率高,having在分组基础上过滤结果,wehere先过滤在分组
SELECT AVG(SAL),DEPTNO FROM EMP GROUP BY DEPTNO,SAL ORDER BY SAL;
SELECT AVG(SAL),DEPTNO FROM EMP GROUP BY DEPTNO ORDER BY AVG(SAL);
SELECT AVG(SAL),DEPTNO FROM EMP GROUP BY DEPTNO ORDER BY 1 desc;--代表select语句中第一列
--分组函数嵌套
select max(avg(sal)) from emp group by deptno;
--group by 语句增强  小计  总计 工资报表
/*select deptno,job,sum(sal) from emp group by deptno,job
+
select deptno,sum(sal) from emp group by deptno
+
select sum(sal) from emp
=*/
select deptno,job,sum(sal) from emp group by rollup(deptno,job);
/*group by rollup(deptno,job)
group by a,b
group by a
group by null*/
break on deptno SKIP 2--相同部门号只显示一次,不同部门号空两行
set pagesize 30
--sqlplus报表功能
--标题,页码,别名
title col 15 '我的报表' col 35 sql.pno
col deptno heading 部门号
col job heading 职位
col sum(sal) heading 工资总额
break on deptno skip 1
--多表查询
--笛卡尔集 列数相加 行数相乘
select * from emp,dept order by emp.empno;
--应避免使用笛卡尔集 所以要有连接条件
--连接条件至少有n-1个,n是表的个数
--等值连接
select emp.empno,emp.ename,emp.sal,dept.dname from emp,dept where emp.deptno=dept.deptno
--不等值连接
select e.empno,e.ename,s.grade from emp e,salgrade s where e.sal <= s.hisal and e.sal>= s.losal
select e.empno,e.ename,s.grade from emp e,salgrade s where e.sal between s.losal and s.hisal
--外连接 核心 把连接条件不成立的记录仍然包含在最后的结果中
--左外连接:等号左边表仍然被包含在结果中 右外连接
--左连接
SELECT * FROM EMP E LEFT JOIN DEPT D ON E.EMPNO=D.DEPTNO
SELECT * FROM EMP E,DEPT D WHERE E.DEPTNO=D.DEPTNO(+)
--自连接 不能操作大表
select e1.ename,e2.ename from emp e1,emp e2 where e1.mgr=e2.empno
--层次查询 是一个单表查询 只有一张表不存在第二张表 因为只有在一张表时才不会产生笛卡尔集
--没有自连接直观
select *
from emp
connect by prior empno=mgr  --prior 相邻上层empno等于当前层mgr
start with empno=7839               --从任何节点开始
--start with mgr is null;
--伪列表示树的深度 也就是level 
select level,empno,ename,sal,mgr
from emp
connect by prior empno=mgr  --prior 相邻上层empno等于当前层mgr
start with empno=7839 
--子查询
select * from emp
where sal >
(
select sal from emp where ename='SCOTT'
)
--子查询十个问题
/**
 1子查询语法中的小括号
 子查询书写风格
 3可以使用子查询的位置 where,select,having,from
 不可以使用子查询的位置 group by
 5强调:from后面的子查询
 主查询和子查询可以不是同一张表
 7一般不在子查询中使用排序;但是在Top-N分析问题中,必须对子查询排序
 一般先执行子查询再执行主查询;但相关子查询例外
 9单行子查询只能使用单行操作符;多行子查询只能使用多行操作符
 注意:子查询中是null的问题
**/
--3.select后子查询必须是单行子查询
--6尽量使用多表查询而不是子查询 因为只有一个from访问一次数据--视具体情况而定
--7 Top-n 查找工资最高的前三名 使用行号 rownum伪列
select rownum,empno,ename,sal from emp;
select rownum,empno,ename from emp order by sal desc;
--rownum 行号永远按照默认的顺序生成,行号只能使用 <  <=  不能使用 >  >=
select rownum,t.* from (select empno,ename from emp order by sal desc) t where rownum <= 3
--相关子查询   除了这个情况 其他子查询都是先执行子查询
select e.empno,e.ename,e.sal,(select avg(sal) from emp where deptno=e.deptno) avgsal
from emp e
where e.sal > (select avg(e1.sal) from emp e1  where e1.deptno=e.deptno)
--多行操作符  in,any,all
select * from emp where sal > any(select sal from emp where deptno=30)
select * from emp where sal > (select min(sal) from emp where deptno=30)
select * from emp where sal > all(select sal from emp where deptno=30)
select * from emp where sal > (select max(sal) from emp where deptno=30)
--10多行子查询返回null问题
select * from emp where empno not in (select mgr from emp)
select * from emp where empno in (select mgr from emp)
select * from emp where empno not in (select mgr from emp where mgr is not null)
--案例集锦
explain plan for
select * from emp;
select * from table(dbms_xplan.display)--查看执行计划的一种方式













猜你喜欢

转载自blog.csdn.net/qq_36421955/article/details/88850831