【Hive】累乘 和 累加

通过窗口函数在Hive中实现累加累乘。如下:

1.依据date的顺序,对col分别进行累加和累乘;

2.在累乘过程中,由于进行了log转换,存在较小精度损失,用round()进行处理四舍五入处理;

3.rows between unbounded preceding and current row 表示窗口的范围在当前行及前面所有行;

select 
    col
    , sum(col) over(order by date rows between unbounded preceding and current row) as 累加
    , power(10, sum(log(10, col))over(order by date rows between unbounded preceding and current row)) as 累乘 
    , round(power(10, sum(log(10, col))over(order by date rows between unbounded preceding and current row))) as 累乘处理
from (
    select 1 as col, '2018-12-01' as date
    union all 
    select 2 as col, '2018-12-02' as date
    union all 
    select 3 as col, '2018-12-03' as date
    union all 
    select 4 as col, '2018-12-04' as date
) as a 

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值