1.sum(col2) over(partition by col1 order by col2) : 通过
col1分组对
col2
累计求和
2.min(col2) over(partition by col1 order by col2) : 通过
col1分组对
col2累计求每次的最小值
3.max(col2) over(partition by col1 order by col2) : 通过
col1分组对
col2累计求每次的最大值
4.avg(col2) over(partition by col1 order by col2) : 通过
col1分组对
col2累计求每次的平均值
5.
first_value(col) over(): 取分组内排序后,截止到当前行,第一个值
6.last_value(col) over() : 分组内排序后,截止到当前行,最后一个值
7.lag(col,n,DEFAULT) :
统计往前
n
行的
col
值,
n
可选,默认为
1
,
DEFAULT
当往上第
n
行为
NULL
时候, 取默认值,如不指定,则为NULL,一般搭配开窗函数使用,例如lag(col1,1,0) over(partition by col2 order by col1)
8.lead(col,n,DEFAULT) :
统计往后
n
行的
col
值,
n
可选,默认为
1
,
DEFAULT
当往下第
n
行为
NULL时候, 取默认值,如不指定,则为NULL,一般搭配开窗函数使用,例如lead(col1,1,0) over(partition by col2 order by col1)
9.ntile(n) :
用于将分组数据按照顺序切分成
n
片,返回当前切片值。注意:
n
必须为
int
类型。
10.排名函数:
row_number() over() :
排名函数,不会重复,适合于生成主键或者不并列排名
rank() over() :
排
名函数,有并列名次,名次不连续。一般搭配开窗函数使用,例如row_number() over(partition by col2 order by col1)
-- rank():有并列,相同名次空位,一般搭配开窗函数使用,例如rank() over(partition by col2 order by col1)
-- dense_rank():有并列,相同名次不空位,一般搭配开窗函数使用,例如dense_rank() over(partition by col2 order by col1)
应用
sum()
求月总销售额
mim()
求月最小销售额
max()
求月最大销售额
avg()
求月平均销售额
last_value()求月销售额总最小的一个
排名函数 对学生分数进行排名