一、常用函数结构整理
row_number() over(order by …)
rank() over( order by …)
dense_rank() over(partition by … order by …)
count() over(partition by … order by …)
max() over(partition by … order by …)
min() over(partition by … order by …)
sum() over(partition by … order by …)
avg() over(partition by … order by …)
first_value() over(partition by … order by …)
last_value() over(partition by … order by …)
lag() over(order by …)
lead() over(order by …)
- row_number over
--使用partition by 表示分区后再进行行号显示
SELECT deptno,empno,eNAME,JOB,sal,row_number() OVER(PARTITION BY deptno ORDER BY job ASC) AS 分析函数累加 FROM scott.emp;
- lag、lead向上或者向下挪动一行【rank、dense_rank用法一致】
--使用partition by 表示分区后再下移
SELECT deptno,empno,eNAME,JOB,sal,lag(sal) OVER(PARTITION BY deptno ORDER BY sal ASC) AS 下移 FROM scott.emp;
--使用order by 则表示上移
SELECT deptno,empno,eNAME,JOB,sal,lead(sal) OVER(ORDER BY sal ASC) AS 上移 FROM scott.emp;
- sum,count,max,min,avg,first_value,last_value
--使用partition by 表示分区统计
SELECT deptno,empno,eNAME,JOB,sal,to_char(AVG(sal) OVER(PARTITION BY deptno),'fm999999999999990.00') AS 分析函数分块 FROM scott.emp;
--使用order by 则表示连续累加
SELECT deptno,empno,eNAME,JOB,sal,AVG(sal) OVER(ORDER BY deptno ASC) AS 分析函数累加 FROM scott.emp;
--使用partition by 表示分区后再进行处理
SELECT deptno,empno,eNAME,JOB,sal,AVG(sal) OVER(PARTITION BY deptno ORDER BY job ASC) AS 分析函数处理 FROM scott.emp;