insert into ads.ALS_XXH_CANCEL_NEW_C4
select '${month}' as TARGET_MONTH,
	s0.USERID as USERID,`PROVINCE`,`CHANNEL`,s0.CPID as CPID,s0.SPID as SPID,`ORDERTIME`,DIFF_MONYH,`DIFF_MONYH_TYPE`,`IS_CANCEL`,e1.`PRICE` as `PRICE`,toFloat64OrZero(e1.MAXFLOW ) as MAXFLOW,
	if(isNaN(MAvg3_Flow),0,MAvg3_Flow) as MAvg3_Flow,`MPer1_Flow`,
	--处理当月流量，使其更合理
	`M1_Flow`,dateDiff('day', ORDERTIME, now()) as diffDay,if(diffDay<30,(30/diffDay)*M1_Flow,M1_Flow) as M1_Flow01,if(M1_Flow01>MAXFLOW,MAXFLOW,M1_Flow01) as M1_Flow02,
	MAvg3_Flow/MAXFLOW as MAvg3_Flow_P,MPer1_Flow/MAXFLOW as MPer1_Flow_P,M1_Flow02/MAXFLOW as M1_Flow02_P,
	ifNull(CUST_SEX,'未知') CUST_SEX,ifNull(CERT_AGE,'未知') CERT_AGE ,ifNull(TOTAL_FEE_AVG,0) TOTAL_FEE_AVG,
	ifNull(MANU_NAME,'未知') MANU_NAME ,ifNull(IS_ACCT_AVG,0) IS_ACCT_AVG ,ifNull(TOTAL_FLUX_AVG,0) TOTAL_FLUX_AVG,
	ifNull(TOTAL_FLUX_VIDEO_AVG,0) TOTAL_FLUX_VIDEO_AVG,BELONGTO,concat('c',toString(CPTYPE)) as CPTYPE,if(USERVAL='','unget','get') as IS_VIP,ORDER_HOUR
	--new
	,taocan.PRICE as PACKAGE_PRICE,H_FLUX,IS_DAWANG
	,MPer1_TOTAL_FLUX,MPer2_TOTAL_FLUX,MPer3_TOTAL_FLUX,MPer4_TOTAL_FLUX,MPer1_TOTAL_VIDEO_FLUX,MPer2_TOTAL_VIDEO_FLUX,MPer3_TOTAL_VIDEO_FLUX,MPer4_TOTAL_VIDEO_FLUX
	,MPer1_Flow_kuaishou,MPer2_Flow_kuaishou,MPer3_Flow_kuaishou,MPer4_Flow_kuaishou
	,MPer1_Flow_tencent,MPer2_Flow_tencent,MPer3_Flow_tencent,MPer4_Flow_tencent,
	MPer1_Flow_bilibl,MPer2_Flow_bilibl,MPer3_Flow_bilibl,MPer4_Flow_bilibl,
	MPer1_Flow_aiqy,MPer2_Flow_aiqy,MPer3_Flow_aiqy,MPer4_Flow_aiqy,
	MPer1_Flow_youtu,MPer2_Flow_youtu,MPer3_Flow_youtu,MPer4_Flow_youtu
	from
	(
		--订购数据
		select USERID,any(PROVINCE) as PROVINCE,CHANNEL,CPID ,SPID,
		ORDERTIME,--CANCELTIME,
		DIFF_MONYH,DIFF_MONYH_TYPE,IS_CANCEL,ORDER_HOUR
		from (
			with toString(THEMONTH) as mstr
			select USERID,THEMONTH,replaceOne(replaceOne(replaceOne(PROVINCE,'省',''),'市',''),'自治区','') as PROVINCE,CHANNEL,CPID,SPID,
			ORDERTIME,--CANCELTIME,
			dateDiff('month', ORDERTIME, toDate(concat(SUBSTRING(mstr,1,4),'-' ,SUBSTRING(mstr,5,2),'-01')))+1 DIFF_MONYH,
			multiIf(DIFF_MONYH>6,'diff6_p',DIFF_MONYH>=4 and DIFF_MONYH<=6 ,'diff4_6',concat('diff',toString(DIFF_MONYH))) as DIFF_MONYH_TYPE,
			if(CANCELTIME!='1970-01-01' and CANCELTIME <'${pr1full_month}',1,0) as IS_CANCEL,
      concat('h',toString(floor(toHour(ORDERTIME2,'Asia/Shanghai')/3))) as ORDER_HOUR
			from
			ads.HIS_FLOW_ORDER
			where THEMONTH ='${month}' and DIFF_MONYH<13
      and SPID !='99144186'
      ${sql_Cancel}
			-- and DIFF_MONYH>3 	order by USERID
			-- and USERID ='15694037210'
		) group by  USERID,SPID,CPID,ORDERTIME,CHANNEL,DIFF_MONYH,DIFF_MONYH_TYPE,IS_CANCEL,ORDER_HOUR
	) s0
	left join
	(
		--流量使用数据
		select USERID,SPID,
		-- 前三月平均流量
		avgIf(SumMonthFlow,THEMONTH<'${month}') as MAvg3_Flow,
		-- 最近一月流量
		anyIf(SumMonthFlow,THEMONTH='${p2month}') as MPer1_Flow,
		-- 当月此时使用流量
		anyIf(SumMonthFlow,THEMONTH='${month}') as M1_Flow from
		(
			--算出每个月 每人 每产品 流量和
			select THEMONTH,USERID,SPID,sum(FLOW) as SumMonthFlow from
			(
				select THEMONTH,USERID,SPID,FLOW from ads.TD_DXFLOWINPUT_MONTH
				where THEMONTH in ('${p4month}','${p3month}','${p2month}','${month}')
				-- and USERID ='15694037210'
			)
			group by THEMONTH,USERID,SPID
		) group by USERID,SPID
	) s1 on s0.USERID=s1.USERID and s0.SPID=s1.SPID
	left join ads.ETL_V_TB_SP_INFO e1 on s0.SPID=e1.SPID
 	left join ads.DIM_TB_CHANNELS c1 on s0.CHANNEL=c1.ID
	left join
	(
		select DISTINCT USERVAL
		from ads.ETL_TL_VIP_LOG
		where THEDAY >='${per3Day}'
		and THEDAY<'${theDay}'
		and VIPID in
		(select VIPID from ads.DIM_SXXHY)
	) v1 on s0.USERID=v1.USERVAL
	left join
	(
		select DEVICE_NUMBER,avgIf(TOTAL_FEE,IS_ACCT=1) as TOTAL_FEE_AVG,
		any(PRODUCT_CLASS_DESC) as PRODUCT_CLASS_DESC,
		any(MANU_NAME) as MANU_NAME,avg(IS_ACCT) as IS_ACCT_AVG,
		avg(TOTAL_FLUX) as TOTAL_FLUX_AVG,
		avg(REAL_TOTAL_FLUX_VIDEO) as TOTAL_FLUX_VIDEO_AVG,
  		-- 前1月流量
		anyOrNullIf(TOTAL_FLUX,MONTH_ID='${p2month}') as MPer1_TOTAL_FLUX,
		-- 前2月流量
		anyOrNullIf(TOTAL_FLUX,MONTH_ID='${p3month}') as MPer2_TOTAL_FLUX,
		-- 前3月流量
		anyOrNullIf(TOTAL_FLUX,MONTH_ID='${p4month}') as MPer3_TOTAL_FLUX,
		-- 前4月流量
		anyOrNullIf(TOTAL_FLUX,MONTH_ID='${p5month}') as MPer4_TOTAL_FLUX,
		-- 前1月流量
		anyOrNullIf(REAL_TOTAL_FLUX_VIDEO,MONTH_ID='${p2month}') as MPer1_TOTAL_VIDEO_FLUX,
		-- 前2月流量
		anyOrNullIf(REAL_TOTAL_FLUX_VIDEO,MONTH_ID='${p3month}') as MPer2_TOTAL_VIDEO_FLUX,
		-- 前3月流量
		anyOrNullIf(REAL_TOTAL_FLUX_VIDEO,MONTH_ID='${p4month}') as MPer3_TOTAL_VIDEO_FLUX,
		-- 前4月流量
		anyOrNullIf(REAL_TOTAL_FLUX_VIDEO,MONTH_ID='${p5month}') as MPer4_TOTAL_VIDEO_FLUX
    from
		 (
		 	 --INNET_MONTHS,
			 select MONTH_ID,DEVICE_NUMBER,CUST_SEX ,CERT_AGE ,toFloat64OrZero(TOTAL_FEE) as TOTAL_FEE,
			 MANU_NAME,toUInt8OrZero(IS_ACCT) as IS_ACCT,PRODUCT_CLASS_DESC,toFloat64OrZero(TOTAL_FLUX) as TOTAL_FLUX,
			 toFloat64OrZero(TOTAL_FLUX_VIDEO) as REAL_TOTAL_FLUX_VIDEO  from  ads.XXH_ALL_MONTH
			 where MONTH_ID in ('${p2month}','${p3month}','${p4month}','${p5month}')
			 --and  DEVICE_NUMBER ='15694037210'
			 and DEVICE_NUMBER in (select distinct USERID from ads.HIS_FLOW_ORDER where THEMONTH ='${month}' and (dateDiff('month', ORDERTIME, toDate('${dateStr}'))+1)<13)
		 ) s
		group by DEVICE_NUMBER
	) s2 on s0.USERID=s2.DEVICE_NUMBER
 left join ads.ETL_TB_TAOCAN_INFO_2 taocan on s2.PRODUCT_CLASS_DESC=taocan.PRODUCT_CLASS_DESC
	left JOIN
 	(select DEVICE_NUMBER,CUST_SEX,CERT_AGE from  ads.XXH_ALL_MONTH_LONG	where
 		DEVICE_NUMBER in
 		(select distinct USERID from ads.HIS_FLOW_ORDER where THEMONTH ='${month}' and (dateDiff('month', ORDERTIME, toDate('${dateStr}'))+1)<13)
 		 --and  DEVICE_NUMBER ='15501470503'
 	) lo on s0.USERID=lo.DEVICE_NUMBER
 	--快手数据
 	left JOIN
 	(
 	select DEVICE_NUMBER,
		-- 前1月流量
		anyOrNullIf(MonthFlow,MONTH_ID='${p2month}' and APP_NAME='快手') as MPer1_Flow_kuaishou,
		-- 前2月流量
		anyOrNullIf(MonthFlow,MONTH_ID='${p3month}' and APP_NAME='快手') as MPer2_Flow_kuaishou,
		-- 前3月流量
		anyOrNullIf(MonthFlow,MONTH_ID='${p4month}' and APP_NAME='快手') as MPer3_Flow_kuaishou,
		-- 前4月流量
		anyOrNullIf(MonthFlow,MONTH_ID='${p5month}' and APP_NAME='快手') as MPer4_Flow_kuaishou,
				anyOrNullIf(MonthFlow,MONTH_ID='${p2month}' and APP_NAME='腾讯视频') as MPer1_Flow_tencent,
		anyOrNullIf(MonthFlow,MONTH_ID='${p3month}' and APP_NAME='腾讯视频') as MPer2_Flow_tencent,
		anyOrNullIf(MonthFlow,MONTH_ID='${p4month}' and APP_NAME='腾讯视频') as MPer3_Flow_tencent,
		anyOrNullIf(MonthFlow,MONTH_ID='${p5month}' and APP_NAME='腾讯视频') as MPer4_Flow_tencent,
				anyOrNullIf(MonthFlow,MONTH_ID='${p2month}' and APP_NAME='哔哩哔哩') as MPer1_Flow_bilibl,
		anyOrNullIf(MonthFlow,MONTH_ID='${p3month}' and APP_NAME='哔哩哔哩') as MPer2_Flow_bilibl,
		anyOrNullIf(MonthFlow,MONTH_ID='${p4month}' and APP_NAME='哔哩哔哩') as MPer3_Flow_bilibl,
		anyOrNullIf(MonthFlow,MONTH_ID='${p5month}' and APP_NAME='哔哩哔哩') as MPer4_Flow_bilibl,
				anyOrNullIf(MonthFlow,MONTH_ID='${p2month}' and APP_NAME='爱奇艺') as MPer1_Flow_aiqy,
		anyOrNullIf(MonthFlow,MONTH_ID='${p3month}' and APP_NAME='爱奇艺') as MPer2_Flow_aiqy,
		anyOrNullIf(MonthFlow,MONTH_ID='${p4month}' and APP_NAME='爱奇艺') as MPer3_Flow_aiqy,
		anyOrNullIf(MonthFlow,MONTH_ID='${p5month}' and APP_NAME='爱奇艺') as MPer4_Flow_aiqy,
				anyOrNullIf(MonthFlow,MONTH_ID='${p2month}' and APP_NAME='优酷视频') as MPer1_Flow_youtu,
		anyOrNullIf(MonthFlow,MONTH_ID='${p3month}' and APP_NAME='优酷视频') as MPer2_Flow_youtu,
		anyOrNullIf(MonthFlow,MONTH_ID='${p4month}' and APP_NAME='优酷视频') as MPer3_Flow_youtu,
		anyOrNullIf(MonthFlow,MONTH_ID='${p5month}' and APP_NAME='优酷视频') as MPer4_Flow_youtu
		from
		(
			select MONTH_ID,DEVICE_NUMBER,APP_NAME,MAX(ONE_FLUX) as MonthFlow from
			(
				select MONTH_ID ,DEVICE_NUMBER,APP_NAME,ONE_FLUX from ads.XXH_ALL_APP_MONTH
				where MONTH_ID in ('${p2month}','${p3month}','${p4month}','${p5month}')
				and APP_NAME in ('快手','腾讯视频','哔哩哔哩','爱奇艺','优酷视频')
				--and DEVICE_NUMBER ='15501470503'
				and DEVICE_NUMBER in
 				(select distinct USERID from ads.HIS_FLOW_ORDER where THEMONTH ='${month}' and (dateDiff('month', ORDERTIME, toDate('${dateStr}'))+1)<13)
			)
			group by MONTH_ID,APP_NAME,DEVICE_NUMBER
		) group by DEVICE_NUMBER
 	) t_app on s0.USERID=t_app.DEVICE_NUMBER
   where e1.CPTYPE in ('0','1')