hive以半小時爲維度進行統計的需求

從2020/04/22日開始,往延3天,按req_id關聯,統計曝光事件與點擊事件之間的時間間隔分佈情況,按30分鐘爲粒度,
點擊事件字段:req_id,clickTime,
曝光事件字段:req_id,exposureTime
需要統計clickTime-exposureTime在各個時間差(30分鐘,60分鐘,90分鐘。。。)的百分率
數據結果如下:

時間間隔(單位分鐘) 佔比情況(%) 備註
30 80
60 95
90 96
120 98
150 99
思路:
1.用req_id 關聯點擊事件left join 曝光事件,且曝光時間和點擊事件都存在的情況下,click[20200422],exposure[20200420,20200421,20200422]
2.獲取點擊時間和曝光時間
3.利用2步驟中的 點擊時間-曝光時間差 得到 時間差
4.計算各個時間差的佔比

function etldata() {
    echo "start etldata"

    beeline -e "
		select 
		time
		,count
		,sum
		,sum(rate) over(rows between UNBOUNDED PRECEDING and CURRENT ROW)
		from(
			select
				time
				,count
				,sum
				,(count*100.00)/sum as rate
				from
				(
						select 
						time
						,count
						,sum(count) over() as sum
						from(
							select 
							(time+1)*30 as time
							,count(*) as count
							from 
							(
								select
								  click.req_id
								  ,click.rtime as ctime
								  ,expo.rtime as etime
								  ,floor((click.rtime-expo.rtime)/1000/60/30) as time
								  ,row_number() over(partition by expo.req_id) as rn
								  from (
										select rtime,req_id
										from tmp.ad_click_b
											where data_date=2020042406
								  ) click
								  left join 
									  (
										select rtime,req_id
										from tmp.ad_exposure_b
											where data_date>=2020042404 and data_date<=2020042406	
									  ) expo on click.req_id = expo.req_id
							)cli_epo where rn=1 group by time 
						)t2

				)t1 
		)t order by time asc
	 "

   echo "end etldata"
}

function main() {

    etldata
}

main

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章