【hive】關於用戶留存率的計算


首先用戶留存率一般是面向新增用戶的概念,是指某一天注冊后的幾天還是否活躍,是以每天為單位進行計算的.
一般收到的需求都是一個時間段內的新增用戶的幾天留存

(1)找到這個時間段內的新增用戶(也可能含有地區啊的各種附加條件),一般在日活表中有記錄是否是新增狀態.
  注意,需要以天為單位進行分組找出用戶的id.因為留存率都是以每天為單位進行計算的.
  表結構(register_date,user_id)

(2)找到這個時間段內的活躍用戶(active_date,user_id)

(3)以 1表 為主表left join 2表 以user_id為關聯鍵,統計留存數

  這樣后的記錄類型為:register_date,user_id,active_date

  register_date為新增日期,即留存率的單位天.

  user_id為用戶id,distinct user_id來計算用戶數

 

留存率怎么算?
  active_date - register_date = 1,說明注冊的次日用戶是活躍的,所以count+1
  所以我們只要關注 active_date 和 register_date 相差天數即可統計留存數
  取天數差的時候用datediff(active_date,register_date)來計算,active_date 和 register_date 的格式為 yyyy-MM-dd

(4)計算留存率

 

模板:

    SET mapreduce.job.queuename=xxx;
    SET mapreduce.job.name=xxx;
    set mapreduce.job.reduces=19;
            select '日期', '注冊用戶數', '次日留存率', '2日留存率', '3日留存率',  dim_date
                ,total_cnt
                ,concat_ws('% | ', cast(round(dif_1cnt*100/total_cnt, 2) as string), cast(dif_1cnt as string))
                ,concat_ws('% | ', cast(round(dif_2cnt*100/total_cnt, 2) as string), cast(dif_2cnt as string))
                ,concat_ws('% | ', cast(round(dif_3cnt*100/total_cnt, 2) as string), cast(dif_3cnt as string))
                ,concat_ws('% | ', cast(round(dif_4cnt*100/total_cnt, 2) as string), cast(dif_4cnt as string))
            from
            (
                select p1.state dim_date
                    ,p1.device_os
                    ,count(distinct p1.user_id) total_cnt
                    ,count(distinct if(datediff(p3.state,p1.state) = 1, p1.user_id, null)) dif_1cnt
                    ,count(distinct if(datediff(p3.state,p1.state) = 2, p1.user_id, null)) dif_2cnt
                    ,count(distinct if(datediff(p3.state,p1.state) = 3, p1.user_id, null)) dif_3cnt
                    ,count(distinct if(datediff(p3.state,p1.state) = 4, p1.user_id, null)) dif_4cnt
                from
                    (
                        select
                            from_unixtime(unix_timestamp(cast(partition_date as string), 'yyyyMMdd'), 'yyyy-MM-dd') state,
                            user_id
                        from user_active_day
                        where partition_date between date1 and date2
                        and user_is_new = 1
                        group by 1,2
                    )p1 --日新增用戶名單(register_date,user_id)
                    left outer join
                    (
                        select
                            from_unixtime(unix_timestamp(cast(partition_date as string), 'yyyyMMdd'), 'yyyy-MM-dd') state,
                            user_id
                        from active_users
                        where partition_date between date1 and date2
                        group by 1,2
                    )p3 --期間活躍用戶(active_date,user_id)
                    on (p3.user_id = p1.user_id)
                group by 1,2
            ) p4;

友情附贈:

求用戶首充的時間,在充值表中以user_id分組,然后找出最小的訂單添加時間,對應的訂單就是首充的記錄.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM