1.说明
分析函数用于计算基于组的某种聚合值,它和聚合函数的不同之处是 ,对于每个组返回多行,而聚合函数对于每个组只返回一行。
2.创建数据结构
使用的脚本
create table tb1(id int,v1 int,v2 int);
insert into tb1(id,v1,v2) values(1,11,21);
insert into tb1(id,v1,v2) values(2,11,22);
insert into tb1(id,v1,v2) values(3,12,23);
insert into tb1(id,v1,v2) values(4,12,24);
3.1 例1
select id,v1,v2, sum(v2) over(partition by v1) from tb1
3.2 例2
select id,rank() over(partition by v1 order by v2 desc) mm from tb1
3.3 例3
select id,v1,v2,sum(v2) over(partition by null) v2_sum from tb1
3.4 例4
select id,v1,v2,v2*100/sum(v2) over(partition by v1) v2百分比 from tb1
3.5
select id,v1,v2,dense_rank() over(partition by v1 order by v2) from tb1
select id,v1,v2,row_number() over(partition by v1 order by v2) from tb1
4 引用
https://www.cnblogs.com/theonewu/p/9446882.html
https://www.cnblogs.com/strivers/p/8351306.html