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那的
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计算
https://blog.csdn.net/ddxygq/article/details/81391718