hive中的窗口函数以及应用

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()求月销售额总最小的一个
排名函数 对学生分数进行排名
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值