DELETE FROM datasci.ads_996eco_index_ltv_m WHERE 1=1; -- 从24年1月到现在 数据量无法做增量 -------------------- 1 --LTV WITH ga AS ( SELECT id, name, bind_game_id FROM public.gm_apply WHERE bind_type = 1 AND bind_game_type = 1 ), po AS ( SELECT appid, ds, user_id, SUM(amount) / 100 AS pay_amount FROM public.ods_payment_order WHERE status = 1 AND ds::date BETWEEN '2024-10-01' AND CURRENT_DATE - INTERVAL'1day' GROUP BY appid, ds, user_id ), na AS ( SELECT ds, appid, case when device_type ~*'h5' then 'H5' when device_type ~*'pc' then 'PC' when device_type ~*'ios' then 'IOS' when device_type ~*'mac' then 'mac' when device_type ~*'android' then 'Android' when device_type ~*'harmony' then '鸿蒙' else '其他' end AS re_device_type, user_id FROM public.ods_newly_account WHERE 1=1 AND ds::date BETWEEN '2024-10-01' AND '2025-03-31' ), base_data AS (SELECT na.ds::date AS cohort_date, na.appid, ga.name AS appname, na.re_device_type, na.user_id, po.pay_amount, po.ds::date - na.ds::date AS n_day FROM na INNER JOIN ga ON na.appid = ga.id LEFT JOIN po ON na.appid = po.appid AND na.user_id = po.user_id AND po.ds::date BETWEEN na.ds::date AND na.ds::date + 120 ) SELECT 're_device_ltv' index_type, re_device_type, appid, UNIQ(user_id) AS acc_count, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 0 AND n_day <= 0 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV0, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 1 AND n_day <= 1 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV1, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 2 AND n_day <= 2 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV2, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 3 AND n_day <= 3 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV3, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 4 AND n_day <= 4 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV4, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 5 AND n_day <= 5 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV5, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 6 AND n_day <= 6 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV6, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 7 AND n_day <= 7 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV7, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 8 AND n_day <= 8 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV8, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 9 AND n_day <= 9 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV9, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 10 AND n_day <= 10 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV10, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 11 AND n_day <= 11 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV11, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 12 AND n_day <= 12 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV12, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 13 AND n_day <= 13 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV13, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 14 AND n_day <= 14 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV14, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 15 AND n_day <= 15 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV15, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 16 AND n_day <= 16 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV16, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 17 AND n_day <= 17 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV17, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 18 AND n_day <= 18 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV18, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 19 AND n_day <= 19 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV19, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 20 AND n_day <= 20 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV20, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 21 AND n_day <= 21 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV21, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 22 AND n_day <= 22 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV22, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 23 AND n_day <= 23 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV23, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 24 AND n_day <= 24 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV24, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 25 AND n_day <= 25 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV25, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 26 AND n_day <= 26 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV26, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 27 AND n_day <= 27 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV27, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 28 AND n_day <= 28 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV28, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 29 AND n_day <= 29 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV29, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 30 AND n_day <= 30 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV30, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 31 AND n_day <= 31 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV31, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 45 AND n_day <= 45 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV45, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 60 AND n_day <= 60 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV60, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 90 AND n_day <= 90 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV90, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 120 AND n_day <= 120 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV120 FROM base_data -- WHERE play_tag IS NOT NULL GROUP BY appid, re_device_type ; -------------------- -- 从24年1月到现在 数据量无法做增量 WITH ga AS ( -- 筛选传奇类产品的appid SELECT id, name, bind_game_id FROM public.gm_apply WHERE bind_type = 1 AND bind_game_type = 1 ), ib AS ( SELECT user_id FROM public.ods_newly_account WHERE appid = 1015 GROUP BY user_id ), po AS ( -- 付费基础表 SELECT appid, ds, user_id, SUM(amount) / 100 AS pay_amount FROM public.ods_payment_order WHERE status = 1 AND ds::date BETWEEN '2024-01-01' AND CURRENT_DATE - INTERVAL'1DAY' GROUP BY appid, ds, user_id ), na AS ( -- 新增基础表 SELECT ds, appid, user_id FROM public.ods_newly_account WHERE 1=1 AND ds::date BETWEEN '2024-01-01' AND CURRENT_DATE - INTERVAL'1DAY' ) , base_data AS ( -- 联合计算新增人数和后续付费及间隔天数 SELECT na.ds::date AS cohort_date, '盒子' AS is_box, na.appid, po.ds::date - na.ds::date AS n_day, na.user_id, po.pay_amount FROM na INNER JOIN ga ON na.appid = ga.id LEFT JOIN po ON na.appid = po.appid AND na.user_id = po.user_id AND po.ds::date >= na.ds::date -- 避免出现负数天 WHERE EXISTS( SELECT 1 FROM ib WHERE na.user_id = ib.user_id ) UNION ALL SELECT na.ds::date AS cohort_date, '非盒子' AS is_box, na.appid, po.ds::date - na.ds::date AS n_day, na.user_id, po.pay_amount FROM na INNER JOIN ga ON na.appid = ga.id LEFT JOIN po ON na.appid = po.appid AND na.user_id = po.user_id AND po.ds::date >= na.ds::date WHERE NOT EXISTS( SELECT 1 FROM ib WHERE na.user_id = ib.user_id ) ), ltv_base AS ( -- 计算基于appid的LTV SELECT TO_CHAR(DATE_TRUNC('month', cohort_date), 'YYYY-MM') AS data_month, is_box, appid, UNIQ(user_id) AS acc_count, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 0 AND n_day <= 0 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv0, -- 第一个判断是避免天数不够不应该有数却有,第二个判断是计算在对应天数内所有付费金额,下同 SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 1 AND n_day <= 1 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv1, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 2 AND n_day <= 2 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv2, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 3 AND n_day <= 3 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv3, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 4 AND n_day <= 4 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv4, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 5 AND n_day <= 5 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv5, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 6 AND n_day <= 6 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv6, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 7 AND n_day <= 7 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv7, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 8 AND n_day <= 8 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv8, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 9 AND n_day <= 9 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv9, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 10 AND n_day <= 10 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv10, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 11 AND n_day <= 11 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv11, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 12 AND n_day <= 12 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv12, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 13 AND n_day <= 13 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv13, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 14 AND n_day <= 14 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv14, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 15 AND n_day <= 15 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv15, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 16 AND n_day <= 16 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv16, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 17 AND n_day <= 17 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv17, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 18 AND n_day <= 18 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv18, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 19 AND n_day <= 19 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv19, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 20 AND n_day <= 20 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv20, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 21 AND n_day <= 21 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv21, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 22 AND n_day <= 22 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv22, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 23 AND n_day <= 23 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv23, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 24 AND n_day <= 24 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv24, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 25 AND n_day <= 25 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv25, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 26 AND n_day <= 26 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv26, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 27 AND n_day <= 27 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv27, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 28 AND n_day <= 28 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv28, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 29 AND n_day <= 29 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv29, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 30 AND n_day <= 30 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv30, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 31 AND n_day <= 31 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv31, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 45 AND n_day <= 45 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv45, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 60 AND n_day <= 60 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv60, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 90 AND n_day <= 90 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv90, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 120 AND n_day <= 120 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv120 FROM base_data GROUP BY 1, 2, 3) SELECT 'avg_ltv' index_type, data_month, is_box, AVG(acc_count) AS avg_new, -- 基于要求,取每个appid的平均值 AVG(ltv0) AS ltv0, AVG(ltv1) AS ltv1, AVG(ltv2) AS ltv2, AVG(ltv3) AS ltv3, AVG(ltv4) AS ltv4, AVG(ltv5) AS ltv5, AVG(ltv6) AS ltv6, AVG(ltv7) AS ltv7, AVG(ltv8) AS ltv8, AVG(ltv9) AS ltv9, AVG(ltv10) AS ltv10, AVG(ltv11) AS ltv11, AVG(ltv12) AS ltv12, AVG(ltv13) AS ltv13, AVG(ltv14) AS ltv14, AVG(ltv15) AS ltv15, AVG(ltv16) AS ltv16, AVG(ltv17) AS ltv17, AVG(ltv18) AS ltv18, AVG(ltv19) AS ltv19, AVG(ltv20) AS ltv20, AVG(ltv21) AS ltv21, AVG(ltv22) AS ltv22, AVG(ltv23) AS ltv23, AVG(ltv24) AS ltv24, AVG(ltv25) AS ltv25, AVG(ltv26) AS ltv26, AVG(ltv27) AS ltv27, AVG(ltv28) AS ltv28, AVG(ltv29) AS ltv29, AVG(ltv30) AS ltv30, AVG(ltv31) AS ltv31, AVG(ltv45) AS ltv45, AVG(ltv60) AS ltv60, AVG(ltv90) AS ltv90, AVG(ltv120) AS ltv120 FROM ltv_base WHERE acc_count > 100 -- 此处是随便拍的一个避免新增人数过少导致LTV受影响较大的阈值,可根据实际情况调整(从600改成了100) GROUP BY 1, 2, 3; -------------------- -- 可以优化 with retention_new_acc as ( select appid,app_name, case when channel~'盒子' or channel~'996box' then '盒子' 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='zlkj996' then '智灵' when account='ssyy1' then '盈优' when account in ('ky6709') then '恺英' when account in ('hzxs0375') then '勋双' when account in ('nb1290733693','9377yyeb','9377syybyy') then '9377' when account in ('tanwangm', 'tanwan996', 'tw996hz99', 'tw996hz66', 'tanlang996', '15868405282', 'tanwanjx') then '贪玩' when account in ('longxiang','zhanxingbo','18659080001') then '龙翔' when account in ('13216396033','Zwzb2024','YP2024') 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','huiqi2996') then '慧跃' when app_name ~ '【.*?联运' then REGEXP_REPLACE(app_name, '.*【(.*?)联运.*', '\1') when app_name !~ '【.*?联运' and app_name ~ '.*【(.*?)】.*' then REGEXP_REPLACE(app_name, '.*【(.*?)】.*', '\1') 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 '${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,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' ) ----联运商留存 select 'coop_ltv' index_type, TO_CHAR(t1.new_acc_mon, 'YYYY-MM') data_month, t1.co_oper_partner coop, 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 co_oper_partner,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 co_oper_partner,date_trunc('month',new_acc_dt)::date ) t1 left join ( select co_oper_partner, 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 co_oper_partner,date_trunc('month',new_acc_dt)::date ) t2 on t1.new_acc_mon=t2.new_acc_mon and t1.co_oper_partner=t2.co_oper_partner ; -------------------- -- 可以优化 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 '${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,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' ) --渠道留存 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 ; -------------------- -- 从24年10月到现在 数据量无法做增量 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 'channel_ltv' index_type,TO_CHAR(t1.new_acc_mon, 'YYYY-MM') data_month ,t1.tg_play,t1.tg_ver,t1.tg_value,t1.tg_job,t1.tg_feature,t1.new_user_num, t2.ret_1 ltv1,ret_7 ltv7,ret_14 ltv14,ret_30 ltv30,ret_60 ltv60,ret_90 ltv90,ret_180 ltv180 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 ; WITH ga AS ( SELECT id, name, bind_game_id FROM public.gm_apply WHERE bind_type = 1 AND bind_game_type = 1 ), tg AS ( SELECT game_id, string_agg(DISTINCT CASE WHEN tag in ('神器','复古','沉默','火龙','合击','迷失','小极品','大极品','二合一','铭文','冰雪','宠物','战神','暗黑','元素','忘忧','BUFF','天罡') THEN tag ELSE NULL END,',') AS play_tag, string_agg(DISTINCT CASE WHEN tag in ('1.70','1.76','1.80','1.85','1.95')THEN tag ELSE NULL END, ',') AS ver_tag, string_agg(DISTINCT CASE WHEN tag in ('经典','微变','中变','超变') THEN tag ELSE NULL END,',') AS value_tag, string_agg(DISTINCT CASE WHEN tag in ('单职业','三职业','多职业') THEN tag ELSE NULL END,',') AS job_tag, string_agg(DISTINCT CASE WHEN tag in ('3D','开服1年','开服2年','开服3年','开服4年') THEN tag ELSE NULL END,',') AS feature_tag FROM public.game_tags GROUP BY game_id ), po AS ( SELECT appid, ds, user_id, SUM(amount) / 100 AS pay_amount FROM public.ods_payment_order WHERE status = 1 AND ds::date BETWEEN '2024-10-01' AND current_date - interval'1day' GROUP BY appid, ds, user_id ), na AS ( SELECT ds, appid, user_id FROM public.ods_newly_account WHERE 1=1 AND ds::date BETWEEN '2024-10-01' AND '2025-03-31' ), base_data AS (SELECT na.ds::date AS cohort_date, na.appid, ga.name AS appname, tg.play_tag, tg.ver_tag, tg.value_tag, tg.job_tag, tg.feature_tag, na.user_id, po.pay_amount, po.ds::date - na.ds::date AS n_day FROM na INNER JOIN ga ON na.appid = ga.id LEFT JOIN po ON na.appid = po.appid AND na.user_id = po.user_id AND po.ds::date BETWEEN na.ds::date AND na.ds::date + 120 LEFT JOIN tg ON ga.bind_game_id = tg.game_id), ltv_base AS ( SELECT TO_CHAR(DATE_TRUNC('month', cohort_date), 'YYYY-MM') AS data_month, appid, play_tag, -- appname, UNIQ(user_id) AS new_cnt, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 0 AND n_day <= 0 THEN pay_amount ELSE NULL END) AS amount0, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 1 AND n_day <= 1 THEN pay_amount ELSE NULL END) AS amount1, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 2 AND n_day <= 2 THEN pay_amount ELSE NULL END) AS amount2, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 3 AND n_day <= 3 THEN pay_amount ELSE NULL END) AS amount3, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 4 AND n_day <= 4 THEN pay_amount ELSE NULL END) AS amount4, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 5 AND n_day <= 5 THEN pay_amount ELSE NULL END) AS amount5, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 6 AND n_day <= 6 THEN pay_amount ELSE NULL END) AS amount6, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 7 AND n_day <= 7 THEN pay_amount ELSE NULL END) AS amount7, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 8 AND n_day <= 8 THEN pay_amount ELSE NULL END) AS amount8, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 9 AND n_day <= 9 THEN pay_amount ELSE NULL END) AS amount9, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 10 AND n_day <= 10 THEN pay_amount ELSE NULL END) AS amount10, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 11 AND n_day <= 11 THEN pay_amount ELSE NULL END) AS amount11, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 12 AND n_day <= 12 THEN pay_amount ELSE NULL END) AS amount12, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 13 AND n_day <= 13 THEN pay_amount ELSE NULL END) AS amount13, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 14 AND n_day <= 14 THEN pay_amount ELSE NULL END) AS amount14, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 15 AND n_day <= 15 THEN pay_amount ELSE NULL END) AS amount15, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 16 AND n_day <= 16 THEN pay_amount ELSE NULL END) AS amount16, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 17 AND n_day <= 17 THEN pay_amount ELSE NULL END) AS amount17, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 18 AND n_day <= 18 THEN pay_amount ELSE NULL END) AS amount18, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 19 AND n_day <= 19 THEN pay_amount ELSE NULL END) AS amount19, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 20 AND n_day <= 20 THEN pay_amount ELSE NULL END) AS amount20, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 21 AND n_day <= 21 THEN pay_amount ELSE NULL END) AS amount21, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 22 AND n_day <= 22 THEN pay_amount ELSE NULL END) AS amount22, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 23 AND n_day <= 23 THEN pay_amount ELSE NULL END) AS amount23, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 24 AND n_day <= 24 THEN pay_amount ELSE NULL END) AS amount24, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 25 AND n_day <= 25 THEN pay_amount ELSE NULL END) AS amount25, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 26 AND n_day <= 26 THEN pay_amount ELSE NULL END) AS amount26, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 27 AND n_day <= 27 THEN pay_amount ELSE NULL END) AS amount27, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 28 AND n_day <= 28 THEN pay_amount ELSE NULL END) AS amount28, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 29 AND n_day <= 29 THEN pay_amount ELSE NULL END) AS amount29, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 30 AND n_day <= 30 THEN pay_amount ELSE NULL END) AS amount30, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 31 AND n_day <= 31 THEN pay_amount ELSE NULL END) AS amount31, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 45 AND n_day <= 45 THEN pay_amount ELSE NULL END) AS amount45, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 60 AND n_day <= 60 THEN pay_amount ELSE NULL END) AS amount60, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 90 AND n_day <= 90 THEN pay_amount ELSE NULL END) AS amount90, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 120 AND n_day <= 120 THEN pay_amount ELSE NULL END) AS amount120, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 0 AND n_day <= 0 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV0, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 1 AND n_day <= 1 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV1, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 2 AND n_day <= 2 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV2, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 3 AND n_day <= 3 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV3, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 4 AND n_day <= 4 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV4, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 5 AND n_day <= 5 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV5, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 6 AND n_day <= 6 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV6, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 7 AND n_day <= 7 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV7, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 8 AND n_day <= 8 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV8, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 9 AND n_day <= 9 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV9, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 10 AND n_day <= 10 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV10, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 11 AND n_day <= 11 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV11, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 12 AND n_day <= 12 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV12, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 13 AND n_day <= 13 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV13, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 14 AND n_day <= 14 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV14, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 15 AND n_day <= 15 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV15, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 16 AND n_day <= 16 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV16, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 17 AND n_day <= 17 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV17, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 18 AND n_day <= 18 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV18, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 19 AND n_day <= 19 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV19, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 20 AND n_day <= 20 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV20, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 21 AND n_day <= 21 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV21, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 22 AND n_day <= 22 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV22, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 23 AND n_day <= 23 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV23, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 24 AND n_day <= 24 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV24, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 25 AND n_day <= 25 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV25, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 26 AND n_day <= 26 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV26, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 27 AND n_day <= 27 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV27, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 28 AND n_day <= 28 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV28, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 29 AND n_day <= 29 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV29, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 30 AND n_day <= 30 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV30, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 31 AND n_day <= 31 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV31, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 45 AND n_day <= 45 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV45, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 60 AND n_day <= 60 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV60, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 90 AND n_day <= 90 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV90, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 120 AND n_day <= 120 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS LTV120 FROM base_data WHERE play_tag IS NOT NULL GROUP BY GROUPING SETS((data_month, appid, play_tag), (appid, play_tag))) SELECT 'play_tag_ltv' index_type, data_month, play_tag, AVG(new_cnt) AS acc_count, AVG(ltv0) AS ltv0, AVG(ltv1) AS ltv1, AVG(ltv2) AS ltv2, AVG(ltv3) AS ltv3, AVG(ltv4) AS ltv4, AVG(ltv5) AS ltv5, AVG(ltv6) AS ltv6, AVG(ltv7) AS ltv7, AVG(ltv8) AS ltv8, AVG(ltv9) AS ltv9, AVG(ltv10) AS ltv10, AVG(ltv11) AS ltv11, AVG(ltv12) AS ltv12, AVG(ltv13) AS ltv13, AVG(ltv14) AS ltv14, AVG(ltv15) AS ltv15, AVG(ltv16) AS ltv16, AVG(ltv17) AS ltv17, AVG(ltv18) AS ltv18, AVG(ltv19) AS ltv19, AVG(ltv20) AS ltv20, AVG(ltv21) AS ltv21, AVG(ltv22) AS ltv22, AVG(ltv23) AS ltv23, AVG(ltv24) AS ltv24, AVG(ltv25) AS ltv25, AVG(ltv26) AS ltv26, AVG(ltv27) AS ltv27, AVG(ltv28) AS ltv28, AVG(ltv29) AS ltv29, AVG(ltv30) AS ltv30, AVG(ltv31) AS ltv31, AVG(ltv45) AS ltv45, AVG(ltv60) AS ltv60, AVG(ltv90) AS ltv90, AVG(ltv120) AS ltv120 FROM ltv_base WHERE new_cnt > 100 GROUP BY 1, 2, 3; WITH ga AS ( SELECT id, name, bind_game_id FROM public.gm_apply WHERE bind_type = 1 AND bind_game_type = 1 ), tg AS ( SELECT game_id, string_agg(DISTINCT CASE WHEN tag in ('神器','复古','沉默','火龙','合击','迷失','小极品','大极品','二合一','铭文','冰雪','宠物','战神','暗黑','元素','忘忧','BUFF','天罡') THEN tag ELSE NULL END,',') AS play_tag, string_agg(DISTINCT CASE WHEN tag in ('1.70','1.76','1.80','1.85','1.95')THEN tag ELSE NULL END, ',') AS ver_tag, string_agg(DISTINCT CASE WHEN tag in ('经典','微变','中变','超变') THEN tag ELSE NULL END,',') AS value_tag, string_agg(DISTINCT CASE WHEN tag in ('单职业','三职业','多职业') THEN tag ELSE NULL END,',') AS job_tag, string_agg(DISTINCT CASE WHEN tag in ('3D','开服1年','开服2年','开服3年','开服4年') THEN tag ELSE NULL END,',') AS feature_tag FROM public.game_tags GROUP BY game_id ), po AS ( SELECT appid, ds, user_id, SUM(amount) / 100 AS pay_amount FROM public.ods_payment_order WHERE status = 1 AND ds::date BETWEEN '2024-10-01' AND current_date - interval'1day' GROUP BY appid, ds, user_id ), na AS ( SELECT ds, appid, user_id FROM public.ods_newly_account WHERE 1=1 AND ds::date BETWEEN '2024-10-01' AND '2025-03-31' ), base_data AS (SELECT na.ds::date AS cohort_date, na.appid, ga.name AS appname, tg.play_tag, tg.ver_tag, tg.value_tag, tg.job_tag, tg.feature_tag, na.user_id, po.pay_amount, po.ds::date - na.ds::date AS n_day FROM na INNER JOIN ga ON na.appid = ga.id LEFT JOIN po ON na.appid = po.appid AND na.user_id = po.user_id AND po.ds::date BETWEEN na.ds::date AND na.ds::date + 120 LEFT JOIN tg ON ga.bind_game_id = tg.game_id) SELECT 'play_tag_amount_ltv' index_type, TO_CHAR(DATE_TRUNC('month', cohort_date), 'YYYY-MM') AS data_month, appid, play_tag, -- appname, UNIQ(user_id) AS new_cnt, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 0 AND n_day <= 0 THEN pay_amount ELSE NULL END) AS ltv0, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 1 AND n_day <= 1 THEN pay_amount ELSE NULL END) AS ltv1, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 2 AND n_day <= 2 THEN pay_amount ELSE NULL END) AS ltv2, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 3 AND n_day <= 3 THEN pay_amount ELSE NULL END) AS ltv3, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 4 AND n_day <= 4 THEN pay_amount ELSE NULL END) AS ltv4, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 5 AND n_day <= 5 THEN pay_amount ELSE NULL END) AS ltv5, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 6 AND n_day <= 6 THEN pay_amount ELSE NULL END) AS ltv6, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 7 AND n_day <= 7 THEN pay_amount ELSE NULL END) AS ltv7, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 8 AND n_day <= 8 THEN pay_amount ELSE NULL END) AS ltv8, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 9 AND n_day <= 9 THEN pay_amount ELSE NULL END) AS ltv9, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 10 AND n_day <= 10 THEN pay_amount ELSE NULL END) AS ltv10, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 11 AND n_day <= 11 THEN pay_amount ELSE NULL END) AS ltv11, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 12 AND n_day <= 12 THEN pay_amount ELSE NULL END) AS ltv12, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 13 AND n_day <= 13 THEN pay_amount ELSE NULL END) AS ltv13, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 14 AND n_day <= 14 THEN pay_amount ELSE NULL END) AS ltv14, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 15 AND n_day <= 15 THEN pay_amount ELSE NULL END) AS ltv15, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 16 AND n_day <= 16 THEN pay_amount ELSE NULL END) AS ltv16, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 17 AND n_day <= 17 THEN pay_amount ELSE NULL END) AS ltv17, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 18 AND n_day <= 18 THEN pay_amount ELSE NULL END) AS ltv18, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 19 AND n_day <= 19 THEN pay_amount ELSE NULL END) AS ltv19, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 20 AND n_day <= 20 THEN pay_amount ELSE NULL END) AS ltv20, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 21 AND n_day <= 21 THEN pay_amount ELSE NULL END) AS ltv21, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 22 AND n_day <= 22 THEN pay_amount ELSE NULL END) AS ltv22, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 23 AND n_day <= 23 THEN pay_amount ELSE NULL END) AS ltv23, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 24 AND n_day <= 24 THEN pay_amount ELSE NULL END) AS ltv24, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 25 AND n_day <= 25 THEN pay_amount ELSE NULL END) AS ltv25, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 26 AND n_day <= 26 THEN pay_amount ELSE NULL END) AS ltv26, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 27 AND n_day <= 27 THEN pay_amount ELSE NULL END) AS ltv27, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 28 AND n_day <= 28 THEN pay_amount ELSE NULL END) AS ltv28, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 29 AND n_day <= 29 THEN pay_amount ELSE NULL END) AS ltv29, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 30 AND n_day <= 30 THEN pay_amount ELSE NULL END) AS ltv30, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 31 AND n_day <= 31 THEN pay_amount ELSE NULL END) AS ltv31, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 45 AND n_day <= 45 THEN pay_amount ELSE NULL END) AS ltv45, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 60 AND n_day <= 60 THEN pay_amount ELSE NULL END) AS ltv60, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 90 AND n_day <= 90 THEN pay_amount ELSE NULL END) AS ltv90, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 120 AND n_day <= 120 THEN pay_amount ELSE NULL END) AS ltv120 FROM base_data WHERE play_tag IS NOT NULL GROUP BY GROUPING SETS((data_month, appid, play_tag), (appid, play_tag)) UNION ALL SELECT 'play_tag_ltv' index_type, TO_CHAR(DATE_TRUNC('month', cohort_date), 'YYYY-MM') AS data_month, appid, play_tag, -- appname, UNIQ(user_id) AS new_cnt, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 0 AND n_day <= 0 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv0, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 1 AND n_day <= 1 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv1, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 2 AND n_day <= 2 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv2, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 3 AND n_day <= 3 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv3, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 4 AND n_day <= 4 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv4, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 5 AND n_day <= 5 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv5, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 6 AND n_day <= 6 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv6, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 7 AND n_day <= 7 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv7, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 8 AND n_day <= 8 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv8, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 9 AND n_day <= 9 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv9, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 10 AND n_day <= 10 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv10, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 11 AND n_day <= 11 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv11, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 12 AND n_day <= 12 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv12, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 13 AND n_day <= 13 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv13, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 14 AND n_day <= 14 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv14, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 15 AND n_day <= 15 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv15, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 16 AND n_day <= 16 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv16, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 17 AND n_day <= 17 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv17, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 18 AND n_day <= 18 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv18, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 19 AND n_day <= 19 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv19, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 20 AND n_day <= 20 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv20, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 21 AND n_day <= 21 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv21, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 22 AND n_day <= 22 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv22, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 23 AND n_day <= 23 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv23, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 24 AND n_day <= 24 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv24, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 25 AND n_day <= 25 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv25, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 26 AND n_day <= 26 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv26, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 27 AND n_day <= 27 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv27, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 28 AND n_day <= 28 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv28, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 29 AND n_day <= 29 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv29, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 30 AND n_day <= 30 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv30, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 31 AND n_day <= 31 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv31, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 45 AND n_day <= 45 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv45, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 60 AND n_day <= 60 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv60, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 90 AND n_day <= 90 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv90, SUM(CASE WHEN CURRENT_DATE - cohort_date - 1 >= 120 AND n_day <= 120 THEN pay_amount ELSE NULL END)::numeric / UNIQ(user_id)::numeric AS ltv120 FROM base_data WHERE play_tag IS NOT NULL GROUP BY GROUPING SETS((data_month, appid, play_tag), (appid, play_tag)) ;