hive 月活率-设备数统计-月活率统计

7 篇文章 0 订阅

IFNULL() 函数用于判断第一个表达式是否为 NULL,如果为 NULL 则返回第二个参数的值,如果不为 NULL 则返回第一个参数的值。

-- 使用数据库
use big12_umeng ;

-- 查询指定月份的活跃数
create or replace TEMPORARY view v1 as
select
  '${ym}' ,
  ifnull(tt.appid ,'NULLL') appid  ,
  ifnull(tt.appplatform ,'NULLL') appplatform  ,
  ifnull(tt.brand ,'NULLL') brand  ,
  ifnull(tt.devicestyle ,'NULLL') devicestyle  ,
  ifnull(tt.ostype ,'NULLL') ostype  ,
  ifnull(tt.appversion ,'NULLL') appversion  ,
  count(tt.deviceid) cnt   #设备总数 
FROM
(  <font color="red"#到tt那的</font>
  select
    t.appid ,
    t.appplatform,
    t.brand ,
    t.devicestyle,
    t.ostype ,
    t.appversion ,
    t.deviceid
  FROM
  (
    select
      appid ,
      appplatform,
      brand ,
      devicestyle,
      ostype ,
      appversion ,
      deviceid
    from
      appstartuplogs
    WHERE
    #ym <= '201805'  查询2018的5月份的数 
      ym = '${ym}'
    group BY
      appid ,
      appplatform,
      brand ,
      devicestyle,
      ostype ,
      appversion,
      deviceid
    with cube
  )t
  where
    t.appid is not null
    and t.deviceid is not null
)tt
group BY
  tt.appid ,
  tt.appplatform,
  tt.brand ,
  tt.devicestyle,
  tt.ostype ,
  tt.appversion
order by
  tt.appid ,
  tt.appplatform,
  tt.brand ,
  tt.devicestyle,
  tt.ostype ,
  tt.appversion;


-- 查询指定月份之前的设备数
create or replace TEMPORARY view v2 as
SELECT
  t.appid,
  ifnull(t.appplatform,'NULLL') appplatform ,
  ifnull(t.brand ,'NULLL') brand,
  ifnull(t.devicestyle,'NULLL') devicestyle,
  ifnull(t.ostype,'NULLL') ostype ,
  ifnull(t.appversion,'NULLL') appversion,
  count(t.deviceid) cnt
FROM
(
  select
      appid ,
      appplatform,
      brand ,
      devicestyle,
      ostype ,
      appversion,
      deviceid
  FROM
    appstartuplogs
  WHERE
    ym <= '${ym}'
  GROUP BY
      appid ,
      appplatform,
      brand ,
      devicestyle,
      ostype ,
      appversion,
      deviceid
      with cube
)t
WHERE
  t.appid is not null
  and t.deviceid is not NULL
group BY
  t.appid ,
  t.appplatform,
  t.brand ,
  t.devicestyle,
  t.ostype ,
  t.appversion
order BY
  t.appid ,
  t.appplatform,
  t.brand ,
  t.devicestyle,
  t.ostype ,
  t.appversion ;

-- 创建月活率表
create table if not exists stat_actratio_month(
  month string ,
  appid string,
  appplatform string,
  brand string ,
  devicestyle string,
  ostype string ,
  appversion string ,
  cnt float
)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY ','
lines terminated by '\n';

-- 查询指定月份的月活率
insert into stat_actratio_month
select
  '${ym}',
  v1.appid,
  v1.appplatform ,
  v1.brand ,
  v1.devicestyle,
  v1.ostype ,
  v1.appversion ,
  v1.cnt / v2.cnt
from
  v1 ,v2
where
  v1.appid = v2.appid
  and v1.appplatform = v2.appplatform
  and v1.brand = v2.brand
  and v1.devicestyle = v2.devicestyle
  and v1.ostype = v2.ostype
  and v1.appversion = v2.appversion ;

-- 输出结果
select * from stat_actratio_month

1、查询8月份各维度下的月活数 t1

use big12_umeng ;
create table if not exists stat_act_month(
  month string ,
  appid string,
  appplatform string,
  brand string ,
  devicestyle string,
  ostype string ,
  appversion string ,
  cnt int
)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY ','
lines terminated by '\n';

insert into table stat_act_month
select
#ym = formatbymonth(-1,'yyyyMM')
#着部分必须是活的 所以是动态传参,所有写个变量 
  '${ym}' ,
  tt.appid ,
  tt.appplatform,
  tt.brand ,
  tt.devicestyle,
  tt.ostype ,
  tt.appversion ,
  count(tt.deviceid)
FROM
(   #到tt那
  select
    t.appid ,
    t.appplatform,
    t.brand ,
    t.devicestyle,
    t.ostype ,
    t.appversion ,
    t.deviceid
  FROM
  (
    select
      appid ,
      appplatform,
      brand ,
      devicestyle,
      ostype ,
      appversion ,
      deviceid
    from
      appstartuplogs
    WHERE
      #ym = formatbymonth(-1,'yyyyMM')
      ym = '${ym}'
    group BY
      appid ,
      appplatform,
      brand ,
      devicestyle,
      ostype ,
      appversion,
      deviceid
    with cube
  )t
  where
    t.appid is not null
    and t.deviceid is not null
)tt
group BY
  tt.appid ,
  tt.appplatform,
  tt.brand ,
  tt.devicestyle,
  tt.ostype ,
  tt.appversion
order by
  tt.appid ,
  tt.appplatform,
  tt.brand ,
  tt.devicestyle,
  tt.ostype ,
  tt.appversion

在这里插入图片描述

2、查询8月份之前各数维度下的设备总数 t2

create or replace TEMPORARY view v2 as
SELECT
  t.appid,
  ifnull(t.appplatform,'NULLL') appplatform ,
  ifnull(t.brand ,'NULLL') brand,
  ifnull(t.devicestyle,'NULLL') devicestyle,
  ifnull(t.ostype,'NULLL') ostype ,
  ifnull(t.appversion,'NULLL') appversion,
  count(t.deviceid) cnt
FROM
(
  select
      appid ,
      appplatform,
      brand ,
      devicestyle,
      ostype ,
      appversion,
      deviceid
  FROM
    appstartuplogs
  WHERE
    ym <= '${ym}'
  GROUP BY
      appid ,
      appplatform,
      brand ,
      devicestyle,
      ostype ,
      appversion,
      deviceid
      with cube
)t
WHERE
  t.appid is not null
  and t.deviceid is not NULL
group BY
  t.appid ,
  t.appplatform,
  t.brand ,
  t.devicestyle,
  t.ostype ,
  t.appversion
order BY
  t.appid ,
  t.appplatform,
  t.brand ,
  t.devicestyle,
  t.ostype ,
  t.appversion ;

3、连接查询 ,按照相同的维度key,月活数 / 设备总数

-- 创建月活率表
create table if not exists stat_actratio_month(
  month string ,
  appid string,
  appplatform string,
  brand string ,
  devicestyle string,
  ostype string ,
  appversion string ,
  cnt float
)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY ','
lines terminated by '\n';
select
  '${ym}',
  v1.appid,
  v1.appplatform ,
  v1.brand ,
  v1.devicestyle,
  v1.ostype ,
  v1.appversion ,
  v1.cnt / v2.cnt
from
  v1 ,v2
where
  v1.appid = v2.appid
  and v1.appplatform = v2.appplatform
  and v1.brand = v2.brand
  and v1.devicestyle = v2.devicestyle
  and v1.ostype = v2.ostype
  and v1.appversion = v2.appversion ;

每天新老用户,日活,周活,月活的hive计算

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

伟伟哦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值