电商用户之用户画像5-3数据开发--客户消费订单表

--用户画像--客户消费订单表

drop table if exists gdm.gdm_user_portrait_order;
CREATE EXTERNAL TABLE gdm.gdm_user_portrait_order(
    user_id                    bigint        ,--客户ID
    first_order_time        timestamp    ,--第一次消费时间
    last_order_time            timestamp    ,--最近一次消费时间
    first_order_ago            bigint        ,--首单距今时间
    last_order_ago            bigint        ,--尾单距今时间
    month1_hg_order_cnt        bigint        ,--近30天购买次数(不含退拒)
    month1_hg_order_amt        double        ,--近30天购买金额(不含退拒)
    month2_hg_order_cnt        bigint        ,--近60天购买次数(不含退拒)
    month2_hg_order_amt        double        ,--近60天购买金额(不含退拒)
    month3_hg_order_cnt        bigint        ,--近90天购买次数(不含退拒)
    month3_hg_order_amt        double        ,--近90天购买金额(不含退拒)
    month1_order_cnt        bigint        ,--近30天购买次数(含退拒)
    month1_order_amt        double        ,--近30天购买金额(含退拒)
    month2_order_cnt        bigint        ,--近60天购买次数(含退拒)
    month2_order_amt        double        ,--近60天购买金额(含退拒)
    month3_order_cnt        bigint        ,--近90天购买次数(含退拒)
    month3_order_amt        double        ,--近90天购买金额(含退拒)
    max_order_amt            double        ,--最大消费金额
    min_order_amt            double        ,--最小消费金额
    total_order_cnt            bigint        ,--累计消费次数(不含退拒)
    total_order_amt            double        ,--累计消费金额(不含退拒)
    total_coupon_amt        double        ,--累计使用代金券金额
    user_avg_amt            double        ,--客单价(含退拒)
    month3_user_avg_amt        double        ,--近90天客单价(含退拒)
    common_address            string        ,--常用收货地区
    common_paytype            string        ,--常用支付方式
    month1_cart_cnt            bigint        ,--最近30天购物车次数
    month1_cart_goods_cnt    bigint        ,--最近30天购物车商品件数
    month1_cart_submit_cnt  bigint        ,--最近30天购物车提交商品件数
    month1_cart_rate        double        ,--最近30天购物车成功率
    month1_cart_cancnt_cnt    bigint        ,--最近30天购物车放弃商品件数
    
    return_cnt                bigint        ,--退货商品数量
    return_amt                double        ,--退货商品金额
    reject_cnt                bigint        ,--拒收商品数量
    reject_amt                double        ,--拒收商品金额
    
    last_return_time        timestamp    ,--最近一次退货时间
    school_order_cnt        bigint        ,--学校下单总数
    company_order_cnt        bigint        ,--单位下单总数
    home_order_cnt            bigint        ,--家里下单总数
    forenoon_order_cnt        bigint        ,--上午下单总数
    afternoon_order_cnt        bigint        ,--下午下单总数
    night_order_cnt            bigint        ,--晚上下单总数
    morning_order_cnt        bigint        ,--早上下单总数
    dw_date                    timestamp    ,--
    )partitioned by (dt string)
;


客户消费订单表.sql


