with retention_new_acc as
(
    select
        appid,app_name,channel,bind_game_id,
        event_time,user_id,ds::date as new_acc_dt
    from
    (
        select
            bind_game_id,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,channel,event_time,user_id,ds,gm.bind_game_id
            from public.ods_newly_account acc
            inner join
            (
                select id,name,bind_game_id
                from public.gm_apply
                where bind_game_type=1
            ) gm on acc.appid=gm.id
        ) t
    ) t1
    where rn=1
    and 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
        appid,app_name,channel,tg_play.tag as tg_play,tg_ver.tag as tg_ver,tg_value.tag as tg_value,tg_job.tag as tg_job,tg_feature.tag as tg_feature,
        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 outer join
    (select game_id,tag from public.game_tags where tag in ('神器','复古','沉默','火龙','合击','迷失','小极品','大极品','二合一','铭文','冰雪','宠物','战神','暗黑','元素','忘忧','BUFF','天罡'))tg_play
    on t1.bind_game_id=tg_play.game_id
    left outer join
    (select game_id,tag from public.game_tags where tag in ('1.70','1.76','1.80','1.85','1.95'))tg_ver
    on t1.bind_game_id=tg_ver.game_id
    left outer join
    (select game_id,tag from public.game_tags where tag in ('经典','微变','中变','超变'))tg_value
    on t1.bind_game_id=tg_value.game_id
    left outer join
    (select game_id,tag from public.game_tags where tag in ('单职业','三职业','多职业'))tg_job
    on t1.bind_game_id=tg_job.game_id
    left outer join
    (select game_id,tag from public.game_tags where tag in ('3D','开服1年','开服2年','开服3年','开服4年'))tg_feature
    on t1.bind_game_id=tg_feature.game_id
    where t2.dt-t1.new_acc_dt in (0,1,7,14,30,60,90,180)
    --t1.user_id='1419067566'
)

select
    t1.tg_play,t1.tg_ver,t1.tg_value,t1.tg_job,t1.tg_feature,t1.new_acc_mon,t1.new_user_num,t2.ret_1,ret_7,ret_14,ret_30,ret_60,ret_90,ret_180
from
(
    select
        coalesce(tg_play,'未知') as tg_play,coalesce(tg_ver,'未知') as tg_ver,coalesce(tg_value,'未知') as tg_value,coalesce(tg_job,'未知') as tg_job,coalesce(tg_feature,'未知') as tg_feature,
        date_trunc('month',new_acc_dt) as new_acc_mon,uniq(user_id) as new_user_num
    from retention_rd
    where rd=0
    group by
        coalesce(tg_play,'未知') ,coalesce(tg_ver,'未知') ,coalesce(tg_value,'未知') ,coalesce(tg_job,'未知') ,coalesce(tg_feature,'未知'),date_trunc('month',new_acc_dt)
) t1
left join
(
    select
        coalesce(tg_play,'未知') as tg_play,coalesce(tg_ver,'未知') as tg_ver,coalesce(tg_value,'未知') as tg_value,coalesce(tg_job,'未知') as tg_job,
        coalesce(tg_feature,'未知') as tg_feature,date_trunc('month',new_acc_dt) as new_acc_mon,
        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
        coalesce(tg_play,'未知') ,coalesce(tg_ver,'未知') ,coalesce(tg_value,'未知') ,coalesce(tg_job,'未知') ,coalesce(tg_feature,'未知'),date_trunc('month',new_acc_dt)
) t2 on t1.new_acc_mon=t2.new_acc_mon and t1.tg_play=t2.tg_play and t1.tg_ver=t2.tg_ver and t1.tg_value=t2.tg_value and t1.tg_job=t2.tg_job and t1.tg_feature=t2.tg_feature
limit 10000