hive學習02天-訪問次數統計

hive的寫法和sql相似,卻又有一點不同,本次採用模擬數據編寫hql統計訪問次數:sql

求出當月的訪問次數,截至當月前的每月最大訪問次數、截至當月前每一個用戶總的訪問次數。spa

數據表以下code

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

解法一:blog

--(1) # 先求出每一個用戶每月總訪問量 create table record2 as 
select uname,umonth,sum(ucount) as current_month_cnt
from t_access
group by uname,umonth; # record_2 表中內容爲: A
2015-01 33 A 2015-02 10 A 2015-03 38 B 2015-01 30 B 2015-02 15 B 2015-03 44 --(2)

 

select t1.uname,t1.umonth,t1.current_month_cnt,max(t2.current_month_cnt) as max_cnt,sum(t2.current_month_cnt) as sum_cnt
from record2 t1
join record2 t2
on t1.uname=t2.uname
where t1.umonth >=t2.umonth
group by t1.uname,t1.umonth,t1.current_month_cnt;it

 # 最終結果: A 2015-01 33  33  33 A 2015-02 10  43  33 A 2015-03 38  81  38 B 2015-01 30  30  30 B 2015-02 15  45  30 B 2015-03 44  89  44

解法二:io

 
 
--(1) # 先求出每一個用戶每月總訪問量 create table record2 as 
select uname,umonth,sum(ucount) as current_month_cnt
from t_access
group by uname,umonth; # record_2 表中內容爲: A 2015-01 33 A 2015-02 10 A 2015-03 38 B 2015-01 30 B 2015-02 15 B 2015-03 44


--2

select uname,umonth,current_month_cnt ,
max (current_month_cnt) over(partition by uname order by umonth) as mac_cnt,
sum(current_month_cnt) over(partition by uname order by umonth) as sum_cnt
from
record2;table

 結果: A 2015-01 33  33  33 A 2015-02 10  43  33 A 2015-03 38  81  38 B 2015-01 30  30  30 B 2015-02 15  45  30 B 2015-03 44  89  44 

 

代碼參考:https://www.jianshu.com/p/cdde7125bc77class

相關文章
相關標籤/搜索