业务分析:hive下的分组求占比情况
在业务中与见一个占比情况,做了一个小demo,以供自己参考
元数据:
实现的形式
#方式一
with demo as (
select cookieid as cookieid,
pv as pv,
sum(pv) over() as total,
pv/sum(pv) over() percent
from pv_web
)
select cookieid,sum(pv),concat(round(sum(percent)*100,2),'%') from demo group by cookieid
#方式二
select s2.cookieid,s2.aa,sum(s2.aa) over() as bb
from(
select s1.cookieid,sum(s1.pv) as aa
from pv_web s1
group by s1.cookieid
) s2
#方式三:
with demo as (
select cookieid as cookieid,
sum(pv) as pv_total
from pv_web
group by cookieid)
select
cookieid,
pv_total,
sum(pv_total) over() as total,
pv_total/sum(pv_total) over() as percent
from demo
select
id,
amount,
create_date,
sum(amount)over(partition by id order by create_date) as amount1
from user_info
where dt='2016-09-20'
and id=76695
通过对id进行分区,按create_date进行排序,对amount进行叠加,
可以对用户消费金额做叠加处理,针对于判断用户什么时候达到某个消费阀值时,
会比较简便;