-- channel_ltv
--------------------
-- 可以优化
with retention_new_acc as
(
    select
        appid,app_name,
        case when account in ('wanxin996', 'qqq492864156','yueyou996') then '玩心'
             when account in ('13234701199', 'xiongDiwan', 'xiongdi996', '996xiongdi', 'cly225522') then '春秋玩'
             when account in ('tuantuan6400', 'whtuantuan', 'whzhangyou','whzhangyou1')then'安峰'
             when account in ('Jinri', 'zhangqian996') then '掌玩'
             when account in ('zlkj996') then '智灵'
             when account in ('ssyy1') then '盈优'
             when account in ('ky6709','wangsiqun') then '恺英'
             when account in ('hzxs0375','diaobao1') then '勋双'
             when account in ('nb1290733693', '9377yyeb', '9377syybyy') then '9377'
             when account in ('longxiang', 'zhanxingbo', '18659080001') then '龙翔'
             when account in ('13216396033', 'Zwzb2024', 'YP2024') then '玉盘'
             when account in ('tanwant2', 'tanwan996', 'tw996hz99', 'tw996hz66', 'tanlang996', '15868405282', 'tanwanjx') then '贪玩'
             when account in ('chongqu001','18968060998') and app_name ~ '【宠趣.*】|【87.*】' then '宠趣'
             when account in ('youyi888','youyi999') and app_name ~ '【YY.*】|【游奕.*】' then '游奕'
             when account in ('jinrui','huiqi996','996mailiang','HYXCX','masheng527')  then '慧跃'
             when account in ('biqiwan') then '常州万趣'
             when account in ('nxn0310') then '成都星楠祥'
             when account in ('youxinhuyu') then '成都游鑫'
             when account in ('a9527cc') then '东莞起量'
             when account in ('yiwan123') then '赣州易玩'
             when account in ('gzyw6772') then '广州悦玩'
             when account in ('YL15102760231') then     '海南岩岭'
             when account in ('145929L','hzqy9523') then '杭州乾赟'
             when account in ('simo123') then '杭州司墨'
             when account in ('zhaochengzhi') then '杭州速叶'
             when account in ('masheng527') then '杭州炫灿'
             when account in ('leijian4') then '湖北瓜玩'
             when account in ('jiangxicw2025') then     '江西超网'
             when account in ('xc188164') then     '江西热血'
             when account in ('zjzq3585') then     '临沂好汉'
             when account in ('lezhuyou') then     '青岛乐猪游'
             when account in ('quyou') then     '厦门趣游'
             when account in ('jishijiayou') then '上海纪世嘉游'
             when account in ('mita') then     '上海迷塔'
             when account in ('daimiaohudong') then     '四川呆喵'
             when account in ('whqujie2025') then '武汉趣界云图'
             when account in ('qfyw2024996') then '武汉全方易维'
             when account in ('wjyx') then     '武汉莴苣'
             when account in ('yansong') then '徐州木牛游马'
             when account in ('zjh641391114') then    '延边八七互娱'
             when app_name ~ '【.*?联运' then regexp_replace(app_name, '.*【(.*?)联运.*', '\1')
             when app_name !~ '【.*?联运' and app_name ~ '.*【(.*?)】.*' then regexp_replace(app_name, '.*【(.*?)】.*', '\1')
             when channel~'盒子' or channel~'996box' then '盒子'
        else '其他' end as co_oper_partner,
        case when channel~'盒子' or channel~'996box' then '盒子'
             when channel~'ai6' then 'ai6'
             when channel~'玩心' then '玩心'
             when channel~'应用宝' then '应用宝'
             when channel~'纸片' then '纸片'
             when channel~'团团' then '团团'
             when channel~'335玩' then '335玩'
             when channel~'朋克' then '朋克'
             when channel~'安徽追游' then '安徽追游'
             when channel~'冰火' then '冰火'
             when channel~'心乐' then '心乐'
             when channel ~*'华为|vivo|oppo|魅族|努比亚|联想|酷派|硬核联盟' then '硬核联盟'
             when channel~'9377'  then '9377'
             when channel~'智灵' then '智灵'
             when channel~'掌游' then '掌游'
             when channel~'上士' then '上士'
             when channel~'攒趣' then '攒趣'
             when channel~'掌玩' then '掌玩'
             when channel~'贪玩' then '贪玩'
             when channel~'wx'or channel~'WX'or channel~'微信' then '微信'
             when channel~'公众号'  then '公众号'
             when channel~'qq' or channel~'QQ' then 'QQ'
             --when channel~'quick' then 'quick'
             when channel~'七牛' then '七牛'
             when channel~'霸气' then '霸气'
             when channel~'xy' or channel~'XY' then 'XY'
             when channel~'芝麻互娱' then '芝麻互娱'
             when channel~'龙翔' then '龙翔'
             when channel~'布咕' then '布咕'
             when channel~'小柠互娱' then '小柠互娱'
             when channel~'春秋玩' then '春秋玩'
             when channel~'欢喜时代' then '欢喜时代'
             when channel~'上游' then '上游'
             when channel~'倾程' then '倾程'
             when channel~'绿洲' then '绿洲'
             when channel~'雷电' then '雷电'
             when channel~'点点' then '点点'
             when channel~'小七' then '小七'
             when channel~'瓜玩' then '瓜玩'
             when channel~'艺云' then '艺云'
             when channel~'冠游' then '冠游'
             when channel~'玄藏' then '玄藏'
             when channel~'羽龙' then '羽龙'
             when channel~'星云' then '星云'
             when channel~'爱尚游' then '爱尚游'
             when channel~'昊玩' then '昊玩'
             when channel~'8酷' then '8酷'
             when channel~'迅玩' then '迅玩'
             when channel~'星游' then '星游'
             when channel~'小米' then '小米'
             when channel~'趣城' then '趣城'
             when channel~'诺游' then '诺游'
             when channel~'洛谷' then '洛谷'
             when channel~'司墨' then '司墨'
             when channel~'脚印' then '脚印'
             when channel~'神游' then '神游'
             when channel~'拓乐' then '拓乐'
             when channel~'360' then '360'
             when channel~'日昇' then '日昇'
             when channel~'触点' then '触点'
             when channel~'冠游' then '冠游'
             when channel~'巴兔' then '巴兔'
             when channel~'天纵' then '天纵'
             when channel~'早游戏' then '早游戏'
             when channel~'爱趣' then '爱趣'
             when channel~'星楠祥' then '星楠祥'
             when channel~'贪游' then '贪游'
             when channel~'君游' then '君游'
             when channel~'996直播间' then '996直播间'
             when channel~'mumu' then 'mumu'
             when channel~'吉祥' then '吉祥直播间'
             when channel~'溪谷' then '溪谷'
             when channel~'优谷' then '优谷'
             when channel~'闪趣' then '闪趣'
             when channel~'8酷' then '8酷'
             when channel~'抖音' then '抖音'
             when channel~'斗鱼' then '斗鱼'
             when channel~'虎牙' then '虎牙'
             when channel~'巨量' then '巨量'
             when channel~'快手' then '快手'
             when channel~'爱奇艺' then '爱奇艺'
             when channel~'007' then '007'
             when channel~'圣宏' then '圣宏'
             when channel~'顺火' then '顺火'
             when channel~'飞火' then '飞火'
             when channel~'乐岛' then '乐岛'
             when channel~'传柒' then '传柒'
             when channel~'星际' then '星际'
             when channel~'玖趣' then '玖趣'
             when channel~*'ku25' then 'ku25'
             when channel~'8090' then '8090'
             when channel~*'appstore' then 'AppStore'
             when channel~'欢娱' then '欢娱'
             when channel~'逐梦' then '逐梦'
             when channel~'兴动' then '兴动'
             when channel~'3733' then '3733'
             when channel~'第一游戏' then '第一游戏'
             when channel~'公会' then '公会'
             when channel~'欢娱' then '欢娱'
             when channel~'335玩' then '335玩'
             when channel~*'4yx' then '4yx'
             when channel~'37' then '37'
             when channel~'开心' then '开心'
             when channel~*'ios' or channel~'苹果' then 'ios苹果端具体不详'
             when channel~'触点' then '触点'
             when channel~'游码' then '游码'
             when channel~*'45yx' then '45yx'
             when channel~'巨量' then '巨量'
             when channel~'小铃铛' then '小铃铛'
             when channel~'2345' then '2345'
             when channel~'梨子' then '梨子'
             when channel~'麦游' then '麦游'
             when channel~'群英' then '群英'
             when channel~'阅文' then '阅文'
             when channel~*'kopo' then 'kopo'
             when channel~'创启' then '创启'
             when channel~'4366' then '4366'
             when channel~'大楚' then '大楚'
             when channel~'巅峰' then '巅峰'
             when channel~'277' then '277'
             when channel~'112233' then '112233'
             when channel~'714' then '714'
             when channel~'4399' then '4399'
             when channel~'追忆' then '追忆'
             when channel~*'yy' then 'yy'
             when channel~'588' then '588'
             when channel~'八门' then '八门'
             when channel~*'pdd' then 'pdd'
             when channel~'9917' then '9917'
             when channel~'51517' then '51517'
             when channel~*'4yx' then '4yx'
             when channel~'好汉' then '好汉'
             when channel~'51' then '51'
             when channel~'葫芦侠' then '葫芦侠'
             when channel~'虫虫' then '虫虫'
             when channel~'跃游' then '跃游'
             when channel~'天天' then '天天'
             when channel~'66' then '66'
             when channel~'嘉禾' then '嘉禾'
             when channel~'爱游' then '爱游'
             when channel~'乐疯玩' then '乐疯玩'
             when channel~'游戏饭' then '游戏饭'
             when channel~'百度' then '百度'
         when channel~'UC' then 'UC'
         when channel~'网易' then '网易'
         when channel~'启示科创' then '启示科创'
         when channel~'抖音' then '抖音'
         when channel~'搜狗' then '搜狗'
         when channel~'移端' then '移端'
         when channel~'千易' then '千易'
         when channel~'应用市场' then '应用市场'
         when channel~'360' then '360'
         when channel~'07073' then '07073'
         when channel~'巨鳄' then '巨鳄'
         when channel~'星光斗聚' then '星光斗聚'
         when channel~'广点通' then '广点通'
         when channel~'热云' then '热云'
         when channel~'云刺' then '云刺'
         when channel~'财泰' then '财泰'
         when channel~'沈阳阅览' then '沈阳阅览'
         when channel~'新义' then '新义'
         when channel~'钰玺公众号' then '钰玺公众号'
         when channel~'蜀游' then '蜀游'
         when channel~'月象' then '月象'
         when channel~'云境' then '云境'
         when channel~'小米' then '小米'
         when channel~*'sigmob' then 'Sigmob'
         when channel~'睿初' then '睿初'
         when channel~'豌豆荚' then '豌豆荚'
         when channel~'橙益' then '橙益'
         when channel~'汉鸣' then '汉鸣'
         when channel~'因特云' then '因特云'
         when channel~'奕浪' then '奕浪'
         when channel~'鸿扬' then '鸿扬'
         when channel~'新宸睿' then '新宸睿'
         when channel~'翊宸' then '翊宸'
         when channel~'巨麦' then '巨麦'
         when channel~'2345' then '2345'
         when channel~'烈火封神网页' then '烈火封神网页'
         when channel~'北星' then '北星'
         when channel~'承宇齐光' then '承宇齐光'
         when channel~'微信' then '微信'
         when channel~'拾元立方' then '拾元立方'
         when channel~'头条' then '头条'
         when channel~'洛阳兰奇' then '洛阳兰奇'
         when channel~'金领' then '金领'
         when channel~'云游怀旧服' then '云游怀旧服'
         when channel~'自投' then '自投'
         when channel~'陕西米效' then '陕西米效'
         when channel~'芈兔兔' then '芈兔兔'
         when channel~'辣条山海传' then '辣条山海传'
         when channel~'钢蹦罗刹海超神器' then '钢蹦罗刹海超神器'
         when channel~'猫猫上古火龙' then '猫猫上古火龙'
         when channel~'苍云铭文沉默' then '苍云铭文沉默'
         when channel~'蓝奏' then '蓝奏'
         when channel~'三星' then '三星'
         when channel~'上海堡英' then '上海堡英'
         when channel~'短信' then '短信'
         when channel~'朋友圈' then '微信'
         when channel~'996争霸赛' then '996争霸赛'
         when channel~'华联' then '华联'
         when channel~'必应' then '必应'
         when channel~'野草' then '野草'
         when channel~'微博' then '微博'
         when channel~'依目' then '依目'
         when channel~'腾讯' then '腾讯'
         when channel~'去玩诸神沉默' then '去玩诸神沉默'
         when channel~'骏风古龙沉默' then '骏风古龙沉默'
         when channel~'去玩影月沉默' then '去玩影月沉默'
         when channel~'丰巢' then '丰巢'
         when channel~'云想冰雪' then '云想冰雪'
         when channel~'龙云至尊无限刀' then '龙云至尊无限刀'
         when channel~'蔚隆私域' then '蔚隆私域'
         when channel~'云游小极品' then '云游小极品'
         when channel~'珈蓝沉默' then '珈蓝沉默'
         when channel~'灵霜传奇' then '灵霜传奇'
         when channel~*'pp助手' then 'pp助手'
         when channel~'西瓜沉默超超超变' then '西瓜沉默超超超变'
         when channel~'白马火龙' then '白马火龙'
         when channel~'好看' then '好看'
         when channel~'戎马传奇' then '戎马传奇'
         when channel~'烛龙相逢沉默' then '烛龙相逢沉默'
         when channel~'99交易行' then '99交易行'
         when channel~'洪量' then '洪量'
         when channel~'小说定制包' then '小说定制包'
         when channel~'传奇手游' then '传奇手游'
         when channel~'九天传奇' then '九天传奇'
         when channel~'猛虎传奇3' then '猛虎传奇3'
         when channel~'巨灵传奇3' then '巨灵传奇3'
         when channel~'惊天传奇4' then '惊天传奇4'
         when channel~'墨府传奇4' then '墨府传奇4'
         when channel~'盛游火龙' then '盛游火龙'
         when channel~'中天传奇181' then '中天传奇181'
         when channel~'中天龙脉大陆' then '中天龙脉大陆'
         when channel~'潜龙传奇' then '潜龙传奇'
         when channel~'飞轩火龙' then '飞轩火龙'
         when channel~'净土沉默复古版' then '净土沉默复古版'
         when channel~'锋战传奇' then '锋战传奇'
         when channel~'归龙火龙' then '归龙火龙'
         when channel~'大楚传奇' then '大楚传奇'
         when channel~'春秋传奇' then '春秋传奇'
         when channel~'意意180轻变' then '意意180轻变'
         when channel~'巨灵传奇4怀旧版' then '巨灵传奇4怀旧版'
         when channel~'青花传奇' then '青花传奇'
         when channel~'流失用户回归包' then '流失用户回归包'
         when channel~'昆山创游' then '昆山创游'
         when channel~*'ubx' then 'UBX'
         when channel~'木之无尽沉默' then '木之无尽沉默'
         when channel~'品动灯火' then '品动灯火'
         when channel~'宗师传奇' then '宗师传奇'
         when channel~'小说排名公示' then '小说排名公示'
         when channel~'北京雨悦美通' then '北京雨悦美通'
         when channel~*'kook' then 'kook'
         when channel~'尚聚专属无限刀' then '尚聚专属无限刀'
         when channel~'优酷' then '优酷'
        else '其他' end as channel,
        event_time,user_id,ds::date as new_acc_dt
    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
    where rn=1
    and ds  BETWEEN '${startdate}' AND '${bizdate}'
),
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 BETWEEN '${startdate}' AND '${bizdate}'
    group by
        ds::date,user_id
),
retention_rd as
(
    select
        appid,app_name,co_oper_partner,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'
)
--渠道留存
INSERT INTO
    datasci.ads_996eco_index_ltv_m (index_type,data_month,channel,acc_count,ltv1,ltv7,ltv14,ltv30,ltv60,ltv90,ltv180)
select
    'channel_ltv' index_type,TO_CHAR(t1.new_acc_mon, 'YYYY-MM')  data_month,t1.channel,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
        channel,date_trunc('month',new_acc_dt)::date as new_acc_mon,uniq(user_id) as new_user_num
    from retention_rd
    where rd=0
    group by
        channel,date_trunc('month',new_acc_dt)::date
) t1
left join
(
    select
        channel,date_trunc('month',new_acc_dt)::date 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
        channel,date_trunc('month',new_acc_dt)::date
) t2 on t1.new_acc_mon=t2.new_acc_mon and t1.channel=t2.channel
;


