/home/hadoop/nisj/UserPortrait/indexCalc-view-byGameId.sql
-- 观看时长等的计算 -- 观看总时长、总次数;有效观看总时长、有效观看次数、有效平均时长;第一次观看时间 drop table if exists rcd_static_view_bygameid_basic; create table rcd_static_view_bygameid_basic as with Tab_recommend_data_view_byDay as( select uid,gameid,sum(view_time) view_time,pt_day from recommend_data_view where uid>0 group by uid,gameid,pt_day) select uid,gameid, sum(view_time) total_view_time, count(view_time) total_view_times, sum(case when view_time>=5 then view_time else 0 end ) total_view_time_effective, sum(case when view_time>=5 then 1 else 0 end ) total_view_times_effective, sum(case when view_time>=5 then view_time else 0 end )/sum(case when view_time>=5 then 1 else 0 end ) avg_view_time_effective, min(pt_day) frist_view_day, max(pt_day) newest_view_day from Tab_recommend_data_view_byDay group by uid,gameid ; -- 最后一次、倒数第二次有效观看及单次有效最大观看时长相关(时长与时间) drop table if exists rcd_static_view_bygameid_rank; create table rcd_static_view_bygameid_rank as with Tab_recommend_data_view_byDay as( select uid,gameid,sum(view_time) view_time,pt_day from recommend_data_view where uid>0 group by uid,gameid,pt_day) select uid,gameid, max(case when effective_desc_rk=1 then pt_day end) tailender_effective_day, max(case when effective_desc_rk=1 then view_time end) tailender_effective_view_time, max(case when effective_desc_rk=2 then pt_day end) penul_timate_effective_day, max(case when effective_desc_rk=2 then view_time end) penul_timate_effective_view_time, max(case when effective_desc_rk2=1 then pt_day end) max_effective_view_day, max(case when effective_desc_rk2=1 then view_time end) max_effective_view_time from ( select uid,gameid,view_time,pt_day, row_number()over(partition by uid,gameid order by pt_day desc) effective_desc_rk, row_number()over(partition by uid,gameid order by view_time desc) effective_desc_rk2 from Tab_recommend_data_view_byDay where view_time>=5 ) x1 group by uid,gameid ; -- 最近七天有效观看时长 drop table if exists rcd_static_view_bygameid_last7day; create table rcd_static_view_bygameid_last7day as with Tab_recommend_data_view_byDay as( select uid,gameid,sum(view_time) view_time,pt_day from recommend_data_view where uid>0 group by uid,gameid,pt_day) select uid,gameid, sum(case when view_time>=5 then view_time else 0 end ) total_view_time_effective, sum(case when view_time>=5 then 1 else 0 end ) total_view_times_effective, sum(case when view_time>=5 then view_time else 0 end )/sum(case when view_time>=5 then 1 else 0 end ) avg_view_time_effective, min(pt_day) frist_view_day, max(pt_day) newest_view_day from Tab_recommend_data_view_byDay where view_time>=5 and pt_day between date_sub(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),7) and date_sub(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),1) group by uid,gameid ; -- 按周统计每周有效观看时长 drop table if exists rcd_static_view_bygameid_byweek; create table rcd_static_view_bygameid_byweek as with Tab_recommend_data_view_byDay as( select uid,gameid,sum(view_time) view_time,pt_day from recommend_data_view where uid>0 group by uid,gameid,pt_day) select uid,gameid,concat(year(pt_day),'@',weekofyear(pt_day)) week_no, sum(case when view_time>=5 then view_time else 0 end ) total_view_time_effective, sum(case when view_time>=5 then 1 else 0 end ) total_view_times_effective, sum(case when view_time>=5 then view_time else 0 end )/sum(case when view_time>=5 then 1 else 0 end ) avg_view_time_effective, min(pt_day) frist_view_day, max(pt_day) newest_view_day from Tab_recommend_data_view_byDay where view_time>=5 group by uid,gameid,concat(year(pt_day),'@',weekofyear(pt_day)) ; -- 按月统计每月有效观看时长 drop table if exists rcd_static_view_bygameid_bymonth; create table rcd_static_view_bygameid_bymonth as with Tab_recommend_data_view_byDay as( select uid,gameid,sum(view_time) view_time,pt_day from recommend_data_view where uid>0 group by uid,gameid,pt_day) select uid,gameid,concat(year(pt_day),'@',month(pt_day)) month_no, sum(case when view_time>=5 then view_time else 0 end ) total_view_time_effective, sum(case when view_time>=5 then 1 else 0 end ) total_view_times_effective, sum(case when view_time>=5 then view_time else 0 end )/sum(case when view_time>=5 then 1 else 0 end ) avg_view_time_effective, min(pt_day) frist_view_day, max(pt_day) newest_view_day from Tab_recommend_data_view_byDay where view_time>=5 group by uid,gameid,concat(year(pt_day),'@',month(pt_day)) ;