by max over partition_大数据技术之Hive Sum MAX Over Demo

本篇教程探讨了大数据技术之Hive Sum MAX Over Demo,希望阅读本篇文章以后大家有所收获,帮助大家对相关内容的理解更加深入。

A,2015-01,5

A,2015-01,15

B,2015-01,5

A,2015-01,8

B,2015-01,25

A,2015-01,5

A,2015-02,4

A,2015-02,6

B,2015-02,10

B,2015-02,5

A,2015-03,16

A,2015-03,22

B,2015-03,23

B,2015-03,10

B,2015-03,1

求每个用户单月的访问次数和总访问次数

create external table if not exists t_access(

uname string comment '用户名',

umonth string comment '月份',

ucount int comment '访问次数'

) comment '用户访问表'

row format delimited fields terminated by ","

location "/user/hive/t_access";

load data local inpath "/root/tmonthcount.txt" into table t_access;

select tba.*,tbb.allCount

from

(

select uname,umonth,sum(ucount) as tuconut

from t_access

group by uname,umonth) tba

join (select uname,sum(ucount) as allCount from t_access group by uname) tbb on tbb.uname=tba.uname

;

select uname,umonth,ucount,sum(ucount) over(partition by uname,umonth) as tuconut,sum(ucount) over(partition by uname) as allCount

from t_access;

A   2015-01 33  81

A   2015-02 10  81

A   2015-03 38  81

B   2015-01 30  79

B   2015-02 15  79

B   2015-03 34  79

每个用户截止到每月为止的最大单月访问次数和累计到该月的总访问次数,结果数据格式如下

select tmp.*

,max(tmp.tuconut) over(partition by tmp.uname order by tmp.umonth rows between unbounded preceding and current row) as maxCount

,sum(tmp.tuconut) over(partition by tmp.uname order by tmp.umonth rows between unbounded preceding and current row) as allCount

from

(select uname,umonth,sum(ucount) as tuconut

from t_access

group by uname,umonth) tmp;

A   2015-01 33  33  33

A   2015-02 10  33  43

A   2015-03 38  38  81

B   2015-01 30  30  30

B   2015-02 15  30  45

B   2015-03 34  34  79

本文由职坐标整理发布,学习更多的相关知识,请关注职坐标IT知识库!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值