drop table if exists temp.tmp_gdm_user_portrait_order_01;
CREATE TABLE temp.tmp_gdm_user_portrait_order_01 as
drop table if exists temp.tmp_gdm_user_portrait_order_01;
CREATE TABLE temp.tmp_gdm_user_portrait_order_01 as
select    t.user_id,        --客户ID
        min(create_time) first_order_time,        --第一次消费时间
        max(create_time) last_order_time,        --最近一次消费时间
        datediff(min(create_time),'2015-01-01') first_order_ago,    --首单距今时间
        datediff(min(create_time),'2015-01-01') last_order_ago,        --尾单距今时间
        sum(case
                when t.dat_30 = 1 and t.jt_flag=0 then
                1
            end) month1_hg_order_cnt,    --近30天购买次数(不含退拒)
        sum(case
                when t.dat_30 = 1 and t.jt_flag=0 then
                t.order_money
            end) month1_hg_order_amt,    --近30天购买金额(不含退拒)
            
        sum(case
                when t.dat_60 = 1 and t.jt_flag=0 then
                1
            end) month2_hg_order_cnt,    --近60天购买次数(不含退拒)
        sum(case
                when t.dat_60 = 1 and t.jt_flag=0 then
                t.order_money
            end) month2_hg_order_amt,    --近60天购买金额(不含退拒)
            
        sum(case
                when t.dat_90 = 1 and t.jt_flag=0 then
                1
            end) month3_hg_order_cnt,    --近90天购买次数(不含退拒)
        sum(case
                when t.dat_90 = 1 and t.jt_flag=0 then
                t.order_money
            end) month3_hg_order_amt,    --近90天购买金额(不含退拒)
            
        sum(dat_30) month1_order_cnt,    --近30天购买次数(含有退拒)
        sum(case
                when t.dat_30 = 1  then
                t.order_money
            end) month1_order_amt,    --近30天购买金额(含退拒)
            
        sum(dat_60) month2_order_cnt,    --近60天购买次数(含有退拒)
        sum(case
                when t.dat_60 = 1  then
                t.order_money
            end) month2_order_amt,    --近60天购买金额(含退拒)
            
        sum(dat_90) month3_order_cnt,    --近90天购买次数(含有退拒)
        sum(case
                when t.dat_90 = 1  then
                t.order_money
            end) month3_order_amt,    --近90天购买金额(含退拒)
            
        max(t.order_money) max_order_amt, --最大消费金额
        min(t.order_money) min_order_amt, --最小消费金额
        sum(case
                when t.jt_flag = 0  then
                1
            end) total_order_cnt,    --累计消费次数(不含退拒)
        sum(case
                when t.jt_flag = 0  then
                t.order_money
            end) total_order_amt,    --累计消费金额(不含退拒)
            
        sum(coupon_money) total_coupon_amt,        --累计使用代金券金额
        sum(t.order_money)/count(1) user_avg_amt,  --客单价(含退拒)
        
        0 month3_user_avg_amt,    --近90天客单价(含有退拒)
        0 common_address,        --常用收货地区
        0 common_paytype,        --常用支付方式
        0 month1_cart_cnt,        --最近30天购物车次数
        0 month1_cart_goods_cnt,    --最近30天购物车商品件数
        0 month1_cart_submit_cnt,    --最近30天购物车提交商品件数
        0 month1_cart_rate,            --最近30天购物车成功率
        0 month1_cart_cancnt_cnt,    --最近30天购物车放弃商品件数
        
        
        sum(case
                when t.order_status = 3 then
                    t1.goods_amount
            end) return_cnt,        --退货商品数量
        sum(case
                when t.order_status = 3 then
                    t.order_money
            end) return_amt,        --退货商品金额
        
        sum(case
                when t.order_status = 4 then
                    t1.goods_amount
            end) reject_cnt,        --拒收商品数量
        sum(case
                when t.order_status = 4 then
                    t.order_money
            end) reject_amt,        --拒收商品金额
        
        max(case
                when t.order_status = 3 then
                    t.create_time
            end) last_return_time,        --最近一次退货时间
        
        sum(case
                when t2.order_addr = 1 then
                1
            end) school_order_cnt,        --学校下单总数
        sum(case
                when t2.order_addr = 2 then
                1
            end) company_order_cnt,        --单位下单总数
        sum(case
                when t2.order_addr = 3 then
                1
            end) home_order_cnt,        --家里下单总数
            
        
        sum(case
                when t.order_hour >=8 and t.order_hour <= 11 then
                1
            end) forenoon_order_cnt,        --上午下单总数
        sum(case
                when t.order_hour >=12 and t.order_hour <= 18 then
                1
            end) afternoon_order_cnt,        --下午下单总数
        sum(case
                when t.order_hour >=19 and t.order_hour <= 22 then
                1
            end) night_order_cnt,        --晚上下单总数
        sum(case
                when t.order_hour >=23 and t.order_hour <= 7 then
                1
            end) morning_order_cnt        --凌晨下单总数
            
    from ( select a.*,
                    (case
                        when order_date >= date_sub('2015-01-01',29) and
                             order_date <= '2015-01-01' then
                        1
                    end) dat_30,
                    (case
                        when order_date >= date_sub('2015-01-01',59) and
                             order_date <= '2015-01-01' then
                        1
                    end) dat_60,    
                    (case
                        when order_date >= date_sub('2015-01-01',89) and
                             order_date <= '2015-01-01' then
                        1
                    end) dat_90,
                    (case
                        when a.order_status in (3,4) then
                        1
                        else
                        0
                    end) jt_flag,  --退货与拒收
            from gdm.gdm_ord_order a
        where dt ='2015-01-01'
            and order_status = 5) t
        left join (select order_id ,sum(goods_amount)goods_amount from fdm.fdm_ord_order_goods where dt = '2015-01-01' group by   ****
        on(t.order_id = t1.order_id)
            left join (select user_id,order_addr from gdm.gdm_user_order_addr_model where dt = '2015-01-01') t2
        on(t.user_id=t2.user_id)
    group by t.user_id;
    
    
    
drop table if exists temp.tmp_gdm_user_portrait_order_02;
CREATE TABLE temp.tmp_gdm_user_portrait_order_02 as

select user_id,
        count(1) month1_cart_cnt ,        --最近30天购物车次数
        sum(number) month1_cart_goods_cnt, --最近30天购物车商品件数
        sum(case
                when submit_time is not null then
                    number
            end) month1_cart_submit_cnt,    --最近30天购物车提交商品件数
            ''month1_cart_rate,            --最近30天购物车成功率
        sum(case
                when cancel_time is not null then
                    number
            end) month1_cart_cancnt_cnt,    --最近30天购物车放弃商品件数
    from fdm.fdm_ord_cart
    
    where dt = '2015-01-01'
        and to_date(add_time) >= date_sub('2015-01-01',29)
        and to_date(add_time) <= '2015-01-01'
    group by user_id;
        
        
        
drop table if exists temp.tmp_gdm_user_portrait_order_03;
CREATE TABLE temp.tmp_gdm_user_portrait_order_03 as        

select t.user_id,t.con,t.type,t.cnt
    from (select b.user_id,
                 b.con,
                 b.type,
                 b.cnt
                 row_number() over(distribute by b.user_id,b.type sort by b.cnt,b.type desc) rn
            from(select a.user_id,
                        concat(coalesce(area_name,''),coalesce(address,'')) con,
                        'address' type,
                        count(1) cnt
                    from gdm.gdm_ord_order a
                where dt = '2015-01-01'
                group by a.user_id,concat(coalesce(area_name, ''),coalesce(address),''))
            union all
            select a.user_id,
                   a.pay_type con,
                   'pay_type' type,
                   count(1) cnt
                from gdm.gdm_ord_order a
                where dt = '2015-01-01'
                group by a.user_id,a.pay_type)b) t
