1. 程式人生 > 實用技巧 >留存率sql實現

留存率sql實現

用s q l實現留存率的實現。

DROP TABLE IF EXISTS bigdata2c.temp_shihuo_shoe_daidai_1118_04 ;
CREATE TABLE IF NOT EXISTS bigdata2c.temp_shihuo_shoe_daidai_1118_04 AS
SELECT  temp.order_month 
        ,COUNT(distinct user_id_month_0) AS month_01
        ,COUNT(distinct user_id_month_1) AS month_02
        ,COUNT(distinct
user_id_month_2) AS month_03 ,COUNT(distinct user_id_month_3) AS month_04 ,COUNT(distinct user_id_month_4) AS month_05 ,COUNT(distinct user_id_month_5) AS month_06 ,COUNT(distinct user_id_month_6) AS month_07 ,COUNT(distinct user_id_month_7) AS month_08 ,COUNT
(distinct user_id_month_8) AS month_09 ,COUNT(distinct user_id_month_9) AS month_10 ,COUNT(distinct user_id_month_10) AS month_11 ,COUNT(distinct user_id_month_11) AS month_12 ,COUNT(distinct user_id_month_12) AS month_13 FROM ( SELECT distinct a.order_month ,a.user_id_month_0 ,b.client_str
AS user_id_month_1 ,c.client_str AS user_id_month_2 ,d.client_str AS user_id_month_3 ,f.client_str AS user_id_month_4 ,g.client_str AS user_id_month_5 ,h.client_str AS user_id_month_6 ,i.client_str AS user_id_month_7 ,j.client_str AS user_id_month_8 ,k.client_str AS user_id_month_9 ,l.client_str AS user_id_month_10 ,m.client_str AS user_id_month_11 ,n.client_str AS user_id_month_12 FROM ( SELECT distinct SUBSTR(create_time,1,6) as order_month ,client_str AS user_id_month_0 FROM bigdata2c.temp_shihuo_shoe_daidai_1118_01 where client_str is not NULL and create_time is not NULL and SUBSTR(create_time,1,6) >=201910 and SUBSTR(create_time,1,6) <=202010 ) a LEFT JOIN bigdata2c.temp_shihuo_shoe_daidai_1118_02 b ON DATEDIFF( to_date(b.order_month,'yyyymm'),to_date(a.order_month,'yyyymm'),'mm') = 1 AND a.user_id_month_0 = b.client_str LEFT JOIN bigdata2c.temp_shihuo_shoe_daidai_1118_02 c ON DATEDIFF( to_date(c.order_month,'yyyymm'),to_date(a.order_month,'yyyymm'),'mm') = 2 AND a.user_id_month_0 = c.client_str LEFT JOIN bigdata2c.temp_shihuo_shoe_daidai_1118_02 d ON DATEDIFF( to_date(d.order_month,'yyyymm'),to_date(a.order_month,'yyyymm'),'mm') = 3 AND a.user_id_month_0 = d.client_str LEFT JOIN bigdata2c.temp_shihuo_shoe_daidai_1118_02 f ON DATEDIFF( to_date(f.order_month,'yyyymm'),to_date(a.order_month,'yyyymm'),'mm') = 4 AND a.user_id_month_0 = f.client_str LEFT JOIN bigdata2c.temp_shihuo_shoe_daidai_1118_02 g ON DATEDIFF( to_date(g.order_month,'yyyymm'),to_date(a.order_month,'yyyymm'),'mm') = 5 AND a.user_id_month_0 = g.client_str LEFT JOIN bigdata2c.temp_shihuo_shoe_daidai_1118_02 h ON DATEDIFF( to_date(h.order_month,'yyyymm'),to_date(a.order_month,'yyyymm'),'mm') = 6 AND a.user_id_month_0 = h.client_str LEFT JOIN bigdata2c.temp_shihuo_shoe_daidai_1118_02 i ON DATEDIFF( to_date(i.order_month,'yyyymm'),to_date(a.order_month,'yyyymm'),'mm') = 7 AND a.user_id_month_0 = i.client_str LEFT JOIN bigdata2c.temp_shihuo_shoe_daidai_1118_02 j ON DATEDIFF( to_date(j.order_month,'yyyymm'),to_date(a.order_month,'yyyymm'),'mm') = 8 AND a.user_id_month_0 = j.client_str LEFT JOIN bigdata2c.temp_shihuo_shoe_daidai_1118_02 k ON DATEDIFF( to_date(k.order_month,'yyyymm'),to_date(a.order_month,'yyyymm'),'mm') = 9 AND a.user_id_month_0 = k.client_str LEFT JOIN bigdata2c.temp_shihuo_shoe_daidai_1118_02 l ON DATEDIFF( to_date(l.order_month,'yyyymm'),to_date(a.order_month,'yyyymm'),'mm') = 10 AND a.user_id_month_0 = l.client_str LEFT JOIN bigdata2c.temp_shihuo_shoe_daidai_1118_02 m ON DATEDIFF( to_date(m.order_month,'yyyymm'),to_date(a.order_month,'yyyymm'),'mm') = 11 AND a.user_id_month_0 = m.client_str LEFT JOIN bigdata2c.temp_shihuo_shoe_daidai_1118_02 n ON DATEDIFF( to_date(n.order_month,'yyyymm'),to_date(a.order_month,'yyyymm'),'mm') = 12 AND a.user_id_month_0 = n.client_str ) temp group by temp.order_month ;