CREATE TABLE datasci.ads_996eco_index_device_m (
    index_type text,
    data_month text,
    re_device_type text,
    act_cnt bigint,
    pay_cnt bigint,
    pay_amount bigint,
    mau bigint,

    act_num bigint,
    reg_count bigint,

);


DELETE FROM datasci.ads_996eco_index_device_m WHERE ds = '${month}';

-- 每月付费
WITH ga AS (
    SELECT
        id,
        name
    FROM public.gm_apply
    WHERE bind_type = 1 AND bind_game_type = 1
)
,
aa AS (
    SELECT
        '${month}' data_month,
        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,
        UNIQ(user_id) act_cnt
    FROM
    public.ods_active_account aa
    INNER JOIN
    ga  ga ON aa.appid = ga.id
    WHERE ds BETWEEN '${bizmonth}' AND '${bizdate}'
    GROUP BY 1,2
),
po AS (
    SELECT
        '${month}' data_month,
        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,
        UNIQ(po.user_id) pay_cnt,
        SUM(po.amount) pay_amount
    FROM
    public.ods_payment_order po
    INNER JOIN
    ga  ga ON po.appid = ga.id
    WHERE ds BETWEEN '${bizmonth}' AND '${bizdate}'
    GROUP BY 1,2
)
INSERT INTO
    datasci.ads_996eco_index_device_m (index_type,data_month,re_device_type,act_cnt,pay_cnt,pay_amount)
SELECT
    'pay' index_type,
    t1.data_month AS data_month,
    t1.re_device_type,
    t1.act_cnt AS act_cnt,
    t2.pay_cnt AS pay_cnt,
    t2.pay_amount  AS pay_amount
FROM aa t1
LEFT JOIN
po t2 ON t1.data_month = t2.data_month and t1.re_device_type = t2.re_device_type
;

INSERT INTO
    datasci.ads_996eco_index_device_m (index_type,data_month,re_device_type,mau)
select
    'mau' index_type,
    '${month}' data_month,
    case when lower(t1.device_type)~'h5' then 'H5'
         when lower(t1.device_type)~'pc' then 'PC'
         when lower(t1.device_type)~'ios' then 'IOS'
    else t1.device_type end as re_device_type,
    uniq(user_id) as mau
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 '${bizmonth}' AND '${bizdate}'
group by 1,2,3
;


INSERT INTO
    datasci.ads_996eco_index_device_m (index_type,data_month,re_device_type,mau,device_type)
select
    'device-type-mau' index_type,
    '${month}' data_month,
    case when lower(t1.device_type)~'h5' then 'H5'
         when lower(t1.device_type)~'pc' then 'PC'
         when lower(t1.device_type)~'ios' then 'IOS'
    else t1.device_type end as re_device_type,
    uniq(user_id) as mau,
    t1.device_type
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 '${bizmonth}' AND '${bizdate}'
group by 1,2,3,5
;