一. 分析函数主要语法
传统SQL的问题:
基本语法:
组合顺序:
1. 使用PARTITION子句
SELECT did,ename,sal,
SUM(sal) OVER (PARTITION BY did) sum
FROM emp;
2. 不适用PARITION进行分区,直接利用OVER子句操作
SELECT did,ename,sal,
SUM(sal) OVER () sum
FROM emp;
3. 通过PARTITION设置多个分区字段
SELECT did,ename,sal,job,
SUM(sal) OVER (PARTITION BY did,job) sum
FROM emp;
4. 观察ORDER BY 子句
SELECT did,ename,sal,
RANK() OVER (PARTITION BY did ORDER BY sal DESC) rk
FROM emp
# ORDER BY 子句的作用主要就是进行排序,但是现在实现的是分区内数据的排序,而这个会直接影响到最终得到查询结果
5. 直接利用 ORDER BY 排序所有数据
SELECT did,ename,sal,hiredate,
SUM(sal) OVER (ORDER BY ename DESC) sum
FROM emp;
6, ORDER BY 子句选项:
6.1 null在前
SELECT did,ename,sal,comm,
RANK() OVER (ORDER BY comm DESC) rk,
SUM(sal) OVER (ORDER BY comm DESC) sum
FROM emp;
6.2 null在后
SELECT did,ename,sal,comm,
RANK() OVER (ORDER BY comm DESC NULLS LAST) rk,
SUM(sal) OVER (ORDER BY comm DESC NULLS LAST) sum
FROM emp;
二. 分窗操作的使用
WINDOWING子句:
三. 了解基本分析函数