where t.rn = 1;
            
drop table if exists temp.tmp_gdm_user_portrait_order_99;
CREATE TABLE temp.tmp_gdm_user_portrait_order_99 as        
select a.user_id
    from(select user_id
            from temp.tmp_gdm_user_portrait_order_01
        union all
        select user_id from temp.tmp_gdm_user_portrait_order_02) a
    group by a.user_id;
        
        
insert overwrite table gdm.gdm_user_portrait_order partition(dt = '2015-01-01')

select t.user_id,            --客户ID
       t1.first_order_time,    --第一次消费时间
       t1.last_order_time,    --最近一次消费时间
       t1.first_order_ago,    --首单距今时间
       t1.last_order_ago,    --尾单距今时间
       t1.month1_order_cnt,        --近30天购买次数(不含退拒)
       t1.month1_hg_order_amt,    --近30天购买金额(不含退拒)
       t1.month2_hg_order_cnt,    --近60天购买次数(不含退拒)
       t1.month2_hg_order_amt,    --近60天购买金额(不含退拒)
       t1.month3_hg_order_cnt,     --近90天购买次数(不含退拒)
       t1.month3_hg_order_amt,    --近90天购买金额(不含退拒)
       t1.month1_cart_cnt,      --近30天购买次数(含退拒)
       t1.month1_order_amt,        --近30天购买金额(含退拒)
       t1.month2_cart_cnt,        --近60天购买次数(含退拒)
       t1.month2_order_amt,        --近60天购买金额(含退拒)
       t1.month3_cart_cnt,        --近90天购买次数(含退拒)
       t1.month3_order_amt,        --近90天购买金额(含退拒)
       t1.max_order_amt,        --最大消费金额
       t1.min_order_amt,        --最小消费金额
       t1.total_order_cnt,        --累计消费次数(不含退拒)
       t1.total_order_amt,        --累计消费金额(不含退拒)
       t1.total_coupon_amt,        --累计消费代金券金额
       t1.user_avg_amt,            --客单价(含有退拒)
       (case when t1.month3_order_cnt<>0 then
       t1.month3_order_amt/t1.month3_order_cnt
       else 0 end)month3_user_avg_amt,    --近90天客单价(含有退拒)
       t3.common_address,        --常用收货地区
       t3.common_paytype,        --常用支付方式
       t2.month1_cart_cnt,        --最近30天购物车次数
       t2.month1_cart_goods_cnt,    --最近30天购物车商品件数
       t2.month1_cart_submit_cnt,    --最近30天购物车提交商品件数
      (case when t1.month3_order_cnt<>0 then
      t2.month1_cart_submit_cnt/t2.month1_cart_goods_cnt
      else 0 end)month1_cart_rate,        --最近30天购物车成功率
       t2.month1_cart_cancnt_cnt,    --最近30天购物车放弃商品件数
       
       
       t1.last_return_time,            --最近一次提交退货时间
       t1.school_order_cnt,            --学校下单总数
       t1.company_order_cnt,        --单位下单总数
       t1.home_order_cnt,            --家里下单总数
       t1.forenoon_order_cnt,        --上午下单总数
       t1.afternoon_order_cnt,        --下午下单总数
       t1.night_order_cnt,            --晚上下单总数
       t1.morning_order_cnt,        --凌晨下单总数
       from_unixtime(unix_timestamp()) dw_date
    from temp.tmp_gdm_user_portrait_order_99 t
    left join temp.tmp_gdm_user_portrait_order_01 t1
        on(t.user_id = t1.user_id)
    left join temp.tmp_gdm_user_portrait_order_02 t2
        on(t.user_id=t2.user_id)
    left join (select user_id,
                      max(case
                            when type = 'address' then
                            con
                            end) common_address,
                      max(case
                            when type = 'pay_type' then
                            con
                            end) common_paytype,
                from temp.tmp_gdm_user_portrait_order_03
                group by user_id) t3
        on (t.user_id = t3.user_id);


猜你喜欢

转载自blog.csdn.net/qq_36764089/article/details/79469922
今日推荐