hive学習02日間-アクセス回数統計
7712 ワード
hiveの書き方はsqlと似ていますが、少し違っています。今回はアナログデータを使ってhql統計を作成します。
当月の訪問回数を求め、当月までの毎月最大訪問回数、当月までの各ユーザー総訪問回数を求めます。
データテーブルは以下の通りです
select t t 1.uname,t 1.umonth,t 1.urrent_モンズ.cnt,max(t 2.urrentment)as max_cnt,sum(t 2.urrentщnt)as sum_cntfrom record 2 t 1 join record 2 t 1.uname=t 2.unamewere t 1.umonth==t 2.umonthgroup by t 1.uname,t 1.umonth,t 1.current_モンズ.cnt;
当月の訪問回数を求め、当月までの毎月最大訪問回数、当月までの各ユーザー総訪問回数を求めます。
データテーブルは以下の通りです
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
解法一:--(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 t t 1.uname,t 1.umonth,t 1.urrent_モンズ.cnt,max(t 2.urrentment)as max_cnt,sum(t 2.urrentщnt)as sum_cntfrom record 2 t 1 join record 2 t 1.uname=t 2.unamewere t 1.umonth==t 2.umonthgroup by t 1.uname,t 1.umonth,t 1.current_モンズ.cnt;
# :
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
解法二:select uname,umonth,current_モンズ.cnt,max(current Cover)over(partition by uname order by umonth)as mac_cnt,sum(current Cant)over(partition by uname order by umonth)as sum_cntfrom record 2--(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: 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/cdde7125bc77
転載先:https://www.cnblogs.com/students/p/10160376.html