--bizdate 20250431 bizmonth 20250401  bizmonth

CREATE TABLE datasci.ads_996eco_index_ltv_d (
    index_type text,
    data_day text,
    is_box text,
    acc_count bigint,
    ltv0 bigint,
    ltv1 bigint,
    ltv2 bigint,
    ltv3 bigint,
    ltv4 bigint,
    ltv5 bigint,
    ltv6 bigint,
    ltv7 bigint,
    ltv8 bigint,
    ltv9 bigint,
    ltv10 bigint,
    ltv11 bigint,
    ltv12 bigint,
    ltv13 bigint,
    ltv14 bigint,
    ltv15 bigint,
    ltv16 bigint,
    ltv17 bigint,
    ltv18 bigint,
    ltv19 bigint,
    ltv20 bigint,
    ltv21 bigint,
    ltv22 bigint,
    ltv23 bigint,
    ltv24 bigint,
    ltv25 bigint,
    ltv26 bigint,
    ltv27 bigint,
    ltv28 bigint,
    ltv29 bigint,
    ltv30 bigint,
    ltv31 bigint,
    ltv45 bigint,
    ltv60 bigint,
    ltv90 bigint,
    ltv120 bigint
);

DELETE FROM datasci.ads_996eco_index_ltv_d WHERE 1=1;

--------------------

with retention_new_acc as
(
    select
        t1.appid,t1.app_name,t1.channel,t1.event_time,t1.user_id,t1.ds::date as new_acc_dt,coalesce(t2.user_flag,'非盒子用户') as user_flag
    from
    (
        select
            account,appid,app_name,channel,event_time,user_id,ds,row_number() over(partition by user_id order by event_time asc) as rn
        from
        (
            select
                appid,gm.name as app_name,account,name_abbr,channel,event_time,user_id,ds
            from public.ods_newly_account acc
            inner join
            (
                select id,name,bind_game_id,a.account,b.name_abbr
                from public.gm_apply a
                left join public.dim_gm_info b on a.account=b.account
                where bind_game_type=1
            ) gm on acc.appid=gm.id
        ) t
    ) t1
    left join datasci.dws_plt_newly_account_d t2 on t1.user_id=t2.user_id
    where rn=1
    and t1.ds between '${hisdate}' and '${bizdate}'
    --and user_id='1419067566'
),
retention_active_detail as
(
    select
        ds::date as dt,user_id
    from public.ods_active_account t1
    inner join
    (
        select id,name,bind_game_id
        from public.gm_apply
        where bind_game_type=1
    ) t2 on t1.appid=t2.id
    where t1.ds>='${hisdate}' and t1.ds<=to_char(dateadd('${bizdate}'::date,31,'dd'),'yyyymmdd')
    group by
        ds::date,user_id
),
retention_rd as
(
    select
        user_flag,appid,app_name,channel,event_time,t1.user_id,new_acc_dt,t2.dt as act_dt,t2.dt-t1.new_acc_dt as rd
    from retention_new_acc t1
    left join retention_active_detail t2 on t1.user_id=t2.user_id
    --left join 加玩法版本啥的
    where t2.dt-t1.new_acc_dt in (0,1,7,14,30,60,90,180)
    --t1.user_id='1419067566'
)
--渠道留存
SELECT
    'is_box_ltv' index_type,
    t1.new_acc_dt data_day,
    t1.user_flag is_box,
    t1.new_user_num acc_count,
    t2.ret_1 ltv1,
    ret_7 ltv7,
    ret_14 ltv14,
    ret_30 ltv30,
    ret_60 ltv60,
    ret_90 ltv90,
    ret_180 ltv180
FROM
(
    select
        user_flag ,new_acc_dt,uniq(user_id) as new_user_num
    from retention_rd
    where rd=0
    group by
        user_flag,new_acc_dt
) t1
left join
(
    select
        user_flag,new_acc_dt,
        uniq(case when rd=1 then user_id else null end) as ret_1,
        uniq(case when rd=7 then user_id else null end) as ret_7,
        uniq(case when rd=14 then user_id else null end) as ret_14,
        uniq(case when rd=30 then user_id else null end) as ret_30,
        uniq(case when rd=60 then user_id else null end) as ret_60,
        uniq(case when rd=90 then user_id else null end) as ret_90,
        uniq(case when rd=180 then user_id else null end) as ret_180
    from retention_rd
    where rd>0
    group by
        user_flag,new_acc_dt
) t2 on t1.new_acc_dt=t2.new_acc_dt and t1.user_flag=t2.user_flag
;

