zoukankan      html  css  js  c++  java
  • dwt_user_topic

    用户主题宽表

    类似累积事实表

    drop table if exists dwt_user_topic; 
    create external table dwt_user_topic 
    ( 
    user_id string comment '用户 id', 
    login_date_first string comment '首次登录时间', 
    login_date_last string comment '末次登录时间', 
    login_count bigint comment '累积登录天数', 
    login_last_30d_count bigint comment '最近 30 日登录天数', 
    order_date_first string comment '首次下单时间', 
    order_date_last string comment '末次下单时间', 
    order_count bigint comment '累积下单次数', 
    order_amount decimal(16,2) comment '累积下单金额', 
    order_last_30d_count bigint comment '最近 30 日下单次数', 
    order_last_30d_amount bigint comment '最近 30 日下单金额', 
    payment_date_first string comment '首次支付时间', 
    payment_date_last string comment '末次支付时间', 
    payment_count decimal(16,2) comment '累积支付次数', 
    payment_amount decimal(16,2) comment '累积支付金额', 
    payment_last_30d_count decimal(16,2) comment '最近 30 日支付次数', 
    payment_last_30d_amount decimal(16,2) comment '最近 30 日支付金额' 
    )
    COMMENT '用户主题宽表' 
    stored as parquet 
    location '/ecdw/dwt/dwt_user_topic/' 
    tblproperties ("parquet.compression"="lzo");
    
    
    --插入
    insert overwrite table dwt_user_topic 
    select 
        nvl(new.user_id,old.user_id), 
        if(old.login_date_first is null and new.login_count>0,'2020-03-10',old.login_date_first), 
        if(new.login_count>0,'2020-03-10',old.login_date_last), 
        nvl(old.login_count,0)+if(new.login_count>0,1,0), 
        nvl(new.login_last_30d_count,0), 
        if(old.order_date_first is null and new.order_count>0,'2020-03-10',old.order_date_first),
        if(new.order_count>0,'2020-03-10',old.order_date_last), 
        nvl(old.order_count,0)+nvl(new.order_count,0), 
        nvl(old.order_amount,0)+nvl(new.order_amount,0), 
        nvl(new.order_last_30d_count,0), 
        nvl(new.order_last_30d_amount,0), 
        if(old.payment_date_first is null and new.payment_count>0,'2020-03-10',old.payment_date_first), 
        if(new.payment_count>0,'2020-03-10',old.payment_date_last), 
        nvl(old.payment_count,0)+nvl(new.payment_count,0), 
        nvl(old.payment_amount,0)+nvl(new.payment_amount,0), 
        nvl(new.payment_last_30d_count,0), 
        nvl(new.payment_last_30d_amount,0) 
    from dwt_user_topic old 
    
    full outer join 
    ( 
    select 
        user_id, 
        sum(if(dt='2020-03-10',login_count,0)) login_count,   -- 获取最新的会员信息
        sum(if(dt='2020-03-10',order_count,0)) order_count, 
        sum(if(dt='2020-03-10',order_amount,0)) order_amount, 
        sum(if(dt='2020-03-10',payment_count,0)) payment_count, 
        sum(if(dt='2020-03-10',payment_amount,0)) payment_amount, 
        sum(if(login_count>0,1,0)) login_last_30d_count,   -- 获取累积30天的会员信息 
        sum(order_count) order_last_30d_count, 
        sum(order_amount) order_last_30d_amount, 
        sum(payment_count) payment_last_30d_count, 
        sum(payment_amount) payment_last_30d_amount 
    from dws_user_action_daycount 
    where dt>=date_add( '2020-03-10',-30) 
    group by user_id 
    )new 
    on old.user_id=new.user_id;
  • 相关阅读:
    AngularJS(17)-Angular小程序
    AngularJS(16)-路由
    AngularJS(15)-依赖注入
    AngularJS(14)-动画
    AngularJS(13)-包含
    AngularJS(12)-BootStrap集成
    AngularJS(11)-API
    AngularJS(10)-数据验证
    Mysql 备份和恢复.sql文件,导入.csv文件
    Mysql group_concat()
  • 原文地址:https://www.cnblogs.com/ldy233/p/14448831.html
Copyright © 2011-2022 走看看