数据仓库 本周回流用户数

回流用户:上周未启动过应用,本周启动了应用的用户。

实现思路:本周回流=本周活跃-本周新增-上周活跃。

依赖数据:dws_uv_detail_wk(每周活跃设备明细表),dws_new_mid_day(每日新增设备明细表)。

建表语句:

drop table if exists ads_back_count;
create external table ads_back_count( 
    `dt` string COMMENT '统计日期',
    `wk_dt` string COMMENT '统计日期所在周',
    `wastage_count` bigint COMMENT '回流设备数'
) 
row format delimited fields terminated by '	'
location '/warehouse/gmall/ads/ads_back_count';

数据导入脚本:

①t1 从周活表中查出本周活跃的用户。

②t2 从每日新增表中查出本周的新增用户。

③t3 从周活表中查出上周的活跃用户。

④where t2.mid_id is null and t3.mid_id is null 表示去掉本周新增的和上周活跃的用户。

#!/bin/bash

if [ -n "$1" ];then
    do_date=$1
else
    do_date=`date -d "-1 day" +%F`
fi

hive=/opt/module/hive/bin/hive
APP=gmall

echo "-----------导入日期$do_date-----------"

sql="
insert into table "$APP".ads_back_count
select 
       '$do_date' dt,
       concat(date_add(next_day('$do_date','MO'),-7),'_',date_add(next_day('$do_date','MO'),-1)) wk_dt,
       count(*)
from 
(
    select t1.mid_id
    from 
    (
        select mid_id
        from "$APP".dws_uv_detail_wk
        where wk_dt=concat(date_add(next_day('$do_date','MO'),-7),'_',date_add(next_day('$do_date','MO'),-1))
    )t1
    left join
    (
        select mid_id
        from "$APP".dws_new_mid_day
        where create_date<=date_add(next_day('$do_date','MO'),-1) and create_date>=date_add(next_day('$do_date','MO'),-7)
    )t2
    on t1.mid_id=t2.mid_id
    left join
    (
        select mid_id
        from "$APP".dws_uv_detail_wk
        where wk_dt=concat(date_add(next_day('$do_date','MO'),-7*2),'_',date_add(next_day('$do_date','MO'),-7-1))
    )t3
    on t1.mid_id=t3.mid_id
    where t2.mid_id is null and t3.mid_id is null
)t4;
"

$hive -e "$sql"
原文地址:https://www.cnblogs.com/noyouth/p/13207771.html