-- 按月统计游戏的活跃账号相关数据
INSERT INTO dws.game_user_data_month(type,type_name,game_channel_id,game_identity,game_platform_id,ds,active_user_cut,play_user_cut,order_amount,money)

select
      'hyzh' type
      ,'活跃账号' type_name
      ,COALESCE(t.game_channel_id,t1.game_channel_id) game_channel_id
      , COALESCE(t.game_identity,t1.game_identity) game_identity
      , COALESCE(t.game_platform_id,t1.game_platform_id) game_platform_id
      , COALESCE(t.ds,t1.ds)  ds
      , COALESCE(sum(t.active_num), 0)                active_user_cut
      , COALESCE(sum(t1.pay_user_num), 0) play_user_cut
      , COALESCE(sum(t1.order_amount), 0) order_amount
      , COALESCE(SUM(t1.pay_amount), 0)   money

FROM dwd.active_account_df t
FULL OUTER JOIN dwd.order_df t1
on t.game_channel_id = t1.game_channel_id
and t.game_identity = t1.game_identity
and t.game_platform_id = t1.game_platform_id
and t.ds_type = t1.ds_type
and t.ds = t1.ds
where t.ds_type = 'm'
group by  COALESCE(t.game_channel_id,t1.game_channel_id)
        , COALESCE(t.game_identity,t1.game_identity)
        , COALESCE(t.game_platform_id,t1.game_platform_id)
        , COALESCE(t.ds,t1.ds)


ON CONFLICT (type,type_name,game_channel_id,game_identity,game_platform_id,ds) DO
UPDATE SET active_user_cut = EXCLUDED.active_user_cut,play_user_cut = EXCLUDED.play_user_cut,order_amount = EXCLUDED.order_amount
,money = EXCLUDED.money
;


-- 按月增量统计游戏的新账号相关数据


INSERT INTO dws.game_user_data_month(type,type_name,game_channel_id,game_identity,game_platform_id,ds,active_user_cut,play_user_cut,order_amount,money)


select
        'xzh' type
        ,'新账号' type_name
        ,t1.game_channel_id
        ,t1.game_identity
        ,t1.game_platform_id
        ,substring(t1.ds,1,6) || '00' ds
        ,COALESCE(sum(t1.active_user_cut),0) active_user_cut
        ,COALESCE(sum(t1.play_user_cut),0) play_user_cut
        ,COALESCE(sum(t1.order_amount),0) order_amount
        ,COALESCE(sum(t1.money),0) money
from dws.game_user_data t1
group by t1.game_channel_id
        ,t1.game_identity
        ,t1.game_platform_id
        ,substring(t1.ds,1,6) || '00'


ON CONFLICT (type,type_name,game_channel_id,game_identity,game_platform_id,ds) DO
UPDATE SET active_user_cut = EXCLUDED.active_user_cut,play_user_cut = EXCLUDED.play_user_cut,order_amount = EXCLUDED.order_amount
,money = EXCLUDED.money
;


