oracle高階函式(待整理)
阿新 • • 發佈:2019-01-28
create table dep( deptno varchar2(50) primary key, deptname varchar2(50) ); insert into dep values ('1','人事部'); insert into dep values ('2','技術部'); insert into dep values ('3','採購部'); commit; create table dep_emp( deptno varchar2(50), empno varchar2(50), ename varchar2(50), sal number(10,2) ); alter table dep_emp add constraint deptno_fk foreign key (deptno) references dep(deptno) on delete cascade; insert into dep_emp values ('1','011','張三',3000); insert into dep_emp values ('1','012','李四',3400); insert into dep_emp values ('2','021','王五',4300); insert into dep_emp values ('2','021','張三',3200); insert into dep_emp values ('3','031','李四',3400); insert into dep_emp values ('3','032','王五',2300); commit; --select * from user_tables; --select * from dep; --select * from dep_emp;
--顯示各部門員工的工資,並附帶顯示該部分的最高工資。 SELECT E.DEPTNO, E.EMPNO, E.ENAME, E.SAL, LAST_VALUE(E.SAL) OVER(PARTITION BY E.DEPTNO ORDER BY E.SAL ROWS --unbounded preceding and unbouned following針對當前所有記錄的前一條、後一條記錄,也就是表中的所有記錄 --unbounded:不受控制的,無限的 --preceding:在...之前 --following:在...之後 BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) MAX_SAL FROM DEP_EMP E; --按照deptno分組,然後計算每組值的總和 SELECT EMPNO, ENAME, DEPTNO, SAL, SUM(SAL) OVER(PARTITION BY DEPTNO ORDER BY ENAME) max_sal FROM DEP_EMP; --對各部門進行分組,並附帶顯示第一行至當前行的彙總 SELECT EMPNO, ENAME, DEPTNO, SAL, --注意ROWS BETWEEN unbounded preceding AND current row 是指第一行至當前行的彙總 SUM(SAL) OVER(PARTITION BY DEPTNO ORDER BY ENAME ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) max_sal FROM DEP_EMP; --當前行至最後一行的彙總 SELECT EMPNO, ENAME, DEPTNO, SAL, --注意ROWS BETWEEN current row AND unbounded following 指當前行到最後一行的彙總 SUM(SAL) OVER(PARTITION BY DEPTNO ORDER BY ENAME ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) max_sal FROM DEP_EMP; --當前行的上一行(rownum-1)到當前行的彙總 SELECT EMPNO, ENAME, DEPTNO, SAL, --注意ROWS BETWEEN 1 preceding AND current row 是指當前行的上一行(rownum-1)到當前行的彙總 SUM(SAL) OVER(PARTITION BY DEPTNO ORDER BY ENAME ROWS BETWEEN 1 PRECEDING AND CURRENT ROW) max_sal FROM DEP_EMP; --當前行的上一行(rownum-1)到當前行的下輛行(rownum+2)的彙總 SELECT EMPNO, ENAME, DEPTNO, SAL, --注意ROWS BETWEEN 1 preceding AND 1 following 是指當前行的上一行(rownum-1)到當前行的下輛行(rownum+2)的彙總 SUM(SAL) OVER(PARTITION BY DEPTNO ORDER BY ENAME ROWS BETWEEN 1 PRECEDING AND 2 FOLLOWING) max_sal FROM DEP_EMP; select d.deptno,sum(d.sal),grouping(d.deptno) from dep_emp d group by cube(d.deptno); --分組後還有對於薪水的總體總計 select d.deptno,d.empno,sum(d.sal),grouping(d.empno) from dep_emp d group by rollup(d.deptno,d.empno); --分組後每組的總計和總體的總計 select d.deptno,sum(d.sal),grouping(d.deptno) from dep_emp d group by d.deptno; --普通分組 --cube,總計顯示在上面(過於深奧,比rollup更細) select d.deptno,sum(d.sal),grouping(d.deptno) from dep_emp d group by cube(d.deptno); --分組後還有對於薪水的總體總計 select d.deptno,d.empno,sum(d.sal),grouping(d.empno) from dep_emp d group by cube(d.deptno,d.empno); --分組後每組的總計和總體的總計 --grouping用作小計(次數),可以看做樹型的層級 --partition by 暫看成分組(不是很理解) --ratio_to_report(所佔百分比) select dense_rank()over(order by d.deptno desc),ratio_to_report(d.deptno)over() from dep_emp d; --連續排名 select rank()over( order by d.deptno desc),ratio_to_report(d.deptno)over() from dep_emp d; --非連續排名