Quellcode durchsuchen

Merge remote-tracking branch 'origin/master'

renyupeng vor 3 Jahren
Ursprung
Commit
bdf00fa81d

+ 9 - 2
Apietl/bytedance_etl/Q4Task/AppBytedanceAdReportBusi.py

@@ -46,6 +46,8 @@ group by account_id """
                      bei.leader_id leader_id,
                      bei.org_code org_code,
                      report.cost,
+                     ifnull(ratio.cost,0) last_cost,
+                     ifnull(round((report.cost-ratio.cost)/ratio.cost*100,3),0) cost_ratio,
                      report. `SHOW`,
                      report.avg_show_cost,
                      report.click,
@@ -366,8 +368,7 @@ group by account_id """
                     GROUP BY
                     advertiser_id,
                     campaign_id,
-                    ad_id,
-                    ad_name
+                    ad_id
                      ) report
                     LEFT JOIN (
                      SELECT
@@ -396,6 +397,12 @@ group by account_id """
                       cua.account_id IS NOT NULL
                       and cua.account_id ={account_id}
                     ) bei ON report.account_id = bei.account_id
+                     LEFT JOIN 
+                    (select advertiser_id account_id,campaign_id,ad_id,sum(cost) cost from `media_api`.bytedance_ad_report_daily
+                    where stat_datetime between date_format(date_sub(date_format(date_sub({date_time}, INTERVAL {day_count} DAY),'%Y%m%d'), INTERVAL {day_count}-1 DAY),'%Y%m%d')
+                    and date_format(date_sub({date_time}, INTERVAL {day_count} DAY),'%Y%m%d')
+                    group by advertiser_id,campaign_id,ad_id) ratio
+                    on report.account_id = ratio.account_id and report.ad_id=ratio.ad_id;
                         """.format(date_time=self.date_time,
                                    day_count=self.day_count, account_id=account_id[0])
                     print(sql)

+ 9 - 1
Apietl/bytedance_etl/Q4Task/AppBytedanceAdvertiserReportBusi.py

@@ -39,6 +39,8 @@ SELECT concat(date_format(date_sub({date_time}, INTERVAL ({day_count} -1)  DAY),
        bei.leader_id       leader_id,
        bei.org_code        org_code,
        report.cost,
+       ifnull(ratio.cost,0) last_cost,
+       ifnull(round((report.cost-ratio.cost)/ratio.cost*100,3),0) cost_ratio,
        report.`show`,
        report.avg_show_cost,
        report.click,
@@ -378,7 +380,13 @@ FROM (
              LEFT JOIN `jeecg-boot`.user_company uc ON uc.user_id = cua.user_id
              LEFT JOIN `jeecg-boot`.ctop_product product ON product.id = cp.product_id
     WHERE cua.account_id IS NOT NULL
-) bei ON report.account_id = bei.account_id;
+) bei ON report.account_id = bei.account_id
+left join
+(select advertiser_id account_id,sum(cost) cost from `media_api`.bytedance_advertiser_report_daily
+where stat_datetime between date_format(date_sub(date_format(date_sub({date_time}, INTERVAL {day_count} DAY),'%Y%m%d'), INTERVAL {day_count}-1 DAY),'%Y%m%d')
+         and date_format(date_sub({date_time}, INTERVAL {day_count} DAY),'%Y%m%d')
+group by advertiser_id) ratio
+ON report.account_id = ratio.account_id;
             """.format(date_time=self.date_time, day_count=self.day_count)
             TidbConn.upsert(conn=self.conn, sql_buff=sql)
             self.conn.commit()

+ 13 - 5
Apietl/bytedance_etl/Q4Task/AppBytedanceCampaignReportBusi.py

@@ -40,6 +40,8 @@ report.account_id,
  bei.leader_id leader_id,
  bei.org_code org_code,
  report.cost,
+ ifnull(ratio.cost,0) last_cost,
+ ifnull(round((report.cost-ratio.cost)/ratio.cost*100,3),0) cost_ratio,
  report. `SHOW`,
  report.avg_show_cost,
  report.click,
@@ -358,8 +360,7 @@ FROM
                       date_sub({date_time}, INTERVAL {day_count}-1 DAY),'%Y%m%d') 
                     GROUP BY
                     advertiser_id,
-                    campaign_id,
-                    campaign_name) report
+                    campaign_id) report
 LEFT JOIN (
  SELECT
   cua.account_id id,
@@ -377,15 +378,22 @@ LEFT JOIN (
   su.leader_id,
   su.org_code org_code
  FROM
-  `jeecg-boot`.ctop_user_allocation cua
+`jeecg-boot`.ctop_user_allocation cua
  LEFT JOIN `jeecg-boot`.ctop_project cp ON cua.project_id = cp.id
  LEFT JOIN `jeecg-boot`.ctop_advertiser ca ON ca.id = cua.advertiser_id
  LEFT JOIN `jeecg-boot`.sys_user su ON su.id = cua.user_id
  LEFT JOIN `jeecg-boot`.user_company uc ON uc.user_id = cua.user_id
  LEFT JOIN `jeecg-boot`.ctop_product product ON product.id = cp.product_id
  WHERE
-  cua.account_id IS NOT NULL
-) bei ON report.account_id = bei.account_id""".format(date_time=self.date_time, day_count=self.day_count)
+  cua.account_id IS NOT NULL) bei 
+  ON report.account_id = bei.account_id
+  LEFT JOIN 
+(select advertiser_id account_id,campaign_id,sum(cost) cost from `media_api`.bytedance_campaign_report_daily
+where stat_datetime between date_format(date_sub(date_format(date_sub({date_time}, INTERVAL {day_count} DAY),'%Y%m%d'), INTERVAL {day_count}-1 DAY),'%Y%m%d')
+         and date_format(date_sub({date_time}, INTERVAL {day_count} DAY),'%Y%m%d')
+group by advertiser_id,campaign_id) ratio
+on report.account_id = ratio.account_id and report.campaign_id = ratio.campaign_id;
+""".format(date_time=self.date_time, day_count=self.day_count)
             TidbConn.upsert(conn=self.conn, sql_buff=sql)
             self.conn.commit()
             TidbConn.conn_close(conn=self.conn)

+ 208 - 5
Apietl/bytedance_etl/Q4Task/AppBytedanceCompanyDesignReportBusi.py

@@ -24,17 +24,205 @@ class AppBytedanceCompanyDesignReportBusi:
             sql = """
 SET sql_mode = 'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
 REPLACE INTO application.app_bytedance_company_design_report_busi_{day_count}d
-SELECT    concat(date_format(date_sub({date_time}, INTERVAL ({day_count} -1 )  DAY),'%Y%m%d'),'-',{date_time})  as
+SELECT
+new.start_end_time,
+new.company_id,
+new.company_name,
+new.org_code,
+new.org_type,
+new.dep_id,
+new.dep_name,
+new.leader_id ,
+new.leader_name ,
+new.material_count,
+new.innovate_num,
+new.cost,
+ifnull(ratio.cost,0) last_cost, 
+ifnull(round((new.cost-ratio.cost)/ratio.cost*100,3),0) cost_ratio,
+new.show,
+new.avg_show_cost ,
+new.click ,
+new.avg_click_cost,
+new.ctr ,
+new.convert ,
+new.convert_cost,
+new.convert_rate,
+new.deep_convert,
+new.deep_convert_cost ,
+new.deep_convert_rate ,
+new.attribution_convert ,
+new.attribution_convert_cost,
+new.attribution_deep_convert,
+new.attribution_deep_convert_cost ,
+new.download_start,
+new.download_start_cost ,
+new.download_start_rate ,
+new.download_finish ,
+new.download_finish_cost,
+new.download_finish_rate,
+new.click_install ,
+new.install_finish,
+new.install_finish_cost ,
+new.install_finish_rate ,
+new.active,
+new.active_cost ,
+new.active_rate ,
+new.register,
+new.active_register_cost,
+new.active_register_rate,
+new.active_pay_amount ,
+new.next_day_open ,
+new.next_day_open_cost,
+new.next_day_open_rate,
+new.attribution_next_day_open_cnt ,
+new.attribution_next_day_open_cost,
+new.attribution_next_day_open_rate,
+new.game_addiction,
+new.game_addiction_cost ,
+new.game_addiction_rate ,
+new.pay_count ,
+new.active_pay_cost ,
+new.active_pay_rate ,
+new.loan_completion ,
+new.loan_completion_cost,
+new.loan_completion_rate,
+new.pre_loan_credit ,
+new.pre_loan_credit_cost,
+new.loan_credit ,
+new.loan_credit_cost,
+new.loan_credit_rate,
+new.in_app_uv ,
+new.in_app_detail_uv,
+new.in_app_cart ,
+new.in_app_pay,
+new.in_app_order,
+new.attribution_game_pay_7d_count ,
+new.attribution_game_pay_7d_cost,
+new.attribution_active_pay_7d_per_count ,
+new.game_pay_cost ,
+new.game_pay_count,
+new.phone ,
+new.form,
+new.map_search,
+new.button,
+new.view,
+new.download,
+new.qq,
+new.lottery ,
+new.vote,
+new.message ,
+new.redirect,
+new.shopping,
+new.consult ,
+new.wechat,
+new.phone_confirm ,
+new.phone_connect ,
+new.consult_effective ,
+new.coupon,
+new.coupon_single_page,
+new.redirect_to_shop,
+new.poi_collect ,
+new.poi_address_click ,
+new.luban_order_cnt ,
+new.luban_order_stat_amount ,
+new.luban_order_roi ,
+new.luban_live_enter_cnt,
+new.live_watch_one_minute_count ,
+new.luban_live_follow_cnt ,
+new.live_fans_club_join_cnt ,
+new.luban_live_comment_cnt,
+new.luban_live_share_cnt,
+new.luban_live_gift_cnt ,
+new.luban_live_gift_amount,
+new.luban_live_slidecart_click_cnt,
+new.luban_live_click_product_cnt,
+new.luban_live_pay_order_count,
+new.luban_live_pay_order_stat_cost,
+new.wechat_login_count,
+new.attribution_wechat_login_30d_count,
+new.wechat_login_cost ,
+new.attribution_wechat_login_30d_cost ,
+new.wechat_first_pay_count,
+new.attribution_wechat_first_pay_30d_count,
+new.wechat_first_pay_cost ,
+new.attribution_wechat_first_pay_30d_cost ,
+new.wechat_first_pay_rate ,
+new.attribution_wechat_first_pay_30d_rate ,
+new.wechat_pay_amount ,
+new.attribution_wechat_pay_30d_amount ,
+new.attribution_wechat_pay_30d_roi,
+new.phone_effective ,
+new.total_play,
+new.valid_play,
+new.valid_play_cost ,
+new.valid_play_rate ,
+new.play_25_feed_break,
+new.play_50_feed_break,
+new.play_75_feed_break,
+new.play_100_feed_break ,
+new.average_play_time_per_play,
+new.play_over_rate,
+new.wifi_play_rate,
+new.wifi_play ,
+new.play_duration_sum ,
+new.advanced_creative_phone_click ,
+new.advanced_creative_counsel_click ,
+new.advanced_creative_form_click,
+new.advanced_creative_coupon_addition ,
+new.advanced_creative_form_submit ,
+new.card_show ,
+new.share ,
+new.comment ,
+new.like,
+new.follow,
+new.home_visited,
+new.ies_challenge_click ,
+new.ies_music_click ,
+new.location_click,
+new.message_action,
+new.click_landing_page,
+new.click_shopwindow,
+new.click_website ,
+new.click_download,
+new.click_call_dy ,
+new.cpc ,
+new.cpm ,
+new.cpa ,
+new.interact_per_cost ,
+new.convert_show_rate ,
+new.play_duration_10s_rate,
+new.play_50_feed_break_rate ,
+new.play_25_feed_break_rate ,
+new.play_duration_5s_rate ,
+new.play_duration_2s_rate ,
+new.average_play_progress ,
+new.play_100_feed_break_rate,
+new.play_duration_3s_rate ,
+new.play_75_feed_break_rate ,
+new.play_duration ,
+new.play_duration_3s,
+new.play_duration_2s,
+new.play_duration_10s ,
+new.average_video_play,
+new.avg_rank,
+new.submit_certification_count,
+new.approval_count,
+new.first_order_count ,
+new.first_rental_order_count,
+new.commute_first_pay_count 
+from 
+(SELECT    
+          concat(date_format(date_sub({date_time}, INTERVAL ({day_count} -1 )  DAY),'%Y%m%d'),'-',{date_time})  as
           start_end_time,
           ifnull(company_id,'-') company_id,
           company_name,
           org_code,
           org_type,
-          id,
-          depart_name,
+          id dep_id,
+          depart_name dep_name,
           ifnull(leader_id,'-') leader_id,
           leader_name,
-          count(DISTINCT signature) num,
+          count(DISTINCT signature) material_count,
           count(DISTINCT innovate_code) innovate_num,
           sum(cost) cost,
           sum(`show`) `show`,
@@ -446,7 +634,22 @@ FROM (
           AND stat_datetime >= date_format( date_sub({date_time}, INTERVAL {day_count}-1 DAY),'%Y%m%d')) info
           GROUP BY 
           company_id,
-          leader_id;
+          leader_id) new
+          left join 
+          (select  ifnull(cma.leader_id,'-') leader_id,round(sum(cost),3) cost
+           FROM media_api.bytedance_material_report_daily report
+           LEFT JOIN (SELECT advertiser_id,signature,material_id FROM `media_api`.bytedance_file_video_get
+           GROUP BY advertiser_id,signature,material_id) video
+           ON report.advertiser_id = video.advertiser_id AND report.material_id = video.material_id
+           LEFT JOIN  (SELECT material_id,leader_id FROM (
+           SELECT material_id,leader_id,row_number() over (PARTITION BY material_id ORDER BY create_time ) rn
+           FROM `jeecg-boot`.ctop_material_ascription ) m WHERE rn = 1 )  cma
+           ON video.signature = cma.material_id
+           WHERE image_mode IN ('CREATIVE_IMAGE_MODE_VIDEO','CREATIVE_IMAGE_MODE_VIDEO_VERTICAL','CREATIVE_IMAGE_MODE_UNION_SPLASH_VIDEO')
+           and  stat_datetime between date_format(date_sub(date_format(date_sub({date_time}, INTERVAL {day_count} DAY),'%Y%m%d'), INTERVAL {day_count}-1 DAY),'%Y%m%d')
+           and date_format(date_sub({date_time}, INTERVAL {day_count} DAY),'%Y%m%d')
+           group by cma.leader_id) ratio
+           on new.leader_id=ratio.leader_id;
             """.format(date_time=self.date_time, day_count=self.day_count)
             TidbConn.upsert(conn=self.conn, sql_buff=sql)
             self.conn.commit()

+ 183 - 3
Apietl/bytedance_etl/Q4Task/AppBytedanceCompanyUserReportBusi.py

@@ -24,6 +24,174 @@ class AppBytedanceCompanyUserReportBusi:
             sql = """
 SET sql_mode = 'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
 REPLACE INTO application.app_bytedance_company_user_report_busi_{day_count}d 
+SELECT 
+new.start_end_time ,
+new.company_id ,
+new.company_name ,
+new.org_code ,
+new.org_type ,
+new.dep_id ,
+new.dep_name ,
+new.leader_id,
+new.leader_name,
+new.account_num,
+new.cost ,
+ifnull(ratio.cost,0) last_cost,                                    
+ifnull(round((new.cost-ratio.cost)/ratio.cost*100,3),0) cost_ratio,
+new.show ,
+new.avg_show_cost,
+new.click,
+new.avg_click_cost ,
+new.ctr,
+new.convert,
+new.convert_cost ,
+new.convert_rate ,
+new.deep_convert ,
+new.deep_convert_cost,
+new.deep_convert_rate,
+new.attribution_convert,
+new.attribution_convert_cost ,
+new.attribution_deep_convert ,
+new.attribution_deep_convert_cost,
+new.download_start ,
+new.download_start_cost,
+new.download_start_rate,
+new.download_finish,
+new.download_finish_cost ,
+new.download_finish_rate ,
+new.click_install,
+new.install_finish ,
+new.install_finish_cost,
+new.install_finish_rate,
+new.active ,
+new.active_cost,
+new.active_rate,
+new.register ,
+new.active_register_cost ,
+new.active_register_rate ,
+new.next_day_open,
+new.next_day_open_cost ,
+new.next_day_open_rate ,
+new.attribution_next_day_open_cnt,
+new.attribution_next_day_open_cost ,
+new.attribution_next_day_open_rate ,
+new.game_addiction ,
+new.game_addiction_cost,
+new.game_addiction_rate,
+new.pay_count,
+new.active_pay_cost,
+new.active_pay_rate,
+new.loan_completion,
+new.loan_completion_cost ,
+new.loan_completion_rate ,
+new.pre_loan_credit,
+new.pre_loan_credit_cost ,
+new.loan_credit,
+new.loan_credit_cost ,
+new.loan_credit_rate ,
+new.in_app_uv,
+new.in_app_detail_uv ,
+new.in_app_cart,
+new.in_app_pay ,
+new.in_app_order ,
+new.attribution_game_pay_7d_count,
+new.attribution_game_pay_7d_cost ,
+new.attribution_active_pay_7d_per_count,
+new.game_pay_cost,
+new.game_pay_count ,
+new.phone,
+new.form ,
+new.map_search ,
+new.button ,
+new.view ,
+new.download ,
+new.qq ,
+new.lottery,
+new.vote ,
+new.message,
+new.redirect ,
+new.shopping ,
+new.consult,
+new.wechat ,
+new.phone_confirm,
+new.phone_connect,
+new.consult_effective,
+new.coupon ,
+new.coupon_single_page ,
+new.redirect_to_shop ,
+new.poi_collect,
+new.poi_address_click,
+new.luban_order_cnt,
+new.luban_order_stat_amount,
+new.luban_order_roi,
+new.luban_live_enter_cnt ,
+new.live_watch_one_minute_count,
+new.luban_live_follow_cnt,
+new.live_fans_club_join_cnt,
+new.luban_live_comment_cnt ,
+new.luban_live_share_cnt ,
+new.luban_live_gift_cnt,
+new.luban_live_gift_amount ,
+new.luban_live_slidecart_click_cnt ,
+new.luban_live_click_product_cnt ,
+new.luban_live_pay_order_count ,
+new.luban_live_pay_order_stat_cost ,
+new.live_component_click_count ,
+new.live_component_click_cost,
+new.live_component_click_rate,
+new.wechat_login_count ,
+new.attribution_wechat_login_30d_count ,
+new.wechat_login_cost,
+new.attribution_wechat_login_30d_cost,
+new.wechat_first_pay_count ,
+new.attribution_wechat_first_pay_30d_count ,
+new.wechat_first_pay_cost,
+new.attribution_wechat_first_pay_30d_cost,
+new.wechat_first_pay_rate,
+new.attribution_wechat_first_pay_30d_rate,
+new.wechat_pay_amount,
+new.attribution_wechat_pay_30d_amount,
+new.attribution_wechat_pay_30d_roi ,
+new.phone_effective,
+new.total_play ,
+new.valid_play ,
+new.valid_play_cost,
+new.valid_play_rate,
+new.play_25_feed_break ,
+new.play_50_feed_break ,
+new.play_75_feed_break ,
+new.play_100_feed_break,
+new.average_play_time_per_play ,
+new.play_over_rate ,
+new.wifi_play_rate ,
+new.wifi_play,
+new.play_duration_sum,
+new.advanced_creative_phone_click,
+new.advanced_creative_counsel_click,
+new.advanced_creative_form_click ,
+new.advanced_creative_coupon_addition,
+new.advanced_creative_form_submit,
+new.card_show,
+new.share,
+new.comment,
+new.like ,
+new.follow ,
+new.home_visited ,
+new.ies_challenge_click,
+new.ies_music_click,
+new.location_click ,
+new.message_action ,
+new.click_landing_page ,
+new.click_shopwindow ,
+new.click_website,
+new.click_download ,
+new.click_call_dy,
+new.submit_certification_count ,
+new.approval_count ,
+new.first_rental_order_count ,
+new.first_order_count,
+new.commute_first_pay_count
+from (
 SELECT
 concat(date_format(date_sub({date_time}, INTERVAL ({day_count} -1 )  DAY),'%Y%m%d'),'-',{date_time})  start_end_time,
                ifnull(company_id,'-') company_id,
@@ -31,10 +199,10 @@ concat(date_format(date_sub({date_time}, INTERVAL ({day_count} -1 )  DAY),'%Y%m%
                org_code,
                org_type,
                dep_id,
-               depart_name,
+               depart_name dep_name,
                ifnull(leader_id,'-') leader_id,
                trim(leader_name) leader_name,
-               count(DISTINCT account_id),
+               count(DISTINCT account_id) account_num,
                sum(cost) cost,
                sum(`show`) `show`,
                ifnull(round(sum(cost) / sum(`show`), 3),0) avg_show_cost,
@@ -404,7 +572,19 @@ LEFT JOIN `jeecg-boot`.sys_depart pa ON pa.id = de.dep_id
 ) info
 GROUP BY
 company_id,
-leader_id;
+leader_id) new
+LEFT JOIN
+(select ifnull(su.leader_id,'-') leader_id,sum(report.cost) cost  from
+(select advertiser_id account_id,sum(cost) cost from media_api.bytedance_advertiser_report_daily
+where stat_datetime between date_format(date_sub(date_format(date_sub({date_time}, INTERVAL {day_count} DAY),'%Y%m%d'), INTERVAL {day_count}-1 DAY),'%Y%m%d')
+and date_format(date_sub({date_time}, INTERVAL {day_count} DAY),'%Y%m%d')
+group by advertiser_id) report
+LEFT JOIN  `jeecg-boot`.ctop_user_allocation cua
+ON report.account_id = cua.account_id
+LEFT JOIN `jeecg-boot`.sys_user su
+ON su.id = cua.user_id
+group by su.leader_id) ratio
+ON new.leader_id=ratio.leader_id ;
             """.format(date_time=self.date_time, day_count=self.day_count)
             TidbConn.upsert(conn=self.conn, sql_buff=sql)
             self.conn.commit()

+ 17 - 1
Apietl/bytedance_etl/Q4Task/AppBytedanceImageReportBusi.py

@@ -63,6 +63,8 @@ SELECT concat(date_format(date_sub({date_time}, INTERVAL ({day_count} -1 )  DAY)
        #素材id
        video.filename  as video_name,
        report.cost,
+       ifnull(ratio.cost,0) last_cost,
+       ifnull(round((report.cost-ratio.cost)/ratio.cost*100,3),0) cost_ratio,
        report.`show`,
        report.avg_show_cost,
        report.click,
@@ -441,7 +443,21 @@ ON report.account_id = video.advertiser_id AND report.material_id = video.materi
                    ON cp.product_id = product.id
          LEFT JOIN `jeecg-boot`.sys_user su
                    ON cua.user_id = su.id
-        where cua.project_id={project_id}            
+         LEFT JOIN 
+         (select advertiser_id account_id,material_id,sum(cost) cost  FROM media_api.bytedance_material_report_daily
+    WHERE image_mode IN ('CREATIVE_IMAGE_MODE_SMALL',
+     'CREATIVE_IMAGE_MODE_LARGE',
+     'CREATIVE_IMAGE_MODE_LARGE_VERTICAL',
+     'CREATIVE_IMAGE_MODE_GROUP',
+     'CREATIVE_IMAGE_MODE_GIF',
+     'TOUTIAO_SEARCH_AD_IMAGE',
+     'SEARCH_AD_SMALL_IMAGE',
+     'CREATIVE_IMAGE_MODE_UNION_SPLASH')
+         and  stat_datetime between date_format(date_sub(date_format(date_sub({date_time}, INTERVAL {day_count} DAY),'%Y%m%d'), INTERVAL {day_count}-1 DAY),'%Y%m%d')
+         and date_format(date_sub({date_time}, INTERVAL {day_count} DAY),'%Y%m%d')
+         group by advertiser_id,material_id) ratio
+         on report.account_id = ratio.account_id and report.material_id = ratio.material_id 
+        where cua.project_id={project_id};       
                    
 """.format(date_time=self.date_time, day_count=self.day_count, project_id=project_id[0])
                     print(sql)

+ 203 - 38
Apietl/bytedance_etl/Q4Task/AppBytedanceProductReportBusi.py

@@ -24,22 +24,187 @@ class AppBytedanceProductReportBusi:
             sql = """
 SET sql_mode = 'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
 REPLACE INTO application.app_bytedance_product_report_busi_{day_count}d
-SELECT concat(date_format(date_sub({date_time}, INTERVAL ({day_count} -1 )  DAY),'%Y%m%d'),'-',{date_time}) time,
-               uc.company_id,
-               CASE
-               WHEN uc.company_id = 'd57fecdcf7a94d009736d9c850731582' THEN
-               '北京汇创思拓数字科技有限公司'
-               WHEN uc.company_id = '6608ed13c0dd42de93c790dbdf124234' THEN
-               '广州汇创思拓数字科技有限公司'
-               WHEN uc.company_id = '4b10089775c040119e139087517aed88' THEN
-               '上海汇创思拓数字科技有限公司'
-               ELSE
-               uc.company_name
-               END                              company_name,
-               cn.id                                product_id,
-               cn.product_name,
-               count(report.advertiser_id)          account_count,
-               sum(cost) cost,
+SELECT
+new.start_end_time,
+new.company_id,
+new.company_name,
+ifnull(new.product_id,0) product_id,
+new.product_name,
+new.account_num ,
+new.cost,
+ifnull(ratio.cost,0) last_cost,                                       
+ifnull(round((new.cost-ratio.cost)/ratio.cost*100,3),0) cost_ratio,   
+new.show,
+new.avg_show_cost ,
+new.click ,
+new.avg_click_cost,
+new.ctr ,
+new.convert ,
+new.convert_cost,
+new.convert_rate,
+new.deep_convert,
+new.deep_convert_cost ,
+new.deep_convert_rate ,
+new.attribution_convert ,
+new.attribution_convert_cost,
+new.attribution_deep_convert,
+new.attribution_deep_convert_cost ,
+new.download_start,
+new.download_start_cost ,
+new.download_start_rate ,
+new.download_finish ,
+new.download_finish_cost,
+new.download_finish_rate,
+new.click_install ,
+new.install_finish,
+new.install_finish_cost ,
+new.install_finish_rate ,
+new.active,
+new.active_cost ,
+new.active_rate ,
+new.register,
+new.active_register_cost,
+new.active_register_rate,
+new.next_day_open ,
+new.next_day_open_cost,
+new.next_day_open_rate,
+new.attribution_next_day_open_cnt ,
+new.attribution_next_day_open_cost,
+new.attribution_next_day_open_rate,
+new.game_addiction,
+new.game_addiction_cost ,
+new.game_addiction_rate ,
+new.pay_count ,
+new.active_pay_cost ,
+new.active_pay_rate ,
+new.loan_completion ,
+new.loan_completion_cost,
+new.loan_completion_rate,
+new.pre_loan_credit ,
+new.pre_loan_credit_cost,
+new.loan_credit ,
+new.loan_credit_cost,
+new.loan_credit_rate,
+new.in_app_uv ,
+new.in_app_detail_uv,
+new.in_app_cart ,
+new.in_app_pay,
+new.in_app_order,
+new.attribution_game_pay_7d_count ,
+new.attribution_game_pay_7d_cost,
+new.attribution_active_pay_7d_per_count ,
+new.game_pay_cost ,
+new.game_pay_count,
+new.phone ,
+new.form,
+new.map_search,
+new.button,
+new.view,
+new.download,
+new.qq,
+new.lottery ,
+new.vote,
+new.message ,
+new.redirect,
+new.shopping,
+new.consult ,
+new.wechat,
+new.phone_confirm ,
+new.phone_connect ,
+new.consult_effective ,
+new.coupon,
+new.coupon_single_page,
+new.redirect_to_shop,
+new.poi_collect ,
+new.poi_address_click ,
+new.luban_order_cnt ,
+new.luban_order_stat_amount ,
+new.luban_order_roi ,
+new.luban_live_enter_cnt,
+new.live_watch_one_minute_count ,
+new.luban_live_follow_cnt ,
+new.live_fans_club_join_cnt ,
+new.luban_live_comment_cnt,
+new.luban_live_share_cnt,
+new.luban_live_gift_cnt ,
+new.luban_live_gift_amount,
+new.luban_live_slidecart_click_cnt,
+new.luban_live_click_product_cnt,
+new.luban_live_pay_order_count,
+new.luban_live_pay_order_stat_cost,
+new.live_component_click_count,
+new.live_component_click_cost ,
+new.live_component_click_rate ,
+new.wechat_login_count,
+new.attribution_wechat_login_30d_count,
+new.wechat_login_cost ,
+new.attribution_wechat_login_30d_cost ,
+new.wechat_first_pay_count,
+new.attribution_wechat_first_pay_30d_count,
+new.wechat_first_pay_cost ,
+new.attribution_wechat_first_pay_30d_cost ,
+new.wechat_first_pay_rate ,
+new.attribution_wechat_first_pay_30d_rate ,
+new.wechat_pay_amount ,
+new.attribution_wechat_pay_30d_amount ,
+new.attribution_wechat_pay_30d_roi,
+new.phone_effective ,
+new.total_play,
+new.valid_play,
+new.valid_play_cost ,
+new.valid_play_rate ,
+new.play_25_feed_break,
+new.play_50_feed_break,
+new.play_75_feed_break,
+new.play_100_feed_break ,
+new.average_play_time_per_play,
+new.play_over_rate,
+new.wifi_play_rate,
+new.wifi_play ,
+new.play_duration_sum ,
+new.advanced_creative_phone_click ,
+new.advanced_creative_counsel_click ,
+new.advanced_creative_form_click,
+new.advanced_creative_coupon_addition ,
+new.advanced_creative_form_submit ,
+new.card_show ,
+new.share ,
+new.comment ,
+new.like,
+new.follow,
+new.home_visited,
+new.ies_challenge_click ,
+new.ies_music_click ,
+new.location_click,
+new.message_action,
+new.click_landing_page,
+new.click_shopwindow,
+new.click_website ,
+new.click_download,
+new.click_call_dy ,
+new.submit_certification_count,
+new.approval_count,
+new.first_rental_order_count,
+new.first_order_count ,
+new.commute_first_pay_count 
+from (
+SELECT
+concat(date_format(date_sub({date_time}, INTERVAL ({day_count} -1 )  DAY),'%Y%m%d'),'-',{date_time}) start_end_time,
+uc.company_id,
+CASE
+WHEN uc.company_id = 'd57fecdcf7a94d009736d9c850731582' THEN
+'北京汇创思拓数字科技有限公司'
+WHEN uc.company_id = '6608ed13c0dd42de93c790dbdf124234' THEN
+'广州汇创思拓数字科技有限公司'
+WHEN uc.company_id = '4b10089775c040119e139087517aed88' THEN
+'上海汇创思拓数字科技有限公司'
+ELSE
+uc.company_name
+END company_name,
+ cp.product_id,
+ cn.product_name,
+ count(report.advertiser_id) account_num,
+ sum(cost) cost,
                sum(`show`) `show`,
                ifnull(round(sum(cost) / sum(`show`), 3),0) avg_show_cost,
                sum(click)click,
@@ -193,28 +358,28 @@ SELECT concat(date_format(date_sub({date_time}, INTERVAL ({day_count} -1 )  DAY)
                sum(first_order_count)first_rental_order_count,
                sum(first_rental_order_count) first_order_count,
                sum(commute_first_pay_count)commute_first_pay_count
-FROM (
-         SELECT *
-         FROM media_api.bytedance_advertiser_report_daily
-         WHERE stat_datetime >= date_format(date_sub({date_time}, INTERVAL ({day_count} -1 )  DAY),'%Y%m%d')) report
-         LEFT JOIN `jeecg-boot`.ctop_user_allocation cua ON report.advertiser_id = cua.account_id
-         LEFT JOIN `jeecg-boot`.ctop_project cp ON cua.project_id = cp.id
-         LEFT JOIN `jeecg-boot`.ctop_product cn ON cp.product_id = cn.id
-         LEFT JOIN `jeecg-boot`.user_company uc ON uc.user_id = cua.user_id
-where cn.id is not null
-GROUP BY uc.company_id,
-         CASE
-             WHEN uc.company_id = 'd57fecdcf7a94d009736d9c850731582' THEN
-                 '北京汇创思拓数字科技有限公司'
-             WHEN uc.company_id = '6608ed13c0dd42de93c790dbdf124234' THEN
-                 '广州汇创思拓数字科技有限公司'
-             WHEN uc.company_id = '4b10089775c040119e139087517aed88' THEN
-                 '上海汇创思拓数字科技有限公司'
-             ELSE
-                 uc.company_name
-             END,
-         cn.id,
-         cn.product_name;
+FROM
+(SELECT * FROM media_api.bytedance_advertiser_report_daily
+WHERE stat_datetime >= date_format(date_sub({date_time}, INTERVAL ({day_count} -1 )  DAY),'%Y%m%d')) report
+LEFT JOIN `jeecg-boot`.ctop_user_allocation cua ON report.advertiser_id = cua.account_id
+LEFT JOIN `jeecg-boot`.ctop_project cp ON cua.project_id = cp.id
+LEFT JOIN `jeecg-boot`.ctop_product cn ON cp.product_id = cn.id
+LEFT JOIN `jeecg-boot`.user_company uc ON uc.user_id = cua.user_id
+GROUP BY
+uc.company_id,
+cp.product_id) new
+LEFT JOIN 
+(select ifnull(cp.product_id,'0') product_id,sum(report.cost) cost  from
+(select advertiser_id account_id,sum(cost) cost from media_api.bytedance_advertiser_report_daily
+where stat_datetime between date_format(date_sub(date_format(date_sub({date_time}, INTERVAL {day_count} DAY),'%Y%m%d'), INTERVAL {day_count}-1 DAY),'%Y%m%d')
+and date_format(date_sub({date_time}, INTERVAL {day_count} DAY),'%Y%m%d')
+group by advertiser_id) report
+LEFT JOIN  `jeecg-boot`.ctop_user_allocation cua
+ON report.account_id = cua.account_id
+LEFT JOIN `jeecg-boot`.ctop_project cp
+ON cua.project_id = cp.id
+group by cp.product_id) ratio
+ON new.product_id=ratio.product_id;
 """.format(date_time=self.date_time, day_count=self.day_count)
             TidbConn.upsert(conn=self.conn, sql_buff=sql)
             self.conn.commit()

+ 183 - 14
Apietl/bytedance_etl/Q4Task/AppBytedanceProjectReportBusi.py

@@ -25,7 +25,173 @@ class AppBytedanceCompanyUserReportBusi:
 SET sql_mode = 'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
 REPLACE INTO application.app_bytedance_project_report_busi_{day_count}d 
 SELECT
-concat(date_format(date_sub({date_time}, INTERVAL ({day_count} -1 )  DAY),'%Y%m%d'),'-',{date_time}) time,
+new.start_end_time,
+new.company_id,
+new.company_name,
+ifnull(new.product_id,0) product_id,
+new.product_name,
+ifnull(new.project_id,0) project_id,
+new.project_name,
+new.account_num ,
+new.cost,
+ifnull(ratio.cost,0) last_cost,                                       
+ifnull(round((new.cost-ratio.cost)/ratio.cost*100,3),0) cost_ratio,   
+new.show,
+new.avg_show_cost ,
+new.click ,
+new.avg_click_cost,
+new.ctr ,
+new.convert ,
+new.convert_cost,
+new.convert_rate,
+new.deep_convert,
+new.deep_convert_cost ,
+new.deep_convert_rate ,
+new.attribution_convert ,
+new.attribution_convert_cost,
+new.attribution_deep_convert,
+new.attribution_deep_convert_cost ,
+new.download_start,
+new.download_start_cost ,
+new.download_start_rate ,
+new.download_finish ,
+new.download_finish_cost,
+new.download_finish_rate,
+new.click_install ,
+new.install_finish,
+new.install_finish_cost ,
+new.install_finish_rate ,
+new.active,
+new.active_cost ,
+new.active_rate ,
+new.register,
+new.active_register_cost,
+new.active_register_rate,
+new.next_day_open ,
+new.next_day_open_cost,
+new.next_day_open_rate,
+new.attribution_next_day_open_cnt ,
+new.attribution_next_day_open_cost,
+new.attribution_next_day_open_rate,
+new.game_addiction,
+new.game_addiction_cost ,
+new.game_addiction_rate ,
+new.pay_count ,
+new.active_pay_cost ,
+new.active_pay_rate ,
+new.loan_completion ,
+new.loan_completion_cost,
+new.loan_completion_rate,
+new.pre_loan_credit ,
+new.pre_loan_credit_cost,
+new.loan_credit ,
+new.loan_credit_cost,
+new.loan_credit_rate,
+new.in_app_uv ,
+new.in_app_detail_uv,
+new.in_app_cart ,
+new.in_app_pay,
+new.in_app_order,
+new.attribution_game_pay_7d_count ,
+new.attribution_game_pay_7d_cost,
+new.attribution_active_pay_7d_per_count ,
+new.game_pay_cost ,
+new.game_pay_count,
+new.phone ,
+new.form,
+new.map_search,
+new.button,
+new.view,
+new.download,
+new.qq,
+new.lottery ,
+new.vote,
+new.message ,
+new.redirect,
+new.shopping,
+new.consult ,
+new.wechat,
+new.phone_confirm ,
+new.phone_connect ,
+new.consult_effective ,
+new.coupon,
+new.coupon_single_page,
+new.redirect_to_shop,
+new.poi_collect ,
+new.poi_address_click ,
+new.luban_order_cnt ,
+new.luban_order_stat_amount ,
+new.luban_order_roi ,
+new.luban_live_enter_cnt,
+new.live_watch_one_minute_count ,
+new.luban_live_follow_cnt ,
+new.live_fans_club_join_cnt ,
+new.luban_live_comment_cnt,
+new.luban_live_share_cnt,
+new.luban_live_gift_cnt ,
+new.luban_live_gift_amount,
+new.luban_live_slidecart_click_cnt,
+new.luban_live_click_product_cnt,
+new.luban_live_pay_order_count,
+new.luban_live_pay_order_stat_cost,
+new.live_component_click_count,
+new.live_component_click_cost ,
+new.live_component_click_rate ,
+new.wechat_login_count,
+new.attribution_wechat_login_30d_count,
+new.wechat_login_cost ,
+new.attribution_wechat_login_30d_cost ,
+new.wechat_first_pay_count,
+new.attribution_wechat_first_pay_30d_count,
+new.wechat_first_pay_cost ,
+new.attribution_wechat_first_pay_30d_cost ,
+new.wechat_first_pay_rate ,
+new.attribution_wechat_first_pay_30d_rate ,
+new.wechat_pay_amount ,
+new.attribution_wechat_pay_30d_amount ,
+new.attribution_wechat_pay_30d_roi,
+new.phone_effective ,
+new.total_play,
+new.valid_play,
+new.valid_play_cost ,
+new.valid_play_rate ,
+new.play_25_feed_break,
+new.play_50_feed_break,
+new.play_75_feed_break,
+new.play_100_feed_break ,
+new.average_play_time_per_play,
+new.play_over_rate,
+new.wifi_play_rate,
+new.wifi_play ,
+new.play_duration_sum ,
+new.advanced_creative_phone_click ,
+new.advanced_creative_counsel_click ,
+new.advanced_creative_form_click,
+new.advanced_creative_coupon_addition ,
+new.advanced_creative_form_submit ,
+new.card_show ,
+new.share ,
+new.comment ,
+new.like,
+new.follow,
+new.home_visited,
+new.ies_challenge_click ,
+new.ies_music_click ,
+new.location_click,
+new.message_action,
+new.click_landing_page,
+new.click_shopwindow,
+new.click_website ,
+new.click_download,
+new.click_call_dy ,
+new.submit_certification_count,
+new.approval_count,
+new.first_rental_order_count,
+new.first_order_count ,
+new.commute_first_pay_count 
+from (
+SELECT
+concat(date_format(date_sub({date_time}, INTERVAL ({day_count} -1 )  DAY),'%Y%m%d'),'-',{date_time}) start_end_time,
 uc.company_id,
 CASE
 WHEN uc.company_id = 'd57fecdcf7a94d009736d9c850731582' THEN
@@ -197,17 +363,8 @@ END company_name,
                sum(first_rental_order_count) first_order_count,
                sum(commute_first_pay_count)commute_first_pay_count
 FROM
-(
-SELECT
-*
-FROM
-media_api.bytedance_advertiser_report_daily
-WHERE
-stat_datetime >= date_format(
-date_sub({date_time}, INTERVAL ({day_count} -1 )  DAY),
-'%Y%m%d'
-)
-) report
+(SELECT * FROM media_api.bytedance_advertiser_report_daily
+WHERE stat_datetime >= date_format(date_sub({date_time}, INTERVAL ({day_count} -1 )  DAY),'%Y%m%d')) report
 LEFT JOIN `jeecg-boot`.ctop_user_allocation cua ON report.advertiser_id = cua.account_id
 LEFT JOIN `jeecg-boot`.ctop_project cp ON cua.project_id = cp.id
 LEFT JOIN `jeecg-boot`.ctop_product cn ON cp.product_id = cn.id
@@ -224,8 +381,20 @@ WHEN uc.company_id = '4b10089775c040119e139087517aed88' THEN
 ELSE
 uc.company_name
 END,cua.project_id,
-cp.project_name
-having sum(cost)>0""".format(date_time=self.date_time, day_count=self.day_count)
+cp.project_name) new
+LEFT JOIN 
+(select ifnull(cp.product_id,'0') product_id,ifnull(cua.project_id,'0') project_id,sum(report.cost) cost  from
+(select advertiser_id account_id,sum(cost) cost from media_api.bytedance_advertiser_report_daily
+where stat_datetime between date_format(date_sub(date_format(date_sub({date_time}, INTERVAL {day_count} DAY),'%Y%m%d'), INTERVAL {day_count}-1 DAY),'%Y%m%d')
+and date_format(date_sub({date_time}, INTERVAL {day_count} DAY),'%Y%m%d')
+group by advertiser_id) report
+LEFT JOIN  `jeecg-boot`.ctop_user_allocation cua
+ON report.account_id = cua.account_id
+LEFT JOIN `jeecg-boot`.ctop_project cp
+ON cua.project_id = cp.id
+group by cua.project_id) ratio
+ON new.project_id=ratio.project_id;
+""".format(date_time=self.date_time, day_count=self.day_count)
             print(sql)
             TidbConn.upsert(conn=self.conn, sql_buff=sql)
             self.conn.commit()

+ 10 - 2
Apietl/bytedance_etl/Q4Task/AppBytedanceVideoReportBusi.py

@@ -91,6 +91,8 @@ SELECT concat(date_format(date_sub({date_time}, INTERVAL ({day_count} -1 )  DAY)
        up.company_id   clip_company_id,
        #剪辑公司
        report.cost,
+       ifnull(ratio.cost,0) last_cost,
+       ifnull(round((report.cost-ratio.cost)/ratio.cost*100,3),0) cost_ratio,
        report.`show`,
        report.avg_show_cost,
        report.click,
@@ -482,9 +484,15 @@ FROM (
          LEFT JOIN `jeecg-boot`.sys_user plan ON plan.id = cma.plan_id
          LEFT JOIN `jeecg-boot`.sys_user leader ON leader.id = cma.leader_id
          LEFT JOIN `jeecg-boot`.user_company up ON up.user_id = cma.clip_id
+         LEFT JOIN 
+         (select advertiser_id account_id,material_id,sum(cost) cost  FROM media_api.bytedance_material_report_daily
+         WHERE image_mode IN ('CREATIVE_IMAGE_MODE_VIDEO','CREATIVE_IMAGE_MODE_VIDEO_VERTICAL','CREATIVE_IMAGE_MODE_UNION_SPLASH_VIDEO')
+         and  stat_datetime between date_format(date_sub(date_format(date_sub({date_time}, INTERVAL {day_count} DAY),'%Y%m%d'), INTERVAL {day_count}-1 DAY),'%Y%m%d')
+         and date_format(date_sub({date_time}, INTERVAL {day_count} DAY),'%Y%m%d')
+         group by advertiser_id,material_id) ratio
+         on report.account_id = ratio.account_id and report.material_id = ratio.material_id 
          where cua.account_id = {account_id};
-         
-    """.format(date_time=self.date_time,
+         """.format(date_time=self.date_time,
                                                         day_count=self.day_count,
                                                         account_id=account_id[0])
                     TidbConn.upsert(conn=self.conn, sql_buff=sql)

+ 1 - 1
Apietl/kwai_etl/Q4Task/AppKuaishouAccountReportBusi.py

@@ -325,7 +325,7 @@ WHERE
 cua.account_id IS NOT NULL) bei 
 ON report.account_id = bei.account_id
 LEFT JOIN  (select advertiser_id account_id,round(sum(charge),3) charge from `media_api`.kuaishou_account_report_daily a  
-where stat_date between date_format(date_sub(date_format(date_sub({date_time}, INTERVAL 7 DAY),'%Y%m%d'), INTERVAL {day_count}-1 DAY),'%Y%m%d') and date_format(date_sub({date_time}, INTERVAL {day_count} DAY),'%Y%m%d') 
+where stat_date between date_format(date_sub(date_format(date_sub({date_time}, INTERVAL {day_count} DAY),'%Y%m%d'), INTERVAL {day_count}-1 DAY),'%Y%m%d') and date_format(date_sub({date_time}, INTERVAL {day_count} DAY),'%Y%m%d') 
 group by advertiser_id) ratio
 ON report.account_id = ratio.account_id;
             """.format(date_time=self.date_time, day_count=self.day_count)

+ 8 - 2
Apietl/kwai_etl/Q4Task/AppKuaishouCampaignReportBusi.py

@@ -42,6 +42,8 @@ bei.user_id,
 bei.leader_id,
 bei.org_code,
 report.charge,
+ifnull(ratio.charge,0) last_charge,
+ifnull(round((report.charge-ratio.charge)/ratio.charge*100,3),0) charge_ratio,
 report.`show`,
 report.photo_click,
 report.aclick,
@@ -296,7 +298,6 @@ sum(event_measurement_house) event_measurement_house,
 ifnull(round(sum(ad_photo_played_2s_ratio)/{day_count}*100,3),0) ad_photo_played_2s_ratio,
 ifnull(round(sum(event_outbound_call_ratio)/sum(activation)*100,3),0) event_outbound_call_ratio,
 sum(event_ad_watch_times) event_ad_watch_times
-
 from 
  media_api.kuaishou_campaign_report_daily  a  
  where stat_date >= date_format(date_sub({date_time}, INTERVAL {day_count} -1  DAY),'%Y%m%d')
@@ -326,7 +327,12 @@ LEFT JOIN `jeecg-boot`.user_company uc ON uc.user_id = cua.user_id
 LEFT JOIN `jeecg-boot`.ctop_product product ON product.id = cp.product_id
 WHERE
 cua.account_id IS NOT NULL) bei 
-ON report.account_id = bei.account_id;
+ON report.account_id = bei.account_id
+LEFT JOIN
+(select advertiser_id account_id,campaign_id,round(sum(charge),3) charge from `media_api`.kuaishou_campaign_report_daily a  
+where stat_date between date_format(date_sub(date_format(date_sub({date_time}, INTERVAL {day_count} DAY),'%Y%m%d'), INTERVAL {day_count}-1 DAY),'%Y%m%d') and date_format(date_sub({date_time}, INTERVAL {day_count} DAY),'%Y%m%d')
+group by advertiser_id,campaign_id) ratio
+on report.account_id = ratio.account_id and report.campaign_id=ratio.campaign_id;
 """.format(date_time=self.date_time, day_count=self.day_count)
             print(sql)
             TidbConn.upsert(conn=self.conn, sql_buff=sql)

+ 251 - 109
Apietl/kwai_etl/Q4Task/AppKuaishouCompanyDesignReportBusi.py

@@ -24,28 +24,168 @@ class AppKuaishouCompanyDesignReportBusi:
 SET sql_mode = 'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
 REPLACE INTO application.app_kuaishou_company_design_report_busi_{day_count}d 
 select
-concat(date_format(date_sub({date_time}, INTERVAL ({day_count} -1 )  DAY),'%Y%m%d'),'-',{date_time}) start_end_time,
-ifnull(company_id,'-') company_id,
-company_name,
-org_code,
-org_type,
-id,
-depart_name,
-ifnull(leader_id,'-') leader_id,
-leader_name,
-count(DISTINCT signature) num,
-count(distinct innovate_code) innovate_num,
-sum(charge) charge,
+new.start_end_time,
+ifnull(new.company_id,'-') company_id,
+new.company_name,
+new.org_code,
+new.org_type,
+new.dep_id,
+new.dep_name,
+ifnull(new.leader_id,'-') leader_id,
+new.leader_name,
+new.material_count,
+new.innovate_num,
+new.charge,
+ifnull(ratio.charge,0) last_charge,
+ifnull(round((new.charge-ratio.charge)/ratio.charge*100,3),0) charge_ratio,
+new.show ,
+new.photo_click,
+new.aclick,
+new.bclick,
+new.photo_click_ratio,
+new.play_3s_ratio ,
+new.action_ratio,
+new.impression_1k_cost,
+new.photo_click_cost ,
+new.action_cost,
+new.share,
+new.comment ,
+new.like ,
+new.follow,
+new.cancel_follow ,
+new.report,
+new.block,
+new.negative,
+new.submit,
+new.download_started ,
+new.download_completed,
+new.activation ,
+new.event_pay_first_day ,
+new.event_pay_purchase_amount_first_day,
+new.event_pay_first_day_roi,
+new.event_pay,
+new.event_pay_purchase_amount ,
+new.event_pay_roi ,
+new.event_register,
+new.event_register_cost ,
+new.event_register_ratio,
+new.event_jin_jian_app,
+new.event_jin_jian_app_cost,
+new.event_credit_grant_app ,
+new.event_credit_grant_app_cost,
+new.event_credit_grant_app_ratio ,
+new.event_order_paid ,
+new.event_order_paid_purchase_amount,
+new.event_order_paid_cost,
+new.event_next_day_stay ,
+new.event_next_day_stay_cost,
+new.event_next_day_stay_ratio ,
+new.form_count ,
+new.form_cost,
+new.form_action_ratio,
+new.event_jin_jian_landing_page,
+new.event_jin_jian_landing_page_cost,
+new.event_credit_grant_landing_page ,
+new.event_credit_grant_landing_page_cost,
+new.event_credit_grant_landing_page_ratio ,
+new.event_valid_clues,
+new.event_valid_clues_cost ,
+new.event_add_wechat ,
+new.event_add_wechat_cost,
+new.event_add_wechat_ratio ,
+new.event_get_through,
+new.event_get_through_cost ,
+new.event_get_through_ratio,
+new.event_app_invoked,
+new.event_app_invoked_cost ,
+new.event_app_invoked_ratio,
+new.event_credit_grant_landing_ratio,
+new.play_5s_ratio ,
+new.play_end_ratio,
+new.event_new_user_pay,
+new.event_new_user_pay_cost,
+new.event_new_user_pay_ratio,
+new.click_1k_cost ,
+new.ad_product_cnt,
+new.event_goods_view ,
+new.merchant_reco_fans,
+new.event_order_amount_roi ,
+new.event_goods_view_cost,
+new.merchant_reco_fans_cost,
+new.event_button_click,
+new.event_button_click_cost,
+new.event_button_click_ratio,
+new.event_order_paid_roi,
+new.event_new_user_jinjian_app,
+new.event_new_user_jinjian_app_cost ,
+new.event_new_user_jinjian_app_roi,
+new.event_new_user_credit_grant_app ,
+new.event_new_user_credit_grant_app_cost,
+new.event_new_user_credit_grant_app_roi,
+new.event_new_user_jinjian_page,
+new.event_new_user_jinjian_page_cost,
+new.event_new_user_jinjian_page_roi ,
+new.event_new_user_credit_grant_page,
+new.event_new_user_credit_grant_page_cost ,
+new.event_new_user_credit_grant_page_roi,
+new.event_appoint_form,
+new.event_appoint_form_cost,
+new.event_appoint_form_ratio,
+new.event_appoint_jump_click,
+new.event_appoint_jump_click_cost,
+new.event_appoint_jump_click_ratio,
+new.union_event_pay_purchase_amount_7d ,
+new.union_event_pay_purchase_amount_7d_roi,
+new.event_order_successed,
+new.ad_photo_played_10s_ratio ,
+new.key_action_cost,
+new.event_add_shopping_cart_cost ,
+new.event_ad_watch_times_ratio,
+new.event_outbound_call_cost,
+new.event_outbound_call ,
+new.action_new_ratio ,
+new.event_watch_app_ad,
+new.event_multi_conversion_cost,
+new.event_phone_card_activate ,
+new.ad_photo_played_75percent_ratio ,
+new.key_action_ratio ,
+new.event_ad_watch_times_cost ,
+new.event_add_shopping_cart,
+new.key_action ,
+new.event_multi_conversion ,
+new.event_wechat_connected ,
+new.event_dsp_gift_form ,
+new.event_intention_confirmed ,
+new.event_phone_get_through,
+new.event_multi_conversion_ratio ,
+new.event_measurement_house,
+new.ad_photo_played_2s_ratio,
+new.event_outbound_call_ratio ,
+new.event_ad_watch_times
+from
+(select
+  concat(date_format(date_sub({date_time}, INTERVAL {day_count}-1 DAY),'%Y%m%d'),'-',{date_time}) start_end_time,
+  company_id,
+  company_name,
+  org_code,
+  org_type,
+  id dep_id,
+  depart_name dep_name,
+  leader_id,
+  leader_name,
+  count(DISTINCT signature) material_count,
+  count(distinct innovate_code) innovate_num,
+  sum(charge) charge,
 sum(`show`) `show`,
 sum(photo_click)photo_click,
 sum(aclick)  aclick,
 sum(bclick) bclick,
-ifnull(round(sum(photo_click)/sum(`show`),3),0) photo_click_ratio,
-ifnull(round(sum(play_3s_ratio)/{day_count}*100,3),0)  play_3s_ratio,
-ifnull(round(sum(bclick)/sum(aclick)*100,3),0) action_ratio,
-ifnull(round(sum(impression_1k_cost)/{day_count},3),0) impression_1k_cost,
-ifnull(round(sum(charge)/sum(photo_click),3),0) photo_click_cost,
-ifnull(round(sum(charge)/sum(bclick),3),0) action_cost,
+round(sum(photo_click)/sum(`show`),3) photo_click_ratio,
+round(sum(play_3s_ratio)/{day_count}*100,3)  play_3s_ratio,
+round(sum(bclick)/sum(aclick)*100,3) action_ratio,
+round(sum(impression_1k_cost)/{day_count},3) impression_1k_cost,
+round(sum(charge)/sum(photo_click),3) photo_click_cost,
+round(sum(charge)/sum(bclick),3) action_cost,
 sum(`share`) `share`,
 sum(`comment`) `comment`,
 sum(`like`)  `like`,
@@ -60,94 +200,94 @@ sum(download_completed) download_completed,
 sum(activation) activation,
 sum(event_pay_first_day) event_pay_first_day,
 sum(event_pay_purchase_amount_first_day) event_pay_purchase_amount_first_day,
-ifnull(round(sum(event_pay_purchase_amount_first_day)/sum(charge)*100,3),0) event_pay_first_day_roi ,
+round(sum(event_pay_purchase_amount_first_day)/sum(charge)*100,3) event_pay_first_day_roi ,
 sum(event_pay) event_pay,
 sum(event_pay_purchase_amount) event_pay_purchase_amount,
-ifnull(round(sum(event_pay_purchase_amount)/sum(charge)*100,3),0) event_pay_roi,
+round(sum(event_pay_purchase_amount)/sum(charge)*100,3) event_pay_roi,
 sum(event_register) event_register,
-ifnull(round(sum(charge)/sum(event_register),3),0) event_register_cost,
-ifnull(round(sum(event_register)/sum(activation)*100,3),0) event_register_ratio,
+round(sum(charge)/sum(event_register),3) event_register_cost,
+round(sum(event_register)/sum(activation)*100,3) event_register_ratio,
 sum(event_jin_jian_app) event_jin_jian_app,
-ifnull(round(sum(charge)/sum(event_jin_jian_app),3),0) event_jin_jian_app_cost,
+round(sum(charge)/sum(event_jin_jian_app),3) event_jin_jian_app_cost,
 sum(event_credit_grant_app) event_credit_grant_app,
-ifnull(round(sum(charge)/sum(event_credit_grant_app),3),0) event_credit_grant_app_cost ,
-ifnull(round(sum(event_credit_grant_app_ratio)/{day_count}*100,3),0) event_credit_grant_app_ratio,
+round(sum(charge)/sum(event_credit_grant_app),3) event_credit_grant_app_cost ,
+round(sum(event_credit_grant_app_ratio)/{day_count}*100,3) event_credit_grant_app_ratio,
 sum(event_order_paid) event_order_paid,
 sum(event_order_paid_purchase_amount) event_order_paid_purchase_amount,
-ifnull(round(sum(event_order_paid_purchase_amount)/sum(event_order_paid) ,3),0) event_order_paid_cost,
+round(sum(event_order_paid_purchase_amount)/sum(event_order_paid) ,3) event_order_paid_cost,
 sum(event_next_day_stay) event_next_day_stay,
-ifnull(round(sum(charge)/sum(event_next_day_stay),3),0) event_next_day_stay_cost,
-ifnull(round(sum(event_next_day_stay)/sum(activation)*100,3),0) event_next_day_stay_ratio,
+round(sum(charge)/sum(event_next_day_stay),3) event_next_day_stay_cost,
+round(sum(event_next_day_stay)/sum(activation)*100,3) event_next_day_stay_ratio,
 sum(form_count) form_count,
-ifnull(round(sum(charge)/sum(form_count),3),0) form_cost,
-ifnull(round(sum(form_count)/sum(`bclick`)*100,3),0) form_action_ratio,
+round(sum(charge)/sum(form_count),3) form_cost,
+round(sum(form_count)/sum(`bclick`)*100,3) form_action_ratio,
 sum(event_jin_jian_landing_page) event_jin_jian_landing_page,
-ifnull(round(sum(charge)/sum(event_jin_jian_landing_page),3),0) event_jin_jian_landing_page_cost,
+round(sum(charge)/sum(event_jin_jian_landing_page),3) event_jin_jian_landing_page_cost,
 sum(event_credit_grant_landing_page) event_credit_grant_landing_page,
-ifnull(round(sum(charge)/sum(event_credit_grant_landing_page),3),0) event_credit_grant_landing_page_cost,
-ifnull(round(sum(event_credit_grant_landing_page)/{day_count}*100,3),0) event_credit_grant_landing_page_ratio,
+round(sum(charge)/sum(event_credit_grant_landing_page),3) event_credit_grant_landing_page_cost,
+round(sum(event_credit_grant_landing_page)/{day_count}*100,3) event_credit_grant_landing_page_ratio,
 sum(event_valid_clues) event_valid_clues,
-ifnull(round(sum(charge)/sum(event_valid_clues),3),0) event_valid_clues_cost,
+round(sum(charge)/sum(event_valid_clues),3) event_valid_clues_cost,
 sum(event_add_wechat) event_add_wechat,
-ifnull(round(sum(charge)/sum(event_add_wechat),3),0) event_add_wechat_cost,
-ifnull(round(sum(event_add_wechat)/sum(bclick)*100,3),0) event_add_wechat_ratio,
+round(sum(charge)/sum(event_add_wechat),3) event_add_wechat_cost,
+round(sum(event_add_wechat)/sum(bclick)*100,3) event_add_wechat_ratio,
 sum(event_get_through) event_get_through,
-ifnull(round(sum(charge)/sum(event_get_through),3),0) event_get_through_cost,
-ifnull(round(sum(event_get_through)/sum(bclick)*100,3),0) event_get_through_ratio,
+round(sum(charge)/sum(event_get_through),3) event_get_through_cost,
+round(sum(event_get_through)/sum(bclick)*100,3) event_get_through_ratio,
 sum(event_app_invoked) event_app_invoked,
-ifnull(round(sum(charge)/sum(event_app_invoked),3),0) event_app_invoked_cost ,
-ifnull(round(sum(event_app_invoked)/sum(bclick)*100,3),0) event_app_invoked_ratio,
-ifnull(round(sum(event_credit_grant_landing_ratio)/{day_count}*100,3),0) event_credit_grant_landing_ratio,
-ifnull(round(sum(play_5s_ratio)/{day_count}*100,3),0) play_5s_ratio,
-ifnull(round(sum(play_end_ratio)/{day_count}*100,3),0) play_end_ratio,
+round(sum(charge)/sum(event_app_invoked),3) event_app_invoked_cost ,
+round(sum(event_app_invoked)/sum(bclick)*100,3) event_app_invoked_ratio,
+round(sum(event_credit_grant_landing_ratio)/{day_count}*100,3) event_credit_grant_landing_ratio,
+round(sum(play_5s_ratio)/{day_count}*100,3) play_5s_ratio,
+round(sum(play_end_ratio)/{day_count}*100,3) play_end_ratio,
 sum(event_new_user_pay) event_new_user_pay,
-ifnull(round(sum(charge)/sum(event_new_user_pay),3),0) event_new_user_pay_cost,
-ifnull(round(sum(event_new_user_pay)/sum(activation)*100,3),0) event_new_user_pay_ratio,
-ifnull(round(sum(click_1k_cost)/{day_count}*100,3),0) click_1k_cost,
+round(sum(charge)/sum(event_new_user_pay),3) event_new_user_pay_cost,
+round(sum(event_new_user_pay)/sum(activation)*100,3) event_new_user_pay_ratio,
+round(sum(click_1k_cost)/{day_count}*100) click_1k_cost,
 sum(ad_product_cnt) ad_product_cnt,
 sum(event_goods_view) event_goods_view,
 sum(merchant_reco_fans) merchant_reco_fans,
-ifnull(round(sum(event_order_amount_roi)/{day_count}*100,3),0) event_order_amount_roi,
-ifnull(round(sum(charge)/sum(event_goods_view) ,3),0) event_goods_view_cost,
-ifnull(round(sum(charge)/sum(merchant_reco_fans),3),0) merchant_reco_fans_cost,
+round(sum(event_order_amount_roi)/{day_count}*100,3) event_order_amount_roi,
+round(sum(charge)/sum(event_goods_view) ,3) event_goods_view_cost,
+round(sum(charge)/sum(merchant_reco_fans),3) merchant_reco_fans_cost,
 sum(event_button_click) event_button_click,
-ifnull(round(sum(charge)/sum(event_button_click),3),0) event_button_click_cost,
-ifnull(round(sum(event_button_click)/sum(bclick)*100,3),0) event_button_click_ratio ,
-ifnull(round(sum(event_order_paid_roi)/{day_count}*100,3),0) event_order_paid_roi,
+round(sum(charge)/sum(event_button_click),3) event_button_click_cost,
+round(sum(event_button_click)/sum(bclick)*100,3) event_button_click_ratio ,
+round(sum(event_order_paid_roi)/{day_count}*100,3) event_order_paid_roi,
 sum(event_new_user_jinjian_app) event_new_user_jinjian_app,
-ifnull(round(sum(charge)/sum(event_new_user_jinjian_app),3),0) event_new_user_jinjian_app_cost,
-ifnull(round(sum(event_new_user_jinjian_app)/sum(form_count)*100,3),0) event_new_user_jinjian_app_roi ,
+round(sum(charge)/sum(event_new_user_jinjian_app),3) event_new_user_jinjian_app_cost,
+round(sum(event_new_user_jinjian_app)/sum(form_count)*100,3) event_new_user_jinjian_app_roi ,
 sum(event_new_user_credit_grant_app) event_new_user_credit_grant_app,
-ifnull(round(sum(charge)/sum(event_new_user_credit_grant_app),3),0)  event_new_user_credit_grant_app_cost,
-ifnull(round(sum(event_new_user_credit_grant_app)/sum(form_count)*100,3),0) event_new_user_credit_grant_app_roi,
+round(sum(charge)/sum(event_new_user_credit_grant_app),3)  event_new_user_credit_grant_app_cost,
+round(sum(event_new_user_credit_grant_app)/sum(form_count)*100,3) event_new_user_credit_grant_app_roi,
 sum(event_new_user_jinjian_page) event_new_user_jinjian_page,
-ifnull(round(sum(charge)/sum(event_new_user_jinjian_page),3),0) event_new_user_jinjian_page_cost,
-ifnull(round(sum(event_new_user_jinjian_page)/sum(form_count)*100,3),0) event_new_user_jinjian_page_roi ,
+round(sum(charge)/sum(event_new_user_jinjian_page),3) event_new_user_jinjian_page_cost,
+round(sum(event_new_user_jinjian_page)/sum(form_count)*100,3) event_new_user_jinjian_page_roi ,
 sum(event_new_user_credit_grant_page) event_new_user_credit_grant_page,
-ifnull(round(sum(charge)/sum(event_new_user_credit_grant_page) ,3),0) event_new_user_credit_grant_page_cost,
-ifnull(round(sum(event_new_user_credit_grant_page)/sum(form_count)*100,3),0) event_new_user_credit_grant_page_roi,
+round(sum(charge)/sum(event_new_user_credit_grant_page) ,3) event_new_user_credit_grant_page_cost,
+round(sum(event_new_user_credit_grant_page)/sum(form_count)*100,3) event_new_user_credit_grant_page_roi,
 sum(event_appoint_form)  event_appoint_form,
-ifnull(round(sum(charge)/sum(event_appoint_form),3),0) event_appoint_form_cost,
-ifnull(round(sum(event_appoint_form)/sum(form_count)*100,3),0) event_appoint_form_ratio,
+round(sum(charge)/sum(event_appoint_form),3) event_appoint_form_cost,
+round(sum(event_appoint_form)/sum(form_count)*100,3) event_appoint_form_ratio,
 sum(event_appoint_jump_click) event_appoint_jump_click,
-ifnull(round(sum(charge)/sum(event_appoint_jump_click),3),0)  event_appoint_jump_click_cost,
-ifnull(round(sum(event_appoint_jump_click_cost)/sum(form_count)*100,3),0) event_appoint_jump_click_ratio,
+round(sum(charge)/sum(event_appoint_jump_click),3)  event_appoint_jump_click_cost,
+round(sum(event_appoint_jump_click_cost)/sum(form_count)*100,3) event_appoint_jump_click_ratio,
 sum(union_event_pay_purchase_amount_7d) union_event_pay_purchase_amount_7d,
-ifnull(round(sum(union_event_pay_purchase_amount_7d_roi)/{day_count}*100,3),0) union_event_pay_purchase_amount_7d_roi,
+round(sum(union_event_pay_purchase_amount_7d_roi)/{day_count}*100,3) union_event_pay_purchase_amount_7d_roi,
 sum(event_order_successed) event_order_successed,
-ifnull(round(sum(ad_photo_played_10s_ratio)/{day_count}*100,3),0) ad_photo_played_10s_ratio,
-ifnull(round(sum(charge)/sum(key_action),3),0) key_action_cost,
-ifnull(round(sum(charge)/sum(event_add_shopping_cart) ,3),0) event_add_shopping_cart_cost ,
-ifnull(round(sum(event_ad_watch_times)/sum(bclick)*100,3),0) event_ad_watch_times_ratio,
-ifnull(round(sum(charge)/sum(event_outbound_call),3),0) event_outbound_call_cost,
+round(sum(ad_photo_played_10s_ratio)/{day_count}*100,3) ad_photo_played_10s_ratio,
+round(sum(charge)/sum(key_action),3) key_action_cost,
+round(sum(charge)/sum(event_add_shopping_cart) ,3) event_add_shopping_cart_cost ,
+round(sum(event_ad_watch_times)/sum(bclick)*100,3) event_ad_watch_times_ratio,
+round(sum(charge)/sum(event_outbound_call),3) event_outbound_call_cost,
 sum(event_outbound_call) event_outbound_call,
-ifnull(round(sum(action_new_ratio)/{day_count}*100,3),0) action_new_ratio,
+round(sum(action_new_ratio)/{day_count}*100,3) action_new_ratio,
 sum(event_watch_app_ad) event_watch_app_ad,
-ifnull(round(sum(charge)/sum(event_multi_conversion) ,3),0) event_multi_conversion_cost,
+round(sum(charge)/sum(event_multi_conversion) ,3) event_multi_conversion_cost,
 sum(event_phone_card_activate) event_phone_card_activate,
-ifnull(round(sum(ad_photo_played_75percent_ratio)/{day_count}*100,3),0) ad_photo_played_75percent_ratio,
-ifnull(round(sum(key_action)/sum(activation)*100,3),0) key_action_ratio ,
-ifnull(round(sum(charge)/sum(event_ad_watch_times),3),0) event_ad_watch_times_cost,
+round(sum(ad_photo_played_75percent_ratio)/{day_count}*100,3) ad_photo_played_75percent_ratio,
+round(sum(key_action)/sum(activation)*100,3) key_action_ratio ,
+round(sum(charge)/sum(event_ad_watch_times),3) event_ad_watch_times_cost,
 sum(event_add_shopping_cart) event_add_shopping_cart,
 sum(key_action) key_action,
 sum(event_multi_conversion) event_multi_conversion,
@@ -155,34 +295,30 @@ sum(event_wechat_connected) event_wechat_connected,
 sum(event_dsp_gift_form) event_dsp_gift_form,
 sum(event_intention_confirmed) event_intention_confirmed,
 sum(event_phone_get_through) event_phone_get_through,
-ifnull(round(sum(event_multi_conversion)/sum(activation)*100,3),0)  event_multi_conversion_ratio,
+round(sum(event_multi_conversion)/sum(activation)*100,3)  event_multi_conversion_ratio,
 sum(event_measurement_house) event_measurement_house,
-ifnull(round(sum(ad_photo_played_2s_ratio)/{day_count}*100,3),0) ad_photo_played_2s_ratio,
-ifnull(round(sum(event_outbound_call_ratio)/sum(activation)*100,3),0) event_outbound_call_ratio,
+round(sum(ad_photo_played_2s_ratio)/{day_count}*100,3) ad_photo_played_2s_ratio,
+round(sum(event_outbound_call_ratio)/sum(activation)*100,3) event_outbound_call_ratio,
 sum(event_ad_watch_times) event_ad_watch_times
 from
 (SELECT
 video.signature,
 uc.company_id,
-CASE
-WHEN uc.company_id = 'd57fecdcf7a94d009736d9c850731582' THEN
-'北京汇创思拓数字科技有限公司'
-WHEN uc.company_id = '6608ed13c0dd42de93c790dbdf124234' THEN
-'广州汇创思拓数字科技有限公司'
-WHEN uc.company_id = '4b10089775c040119e139087517aed88' THEN
-'上海汇创思拓数字科技有限公司'
+CASE WHEN uc.company_id = 'd57fecdcf7a94d009736d9c850731582' THEN '北京汇创思拓数字科技有限公司'
+     WHEN uc.company_id = '6608ed13c0dd42de93c790dbdf124234' THEN '广州汇创思拓数字科技有限公司'
+     WHEN uc.company_id = '4b10089775c040119e139087517aed88' THEN '上海汇创思拓数字科技有限公司'
 ELSE
 uc.company_name
 END company_name,
 cma.leader_id,
-    us.realname leader_name,
+us.realname leader_name,
 ur.org_type,
 ur.org_code,
 ur.id,
 ur.depart_name,
 case when cmi.material_innovate=2 then video.signature else '' end  innovate_code,
 report.charge ,
-    report.`show`,
+report.`show`,
     report.photo_click,
     report.aclick ,
     report.bclick ,
@@ -308,13 +444,11 @@ report.charge ,
     report.ad_photo_played_2s_ratio ,
     report.event_outbound_call_ratio,
     report.event_ad_watch_times
-FROM
-(select * from media_api.kuaishou_material_video_report_daily 
-where stat_date >= date_format(date_sub({date_time}, INTERVAL {day_count} -1  DAY),'%Y%m%d')
-) report
-LEFT JOIN 
-(select account_id,signature,photo_id from media_api.kuaishou_video_list  group by account_id,signature,photo_id) video 
- ON report.advertiser_id = video.account_id AND report.photo_id = video.photo_id 
+    FROM
+     (select * from media_api.kuaishou_material_video_report_daily where stat_date >= date_format(date_sub({date_time}, INTERVAL {day_count}-1 DAY),'%Y%m%d')) report
+     LEFT JOIN
+(select account_id,signature,photo_id from media_api.kuaishou_video_list  group by account_id,signature,photo_id) video
+ON report.advertiser_id = video.account_id AND report.photo_id = video.photo_id
 LEFT JOIN (
 SELECT
 CODE,
@@ -336,25 +470,33 @@ FROM
 SELECT
 material_id,
 leader_id,
-row_number () over (
-PARTITION BY material_id
-ORDER BY
-create_time
-) rn
-FROM
-`jeecg-boot`.ctop_material_ascription
-) m
-WHERE
-rn = 1
-) cma ON video.signature = cma.material_id
- left join `jeecg-boot`.sys_user us on us.id=cma.leader_id
+row_number () over (PARTITION BY material_id ORDER BY create_time) rn FROM `jeecg-boot`.ctop_material_ascription) m
+WHERE rn = 1) cma ON video.signature = cma.material_id
+left join `jeecg-boot`.sys_user us on us.id=cma.leader_id
 LEFT JOIN `jeecg-boot`.user_company uc ON cma.leader_id = uc.user_id
 LEFT JOIN `jeecg-boot`.sys_user_depart de ON cma.leader_id = de.user_id
 LEFT JOIN `jeecg-boot`.sys_depart ur ON de.dep_id = ur.id) def
 group by
-  company_id,
-  leader_id
-;
+company_id,
+company_name,
+org_code,
+org_type,
+id,
+depart_name,
+leader_id,
+leader_name) new
+left join
+(select  ifnull(cma.leader_id,'-') leader_id,sum(report.charge) charge from
+(select advertiser_id account_id,photo_id,sum(charge) charge from media_api.kuaishou_material_video_report_daily
+where stat_date between date_format(date_sub(date_format(date_sub({date_time}, INTERVAL {day_count} DAY),'%Y%m%d'), INTERVAL {day_count} -1 DAY),'%Y%m%d')
+and date_format(date_sub({date_time}, INTERVAL {day_count} DAY),'%Y%m%d') group by advertiser_id,photo_id) report
+LEFT JOIN
+(select account_id,signature,photo_id from media_api.kuaishou_video_list  group by account_id,signature,photo_id) video
+ON report.account_id = video.account_id AND report.photo_id=video.photo_id
+LEFT JOIN (SELECT material_id,leader_id FROM (SELECT material_id,leader_id,row_number () over (PARTITION BY material_id ORDER BY create_time ) rn
+FROM  `jeecg-boot`.ctop_material_ascription) m WHERE rn = 1 ) cma ON video.signature = cma.material_id
+group by cma.leader_id) ratio
+on new.leader_id=ratio.leader_id;
             """.format(date_time=self.date_time, day_count=self.day_count)
             print(sql)
             TidbConn.upsert(conn=self.conn, sql_buff=sql)

+ 296 - 143
Apietl/kwai_etl/Q4Task/AppKuaishouCompanyUserReportBusi.py

@@ -23,142 +23,281 @@ class AppKuaishouCompanyUserReportBusi:
             sql = """
 SET sql_mode = 'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
 REPLACE INTO application.app_kuaishou_company_user_report_busi_{day_count}d
-SELECT
-concat(date_format(date_sub({date_time}, INTERVAL ({day_count} -1 )  DAY),'%Y%m%d'),'-',{date_time}) start_end_time,
-ifnull(company_id,'-') company_id,
+select
+new.start_end_time,
+new.company_id,
+new.company_name,
+new.org_code,
+new.org_type,
+new.dep_id,
+new.dep_name,
+new.leader_id,
+new.leader_name,
+new.account_num,
+new.charge,
+ifnull(ratio.charge,0) last_charge,
+ifnull(round((new.charge-ratio.charge)/ratio.charge*100,3),0) charge_ratio,
+new.show,
+new.photo_click,
+new.aclick,
+new.bclick,
+new.photo_click_ratio,
+new.play_3s_ratio,
+new.action_ratio,
+new.impression_1k_cost,
+new.photo_click_cost,
+new.action_cost,
+new.share,
+new.comment,
+new.like,
+new.follow,
+new.cancel_follow,
+new.report,
+new.block,
+new.negative,
+new.submit,
+new.download_started,
+new.download_completed,
+new.activation,
+new.event_pay_first_day,
+new.event_pay_purchase_amount_first_day,
+new.event_pay_first_day_roi,
+new.event_pay,
+new.event_pay_purchase_amount,
+new.event_pay_roi,
+new.event_register,
+new.event_register_cost,
+new.event_register_ratio,
+new.event_jin_jian_app,
+new.event_jin_jian_app_cost,
+new.event_credit_grant_app,
+new.event_credit_grant_app_cost,
+new.event_credit_grant_app_ratio,
+new.event_order_paid,
+new.event_order_paid_purchase_amount,
+new.event_order_paid_cost,
+new.event_next_day_stay,
+new.event_next_day_stay_cost,
+new.event_next_day_stay_ratio,
+new.form_count,
+new.form_cost,
+new.form_action_ratio,
+new.event_jin_jian_landing_page,
+new.event_jin_jian_landing_page_cost,
+new.event_credit_grant_landing_page,
+new.event_credit_grant_landing_page_cost,
+new.event_credit_grant_landing_page_ratio,
+new.event_valid_clues,
+new.event_valid_clues_cost,
+new.event_add_wechat,
+new.event_add_wechat_cost,
+new.event_add_wechat_ratio,
+new.event_get_through,
+new.event_get_through_cost,
+new.event_get_through_ratio,
+new.event_app_invoked,
+new.event_app_invoked_cost,
+new.event_app_invoked_ratio,
+new.event_credit_grant_landing_ratio,
+new.play_5s_ratio,
+new.play_end_ratio,
+new.event_new_user_pay,
+new.event_new_user_pay_cost,
+new.event_new_user_pay_ratio,
+new.click_1k_cost,
+new.ad_product_cnt,
+new.event_goods_view,
+new.merchant_reco_fans,
+new.event_order_amount_roi,
+new.event_goods_view_cost,
+new.merchant_reco_fans_cost,
+new.event_button_click,
+new.event_button_click_cost,
+new.event_button_click_ratio,
+new.event_order_paid_roi,
+new.event_new_user_jinjian_app,
+new.event_new_user_jinjian_app_cost,
+new.event_new_user_jinjian_app_roi,
+new.event_new_user_credit_grant_app,
+new.event_new_user_credit_grant_app_cost,
+new.event_new_user_credit_grant_app_roi,
+new.event_new_user_jinjian_page,
+new.event_new_user_jinjian_page_cost,
+new.event_new_user_jinjian_page_roi,
+new.event_new_user_credit_grant_page,
+new.event_new_user_credit_grant_page_cost,
+new.event_new_user_credit_grant_page_roi,
+new.event_appoint_form,
+new.event_appoint_form_cost,
+new.event_appoint_form_ratio,
+new.event_appoint_jump_click,
+new.event_appoint_jump_click_cost,
+new.event_appoint_jump_click_ratio,
+new.union_event_pay_purchase_amount_7d,
+new.union_event_pay_purchase_amount_7d_roi,
+new.event_order_successed,
+new.ad_photo_played_10s_ratio,
+new.key_action_cost,
+new.event_add_shopping_cart_cost,
+new.event_ad_watch_times_ratio,
+new.event_outbound_call_cost,
+new.event_outbound_call,
+new.action_new_ratio,
+new.event_watch_app_ad,
+new.event_multi_conversion_cost,
+new.event_phone_card_activate,
+new.ad_photo_played_75percent_ratio,
+new.key_action_ratio,
+new.event_ad_watch_times_cost,
+new.event_add_shopping_cart,
+new.key_action,
+new.event_multi_conversion,
+new.event_wechat_connected,
+new.event_dsp_gift_form,
+new.event_intention_confirmed,
+new.event_phone_get_through,
+new.event_multi_conversion_ratio,
+new.event_measurement_house,
+new.ad_photo_played_2s_ratio,
+new.event_outbound_call_ratio,
+new.event_ad_watch_times
+from
+(SELECT
+concat(date_format(date_sub({date_time}, INTERVAL {day_count}-1 DAY),'%Y%m%d'),'-',{date_time}) start_end_time,
+company_id,
 company_name,
 org_code,
 org_type,
 dep_id,
-depart_name,
-ifnull(leader_id,'-') leader_id,
- leader_name,
+dep_name,
+leader_id,
+trim(leader_name) leader_name,
 count(DISTINCT account_id) account_num,
 sum(charge) charge,
 sum(`show`) `show`,
-sum(photo_click)photo_click,
-sum(aclick)  aclick,
-sum(bclick) bclick,
-ifnull(round(sum(photo_click)/sum(`show`),3),0) photo_click_ratio,
-ifnull(round(sum(play_3s_ratio)/{day_count}*100,3),0)  play_3s_ratio,
-ifnull(round(sum(bclick)/sum(aclick)*100,3),0) action_ratio,
-ifnull(round(sum(impression_1k_cost)/{day_count},3),0) impression_1k_cost,
-ifnull(round(sum(charge)/sum(photo_click),3),0) photo_click_cost,
-ifnull(round(sum(charge)/sum(bclick),3),0) action_cost,
-sum(`share`) `share`,
-sum(`comment`) `comment`,
-sum(`like`)  `like`,
-sum(follow) follow,
-sum(cancel_follow)  cancel_follow,
-sum(report) report,
-sum(block) block,
-sum(negative) negative,
-sum(submit) submit,
-sum(download_started) download_started,
-sum(download_completed) download_completed,
-sum(activation) activation,
-sum(event_pay_first_day) event_pay_first_day,
-sum(event_pay_purchase_amount_first_day) event_pay_purchase_amount_first_day,
-ifnull(round(sum(event_pay_purchase_amount_first_day)/sum(charge)*100,3),0) event_pay_first_day_roi ,
-sum(event_pay) event_pay,
-sum(event_pay_purchase_amount) event_pay_purchase_amount,
-ifnull(round(sum(event_pay_purchase_amount)/sum(charge)*100,3),0) event_pay_roi,
-sum(event_register) event_register,
-ifnull(round(sum(charge)/sum(event_register),3),0) event_register_cost,
-ifnull(round(sum(event_register)/sum(activation)*100,3),0) event_register_ratio,
-sum(event_jin_jian_app) event_jin_jian_app,
-ifnull(round(sum(charge)/sum(event_jin_jian_app),3),0) event_jin_jian_app_cost,
-sum(event_credit_grant_app) event_credit_grant_app,
-ifnull(round(sum(charge)/sum(event_credit_grant_app),3),0) event_credit_grant_app_cost ,
-ifnull(round(sum(event_credit_grant_app_ratio)/{day_count}*100,3),0) event_credit_grant_app_ratio,
-sum(event_order_paid) event_order_paid,
-sum(event_order_paid_purchase_amount) event_order_paid_purchase_amount,
-ifnull(round(sum(event_order_paid_purchase_amount)/sum(event_order_paid) ,3),0) event_order_paid_cost,
-sum(event_next_day_stay) event_next_day_stay,
-ifnull(round(sum(charge)/sum(event_next_day_stay),3),0) event_next_day_stay_cost,
-ifnull(round(sum(event_next_day_stay)/sum(activation)*100,3),0) event_next_day_stay_ratio,
-sum(form_count) form_count,
-ifnull(round(sum(charge)/sum(form_count),3),0) form_cost,
-ifnull(round(sum(form_count)/sum(`bclick`)*100,3),0) form_action_ratio,
-sum(event_jin_jian_landing_page) event_jin_jian_landing_page,
-ifnull(round(sum(charge)/sum(event_jin_jian_landing_page),3),0) event_jin_jian_landing_page_cost,
-sum(event_credit_grant_landing_page) event_credit_grant_landing_page,
-ifnull(round(sum(charge)/sum(event_credit_grant_landing_page),3),0) event_credit_grant_landing_page_cost,
-ifnull(round(sum(event_credit_grant_landing_page)/{day_count}*100,3),0) event_credit_grant_landing_page_ratio,
-sum(event_valid_clues) event_valid_clues,
-ifnull(round(sum(charge)/sum(event_valid_clues),3),0) event_valid_clues_cost,
-sum(event_add_wechat) event_add_wechat,
-ifnull(round(sum(charge)/sum(event_add_wechat),3),0) event_add_wechat_cost,
-ifnull(round(sum(event_add_wechat)/sum(bclick)*100,3),0) event_add_wechat_ratio,
-sum(event_get_through) event_get_through,
-ifnull(round(sum(charge)/sum(event_get_through),3),0) event_get_through_cost,
-ifnull(round(sum(event_get_through)/sum(bclick)*100,3),0) event_get_through_ratio,
-sum(event_app_invoked) event_app_invoked,
-ifnull(round(sum(charge)/sum(event_app_invoked),3),0) event_app_invoked_cost ,
-ifnull(round(sum(event_app_invoked)/sum(bclick)*100,3),0) event_app_invoked_ratio,
-ifnull(round(sum(event_credit_grant_landing_ratio)/{day_count}*100,3),0) event_credit_grant_landing_ratio,
-ifnull(round(sum(play_5s_ratio)/{day_count}*100,3),0) play_5s_ratio,
-ifnull(round(sum(play_end_ratio)/{day_count}*100,3),0) play_end_ratio,
-sum(event_new_user_pay) event_new_user_pay,
-ifnull(round(sum(charge)/sum(event_new_user_pay),3),0) event_new_user_pay_cost,
-ifnull(round(sum(event_new_user_pay)/sum(activation)*100,3),0) event_new_user_pay_ratio,
-ifnull(round(sum(click_1k_cost)/{day_count}*100,3),0) click_1k_cost,
-sum(ad_product_cnt) ad_product_cnt,
-sum(event_goods_view) event_goods_view,
-sum(merchant_reco_fans) merchant_reco_fans,
-ifnull(round(sum(event_order_amount_roi)/{day_count}*100,3),0) event_order_amount_roi,
-ifnull(round(sum(charge)/sum(event_goods_view) ,3),0) event_goods_view_cost,
-ifnull(round(sum(charge)/sum(merchant_reco_fans),3),0) merchant_reco_fans_cost,
-sum(event_button_click) event_button_click,
-ifnull(round(sum(charge)/sum(event_button_click),3),0) event_button_click_cost,
-ifnull(round(sum(event_button_click)/sum(bclick)*100,3),0) event_button_click_ratio ,
-ifnull(round(sum(event_order_paid_roi)/{day_count}*100,3),0) event_order_paid_roi,
-sum(event_new_user_jinjian_app) event_new_user_jinjian_app,
-ifnull(round(sum(charge)/sum(event_new_user_jinjian_app),3),0) event_new_user_jinjian_app_cost,
-ifnull(round(sum(event_new_user_jinjian_app)/sum(form_count)*100,3),0) event_new_user_jinjian_app_roi ,
-sum(event_new_user_credit_grant_app) event_new_user_credit_grant_app,
-ifnull(round(sum(charge)/sum(event_new_user_credit_grant_app),3),0)  event_new_user_credit_grant_app_cost,
-ifnull(round(sum(event_new_user_credit_grant_app)/sum(form_count)*100,3),0) event_new_user_credit_grant_app_roi,
-sum(event_new_user_jinjian_page) event_new_user_jinjian_page,
-ifnull(round(sum(charge)/sum(event_new_user_jinjian_page),3),0) event_new_user_jinjian_page_cost,
-ifnull(round(sum(event_new_user_jinjian_page)/sum(form_count)*100,3),0) event_new_user_jinjian_page_roi ,
-sum(event_new_user_credit_grant_page) event_new_user_credit_grant_page,
-ifnull(round(sum(charge)/sum(event_new_user_credit_grant_page) ,3),0) event_new_user_credit_grant_page_cost,
-ifnull(round(sum(event_new_user_credit_grant_page)/sum(form_count)*100,3),0) event_new_user_credit_grant_page_roi,
-sum(event_appoint_form)  event_appoint_form,
-ifnull(round(sum(charge)/sum(event_appoint_form),3),0) event_appoint_form_cost,
-ifnull(round(sum(event_appoint_form)/sum(form_count)*100,3),0) event_appoint_form_ratio,
-sum(event_appoint_jump_click) event_appoint_jump_click,
-ifnull(round(sum(charge)/sum(event_appoint_jump_click),3),0)  event_appoint_jump_click_cost,
-ifnull(round(sum(event_appoint_jump_click_cost)/sum(form_count)*100,3),0) event_appoint_jump_click_ratio,
-sum(union_event_pay_purchase_amount_7d) union_event_pay_purchase_amount_7d,
-ifnull(round(sum(union_event_pay_purchase_amount_7d_roi)/{day_count}*100,3),0) union_event_pay_purchase_amount_7d_roi,
-sum(event_order_successed) event_order_successed,
-ifnull(round(sum(ad_photo_played_10s_ratio)/{day_count}*100,3),0) ad_photo_played_10s_ratio,
-ifnull(round(sum(charge)/sum(key_action),3),0) key_action_cost,
-ifnull(round(sum(charge)/sum(event_add_shopping_cart) ,3),0) event_add_shopping_cart_cost ,
-ifnull(round(sum(event_ad_watch_times)/sum(bclick)*100,3),0) event_ad_watch_times_ratio,
-ifnull(round(sum(charge)/sum(event_outbound_call),3),0) event_outbound_call_cost,
-sum(event_outbound_call) event_outbound_call,
-ifnull(round(sum(action_new_ratio)/{day_count}*100,3),0) action_new_ratio,
-sum(event_watch_app_ad) event_watch_app_ad,
-ifnull(round(sum(charge)/sum(event_multi_conversion) ,3),0) event_multi_conversion_cost,
-sum(event_phone_card_activate) event_phone_card_activate,
-ifnull(round(sum(ad_photo_played_75percent_ratio)/{day_count}*100,3),0) ad_photo_played_75percent_ratio,
-ifnull(round(sum(key_action)/sum(activation)*100,3),0) key_action_ratio ,
-ifnull(round(sum(charge)/sum(event_ad_watch_times),3),0) event_ad_watch_times_cost,
-sum(event_add_shopping_cart) event_add_shopping_cart,
-sum(key_action) key_action,
-sum(event_multi_conversion) event_multi_conversion,
-sum(event_wechat_connected) event_wechat_connected,
-sum(event_dsp_gift_form) event_dsp_gift_form,
-sum(event_intention_confirmed) event_intention_confirmed,
-sum(event_phone_get_through) event_phone_get_through,
-ifnull(round(sum(event_multi_conversion)/sum(activation)*100,3),0)  event_multi_conversion_ratio,
-sum(event_measurement_house) event_measurement_house,
-ifnull(round(sum(ad_photo_played_2s_ratio)/{day_count}*100,3),0) ad_photo_played_2s_ratio,
-ifnull(round(sum(event_outbound_call_ratio)/sum(activation)*100,3),0) event_outbound_call_ratio,
-sum(event_ad_watch_times) event_ad_watch_times  
+  sum(photo_click)photo_click,
+  sum(aclick)  aclick,
+  sum(bclick) bclick,
+  round(sum(photo_click)/sum(`show`),3) photo_click_ratio,
+  round(sum(play_3s_ratio)/{day_count}*100,3)  play_3s_ratio,
+  round(sum(bclick)/sum(aclick)*100,3) action_ratio,
+  round(sum(impression_1k_cost)/{day_count},3) impression_1k_cost,
+  round(sum(charge)/sum(photo_click),3) photo_click_cost,
+  round(sum(charge)/sum(bclick),3) action_cost,
+  sum(`share`) `share`,
+  sum(`comment`) `comment`,
+  sum(`like`)  `like`,
+  sum(follow) follow,
+  sum(cancel_follow)  cancel_follow,
+  sum(report) report,
+  sum(block) block,
+  sum(negative) negative,
+  sum(submit) submit,
+  sum(download_started) download_started,
+  sum(download_completed) download_completed,
+  sum(activation) activation,
+  sum(event_pay_first_day) event_pay_first_day,
+  sum(event_pay_purchase_amount_first_day) event_pay_purchase_amount_first_day,
+  round(sum(event_pay_purchase_amount_first_day)/sum(charge)*100,3) event_pay_first_day_roi ,
+  sum(event_pay) event_pay,
+  sum(event_pay_purchase_amount) event_pay_purchase_amount,
+  round(sum(event_pay_purchase_amount)/sum(charge)*100,3) event_pay_roi,
+  sum(event_register) event_register,
+  round(sum(charge)/sum(event_register),3) event_register_cost,
+  round(sum(event_register)/sum(activation)*100,3) event_register_ratio,
+  sum(event_jin_jian_app) event_jin_jian_app,
+  round(sum(charge)/sum(event_jin_jian_app),3) event_jin_jian_app_cost,
+  sum(event_credit_grant_app) event_credit_grant_app,
+  round(sum(charge)/sum(event_credit_grant_app),3) event_credit_grant_app_cost ,
+  round(sum(event_credit_grant_app_ratio)/{day_count}*100,3) event_credit_grant_app_ratio,
+  sum(event_order_paid) event_order_paid,
+  sum(event_order_paid_purchase_amount) event_order_paid_purchase_amount,
+  round(sum(event_order_paid_purchase_amount)/sum(event_order_paid) ,3) event_order_paid_cost,
+  sum(event_next_day_stay) event_next_day_stay,
+  round(sum(charge)/sum(event_next_day_stay),3) event_next_day_stay_cost,
+  round(sum(event_next_day_stay)/sum(activation)*100,3) event_next_day_stay_ratio,
+  sum(form_count) form_count,
+  round(sum(charge)/sum(form_count),3) form_cost,
+  round(sum(form_count)/sum(`bclick`)*100,3) form_action_ratio,
+  sum(event_jin_jian_landing_page) event_jin_jian_landing_page,
+  round(sum(charge)/sum(event_jin_jian_landing_page),3) event_jin_jian_landing_page_cost,
+  sum(event_credit_grant_landing_page) event_credit_grant_landing_page,
+  round(sum(charge)/sum(event_credit_grant_landing_page),3) event_credit_grant_landing_page_cost,
+  round(sum(event_credit_grant_landing_page)/{day_count}*100,3) event_credit_grant_landing_page_ratio,
+  sum(event_valid_clues) event_valid_clues,
+  round(sum(charge)/sum(event_valid_clues),3) event_valid_clues_cost,
+  sum(event_add_wechat) event_add_wechat,
+  round(sum(charge)/sum(event_add_wechat),3) event_add_wechat_cost,
+  round(sum(event_add_wechat)/sum(bclick)*100,3) event_add_wechat_ratio,
+  sum(event_get_through) event_get_through,
+  round(sum(charge)/sum(event_get_through),3) event_get_through_cost,
+  round(sum(event_get_through)/sum(bclick)*100,3) event_get_through_ratio,
+  sum(event_app_invoked) event_app_invoked,
+  round(sum(charge)/sum(event_app_invoked),3) event_app_invoked_cost ,
+  round(sum(event_app_invoked)/sum(bclick)*100,3) event_app_invoked_ratio,
+  round(sum(event_credit_grant_landing_ratio)/{day_count}*100,3) event_credit_grant_landing_ratio,
+  round(sum(play_5s_ratio)/{day_count}*100,3) play_5s_ratio,
+  round(sum(play_end_ratio)/{day_count}*100,3) play_end_ratio,
+  sum(event_new_user_pay) event_new_user_pay,
+  round(sum(charge)/sum(event_new_user_pay),3) event_new_user_pay_cost,
+  round(sum(event_new_user_pay)/sum(activation)*100,3) event_new_user_pay_ratio,
+  round(sum(click_1k_cost)/{day_count}*100) click_1k_cost,
+  sum(ad_product_cnt) ad_product_cnt,
+  sum(event_goods_view) event_goods_view,
+  sum(merchant_reco_fans) merchant_reco_fans,
+  round(sum(event_order_amount_roi)/{day_count}*100,3) event_order_amount_roi,
+  round(sum(charge)/sum(event_goods_view) ,3) event_goods_view_cost,
+  round(sum(charge)/sum(merchant_reco_fans),3) merchant_reco_fans_cost,
+  sum(event_button_click) event_button_click,
+  round(sum(charge)/sum(event_button_click),3) event_button_click_cost,
+  round(sum(event_button_click)/sum(bclick)*100,3) event_button_click_ratio ,
+  round(sum(event_order_paid_roi)/{day_count}*100,3) event_order_paid_roi,
+  sum(event_new_user_jinjian_app) event_new_user_jinjian_app,
+  round(sum(charge)/sum(event_new_user_jinjian_app),3) event_new_user_jinjian_app_cost,
+  round(sum(event_new_user_jinjian_app)/sum(form_count)*100,3) event_new_user_jinjian_app_roi ,
+  sum(event_new_user_credit_grant_app) event_new_user_credit_grant_app,
+  round(sum(charge)/sum(event_new_user_credit_grant_app),3)  event_new_user_credit_grant_app_cost,
+  round(sum(event_new_user_credit_grant_app)/sum(form_count)*100,3) event_new_user_credit_grant_app_roi,
+  sum(event_new_user_jinjian_page) event_new_user_jinjian_page,
+  round(sum(charge)/sum(event_new_user_jinjian_page),3) event_new_user_jinjian_page_cost,
+  round(sum(event_new_user_jinjian_page)/sum(form_count)*100,3) event_new_user_jinjian_page_roi ,
+  sum(event_new_user_credit_grant_page) event_new_user_credit_grant_page,
+  round(sum(charge)/sum(event_new_user_credit_grant_page) ,3) event_new_user_credit_grant_page_cost,
+  round(sum(event_new_user_credit_grant_page)/sum(form_count)*100,3) event_new_user_credit_grant_page_roi,
+  sum(event_appoint_form)  event_appoint_form,
+  round(sum(charge)/sum(event_appoint_form),3) event_appoint_form_cost,
+  round(sum(event_appoint_form)/sum(form_count)*100,3) event_appoint_form_ratio,
+  sum(event_appoint_jump_click) event_appoint_jump_click,
+  round(sum(charge)/sum(event_appoint_jump_click),3)  event_appoint_jump_click_cost,
+  round(sum(event_appoint_jump_click_cost)/sum(form_count)*100,3) event_appoint_jump_click_ratio,
+  sum(union_event_pay_purchase_amount_7d) union_event_pay_purchase_amount_7d,
+  round(sum(union_event_pay_purchase_amount_7d_roi)/{day_count}*100,3) union_event_pay_purchase_amount_7d_roi,
+  sum(event_order_successed) event_order_successed,
+  round(sum(ad_photo_played_10s_ratio)/{day_count}*100,3) ad_photo_played_10s_ratio,
+  round(sum(charge)/sum(key_action),3) key_action_cost,
+  round(sum(charge)/sum(event_add_shopping_cart) ,3) event_add_shopping_cart_cost ,
+  round(sum(event_ad_watch_times)/sum(bclick)*100,3) event_ad_watch_times_ratio,
+  round(sum(charge)/sum(event_outbound_call),3) event_outbound_call_cost,
+  sum(event_outbound_call) event_outbound_call,
+  round(sum(action_new_ratio)/{day_count}*100,3) action_new_ratio,
+  sum(event_watch_app_ad) event_watch_app_ad,
+  round(sum(charge)/sum(event_multi_conversion) ,3) event_multi_conversion_cost,
+  sum(event_phone_card_activate) event_phone_card_activate,
+  round(sum(ad_photo_played_75percent_ratio)/{day_count}*100,3) ad_photo_played_75percent_ratio,
+  round(sum(key_action)/sum(activation)*100,3) key_action_ratio ,
+  round(sum(charge)/sum(event_ad_watch_times),3) event_ad_watch_times_cost,
+  sum(event_add_shopping_cart) event_add_shopping_cart,
+  sum(key_action) key_action,
+  sum(event_multi_conversion) event_multi_conversion,
+  sum(event_wechat_connected) event_wechat_connected,
+  sum(event_dsp_gift_form) event_dsp_gift_form,
+  sum(event_intention_confirmed) event_intention_confirmed,
+  sum(event_phone_get_through) event_phone_get_through,
+  round(sum(event_multi_conversion)/sum(activation)*100,3)  event_multi_conversion_ratio,
+  sum(event_measurement_house) event_measurement_house,
+  round(sum(ad_photo_played_2s_ratio)/{day_count}*100,3) ad_photo_played_2s_ratio,
+  round(sum(event_outbound_call_ratio)/sum(activation)*100,3) event_outbound_call_ratio,
+  sum(event_ad_watch_times) event_ad_watch_times
 from (
 SELECT
 report.advertiser_id account_id,
@@ -176,13 +315,13 @@ END company_name,
 dep.org_code,
 dep.org_type,
 dep.dep_id,
-dep.depart_name,
+dep.dep_name,
 dep.user_id,
 dep.realname,
 dep.leader_id,
 dep.leader_name,
 report.charge ,
-    report.`show`,
+report.`show`,
     report.photo_click,
     report.aclick ,
     report.bclick ,
@@ -308,11 +447,10 @@ report.charge ,
     report.ad_photo_played_2s_ratio ,
     report.event_outbound_call_ratio,
     report.event_ad_watch_times
-   from  
+   from
    (select * from  media_api.kuaishou_account_report_daily  
-  where stat_date >= date_format(date_sub({date_time}, INTERVAL {day_count} -1  DAY),'%Y%m%d')
-  ) report
-   left join 
+   where stat_date >= date_format(date_sub({date_time}, INTERVAL {day_count}-1 DAY),'%Y%m%d')) report
+   left join
    (
 SELECT
 cua.account_id,
@@ -321,7 +459,7 @@ uc.company_name,
 pa.org_code,
 pa.org_type,
 pa.id dep_id,
-pa.depart_name,
+pa.depart_name dep_name,
 cua.user_id,
 su.realname,
 cp.sale_id,
@@ -334,14 +472,29 @@ LEFT JOIN `jeecg-boot`.user_company uc ON uc.user_id = cua.user_id
 LEFT JOIN `jeecg-boot`.sys_user su ON su.id = cua.user_id
 LEFT JOIN `jeecg-boot`.sys_user se ON su.leader_id=se.id
 LEFT JOIN `jeecg-boot`.sys_user_depart de ON su.leader_id = de.user_id
-LEFT JOIN `jeecg-boot`.sys_depart pa ON pa.id = de.dep_id) dep 
+LEFT JOIN `jeecg-boot`.sys_depart pa ON pa.id = de.dep_id) dep
 ON report.advertiser_id = dep.account_id) com
-where com.company_id is not null
-group by 
-   leader_id
-
-
-            """.format(date_time=self.date_time, day_count=self.day_count)
+group by
+company_id,
+org_code,
+org_type,
+dep_id,
+leader_id)new
+left join
+(select  ifnull(b.leader_id,'-') leader_id ,sum(a.charge) charge from
+(select advertiser_id account_id,sum(charge)charge from  media_api.kuaishou_account_report_daily
+where stat_date between date_format(date_sub(date_format(date_sub({date_time}, INTERVAL {day_count} DAY),'%Y%m%d'), INTERVAL {day_count}-1 DAY),'%Y%m%d')
+and date_format(date_sub({date_time}, INTERVAL {day_count} DAY),'%Y%m%d')
+group by advertiser_id) a
+left join
+(SELECT cua.account_id,su.leader_id
+FROM  `jeecg-boot`.ctop_user_allocation cua
+LEFT JOIN `jeecg-boot`.ctop_project cp ON cua.project_id = cp.id
+LEFT JOIN `jeecg-boot`.sys_user su ON su.id = cua.user_id) b
+on  a.account_id=b.account_id
+group by b.leader_id) ratio
+on new.leader_id=ratio.leader_id;
+""".format(date_time=self.date_time, day_count=self.day_count)
             print(sql)
             TidbConn.upsert(conn=self.conn, sql_buff=sql)
             self.conn.commit()

+ 238 - 91
Apietl/kwai_etl/Q4Task/AppKuaishouProductReportBusi.py

@@ -23,32 +23,167 @@ class AppKuaishouProductReportBusi:
             sql = """
 SET sql_mode = 'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
 REPLACE INTO application.app_kuaishou_product_report_busi_{day_count}d 
-SELECT
-concat(date_format(date_sub({date_time}, INTERVAL ({day_count} -1 )  DAY),'%Y%m%d'),'-',{date_time}) time,
-ifnull(uc.company_id,'-') company_id,
-CASE WHEN uc.company_id = 'd57fecdcf7a94d009736d9c850731582' THEN '北京汇创思拓数字科技有限公司'
-       WHEN uc.company_id = '6608ed13c0dd42de93c790dbdf124234' THEN '广州汇创思拓数字科技有限公司'
-       WHEN uc.company_id = '4b10089775c040119e139087517aed88' THEN '上海汇创思拓数字科技有限公司' ELSE uc.company_name END 
-       company_name,
- ifnull(cp.product_id,'-') product_id,
- cn.product_name,
- count(report.advertiser_id) account_num,
- sum(charge) charge,
+select
+new.start_end_time,
+new.company_id,
+new.company_name,
+new.product_id,
+new.product_name,
+new.account_num,
+new.charge,
+ifnull(ratio.charge,0) last_charge,
+ifnull(round((new.charge-ratio.charge)/ratio.charge*100,3),0) charge_ratio,
+new.show,
+new.photo_click,
+new.aclick,
+new.bclick,
+new.photo_click_ratio,
+new.play_3s_ratio,
+new.action_ratio,
+new.impression_1k_cost,
+new.photo_click_cost,
+new.action_cost,
+new.share,
+new.comment,
+new.like,
+new.follow,
+new.cancel_follow,
+new.report,
+new.block,
+new.negative,
+new.submit,
+new.download_started,
+new.download_completed,
+new.activation,
+new.event_pay_first_day,
+new.event_pay_purchase_amount_first_day,
+new.event_pay_first_day_roi,
+new.event_pay,
+new.event_pay_purchase_amount,
+new.event_pay_roi,
+new.event_register,
+new.event_register_cost,
+new.event_register_ratio,
+new.event_jin_jian_app,
+new.event_jin_jian_app_cost,
+new.event_credit_grant_app,
+new.event_credit_grant_app_cost,
+new.event_credit_grant_app_ratio,
+new.event_order_paid,
+new.event_order_paid_purchase_amount,
+new.event_order_paid_cost,
+new.event_next_day_stay,
+new.event_next_day_stay_cost,
+new.event_next_day_stay_ratio,
+new.form_count,
+new.form_cost,
+new.form_action_ratio,
+new.event_jin_jian_landing_page,
+new.event_jin_jian_landing_page_cost,
+new.event_credit_grant_landing_page,
+new.event_credit_grant_landing_page_cost,
+new.event_credit_grant_landing_page_ratio,
+new.event_valid_clues,
+new.event_valid_clues_cost,
+new.event_add_wechat,
+new.event_add_wechat_cost,
+new.event_add_wechat_ratio,
+new.event_get_through,
+new.event_get_through_cost,
+new.event_get_through_ratio,
+new.event_app_invoked,
+new.event_app_invoked_cost,
+new.event_app_invoked_ratio,
+new.event_credit_grant_landing_ratio,
+new.play_5s_ratio,
+new.play_end_ratio,
+new.event_new_user_pay,
+new.event_new_user_pay_cost,
+new.event_new_user_pay_ratio,
+new.click_1k_cost,
+new.ad_product_cnt,
+new.event_goods_view,
+new.merchant_reco_fans,
+new.event_order_amount_roi,
+new.event_goods_view_cost,
+new.merchant_reco_fans_cost,
+new.event_button_click,
+new.event_button_click_cost,
+new.event_button_click_ratio,
+new.event_order_paid_roi,
+new.event_new_user_jinjian_app,
+new.event_new_user_jinjian_app_cost,
+new.event_new_user_jinjian_app_roi,
+new.event_new_user_credit_grant_app,
+new.event_new_user_credit_grant_app_cost,
+new.event_new_user_credit_grant_app_roi,
+new.event_new_user_jinjian_page,
+new.event_new_user_jinjian_page_cost,
+new.event_new_user_jinjian_page_roi,
+new.event_new_user_credit_grant_page,
+new.event_new_user_credit_grant_page_cost,
+new.event_new_user_credit_grant_page_roi,
+new.event_appoint_form,
+new.event_appoint_form_cost,
+new.event_appoint_form_ratio,
+new.event_appoint_jump_click,
+new.event_appoint_jump_click_cost,
+new.event_appoint_jump_click_ratio,
+new.union_event_pay_purchase_amount_7d,
+new.union_event_pay_purchase_amount_7d_roi,
+new.event_order_successed,
+new.ad_photo_played_10s_ratio,
+new.key_action_cost,
+new.event_add_shopping_cart_cost,
+new.event_ad_watch_times_ratio,
+new.event_outbound_call_cost,
+new.event_outbound_call,
+new.action_new_ratio,
+new.event_watch_app_ad,
+new.event_multi_conversion_cost,
+new.event_phone_card_activate,
+new.ad_photo_played_75percent_ratio,
+new.key_action_ratio,
+new.event_ad_watch_times_cost,
+new.event_add_shopping_cart,
+new.key_action,
+new.event_multi_conversion,
+new.event_wechat_connected,
+new.event_dsp_gift_form,
+new.event_intention_confirmed,
+new.event_phone_get_through,
+new.event_multi_conversion_ratio,
+new.event_measurement_house,
+new.ad_photo_played_2s_ratio,
+new.event_outbound_call_ratio,
+new.event_ad_watch_times
+from
+(SELECT
+concat(date_format(date_sub({date_time},INTERVAL {day_count}-1 DAY),'%Y%m%d'),'-',{date_time}) start_end_time,
+uc.company_id,
+CASE WHEN uc.company_id='d57fecdcf7a94d009736d9c850731582' THEN '北京汇创思拓数字科技有限公司'
+WHEN uc.company_id='6608ed13c0dd42de93c790dbdf124234' THEN '广州汇创思拓数字科技有限公司'
+WHEN uc.company_id='4b10089775c040119e139087517aed88' THEN '上海汇创思拓数字科技有限公司'
+ELSE uc.company_name END company_name,
+cp.product_id,
+cn.product_name,
+count(report.advertiser_id) account_num,
+sum(charge) charge,
 sum(`show`) `show`,
-sum(photo_click)photo_click,
-sum(aclick)  aclick,
+sum(photo_click) photo_click,
+sum(aclick) aclick,
 sum(bclick) bclick,
-ifnull(round(sum(photo_click)/sum(`show`),3),0) photo_click_ratio,
-ifnull(round(sum(play_3s_ratio)/{day_count}*100,3),0)  play_3s_ratio,
-ifnull(round(sum(bclick)/sum(aclick)*100,3),0) action_ratio,
-ifnull(round(sum(impression_1k_cost)/{day_count},3),0) impression_1k_cost,
-ifnull(round(sum(charge)/sum(photo_click),3),0) photo_click_cost,
-ifnull(round(sum(charge)/sum(bclick),3),0) action_cost,
+round(sum(photo_click)/sum(`show`),3) photo_click_ratio,
+round(sum(play_3s_ratio)/{day_count}*100,3) play_3s_ratio,
+round(sum(bclick)/sum(aclick)*100,3) action_ratio,
+round(sum(impression_1k_cost)/{day_count},3) impression_1k_cost,
+round(sum(charge)/sum(photo_click),3) photo_click_cost,
+round(sum(charge)/sum(bclick),3) action_cost,
 sum(`share`) `share`,
 sum(`comment`) `comment`,
-sum(`like`)  `like`,
+sum(`like`) `like`,
 sum(follow) follow,
-sum(cancel_follow)  cancel_follow,
+sum(cancel_follow) cancel_follow,
 sum(report) report,
 sum(block) block,
 sum(negative) negative,
@@ -58,94 +193,94 @@ sum(download_completed) download_completed,
 sum(activation) activation,
 sum(event_pay_first_day) event_pay_first_day,
 sum(event_pay_purchase_amount_first_day) event_pay_purchase_amount_first_day,
-ifnull(round(sum(event_pay_purchase_amount_first_day)/sum(charge)*100,3),0) event_pay_first_day_roi ,
+round(sum(event_pay_purchase_amount_first_day)/sum(charge)*100,3) event_pay_first_day_roi,
 sum(event_pay) event_pay,
 sum(event_pay_purchase_amount) event_pay_purchase_amount,
-ifnull(round(sum(event_pay_purchase_amount)/sum(charge)*100,3),0) event_pay_roi,
+round(sum(event_pay_purchase_amount)/sum(charge)*100,3) event_pay_roi,
 sum(event_register) event_register,
-ifnull(round(sum(charge)/sum(event_register),3),0) event_register_cost,
-ifnull(round(sum(event_register)/sum(activation)*100,3),0) event_register_ratio,
+round(sum(charge)/sum(event_register),3) event_register_cost,
+round(sum(event_register)/sum(activation)*100,3) event_register_ratio,
 sum(event_jin_jian_app) event_jin_jian_app,
-ifnull(round(sum(charge)/sum(event_jin_jian_app),3),0) event_jin_jian_app_cost,
+round(sum(charge)/sum(event_jin_jian_app),3) event_jin_jian_app_cost,
 sum(event_credit_grant_app) event_credit_grant_app,
-ifnull(round(sum(charge)/sum(event_credit_grant_app),3),0) event_credit_grant_app_cost ,
-ifnull(round(sum(event_credit_grant_app_ratio)/{day_count}*100,3),0) event_credit_grant_app_ratio,
+round(sum(charge)/sum(event_credit_grant_app),3) event_credit_grant_app_cost,
+round(sum(event_credit_grant_app_ratio)/{day_count}*100,3) event_credit_grant_app_ratio,
 sum(event_order_paid) event_order_paid,
 sum(event_order_paid_purchase_amount) event_order_paid_purchase_amount,
-ifnull(round(sum(event_order_paid_purchase_amount)/sum(event_order_paid) ,3),0) event_order_paid_cost,
+round(sum(event_order_paid_purchase_amount)/sum(event_order_paid),3) event_order_paid_cost,
 sum(event_next_day_stay) event_next_day_stay,
-ifnull(round(sum(charge)/sum(event_next_day_stay),3),0) event_next_day_stay_cost,
-ifnull(round(sum(event_next_day_stay)/sum(activation)*100,3),0) event_next_day_stay_ratio,
+round(sum(charge)/sum(event_next_day_stay),3) event_next_day_stay_cost,
+round(sum(event_next_day_stay)/sum(activation)*100,3) event_next_day_stay_ratio,
 sum(form_count) form_count,
-ifnull(round(sum(charge)/sum(form_count),3),0) form_cost,
-ifnull(round(sum(form_count)/sum(`bclick`)*100,3),0) form_action_ratio,
+round(sum(charge)/sum(form_count),3) form_cost,
+round(sum(form_count)/sum(`bclick`)*100,3) form_action_ratio,
 sum(event_jin_jian_landing_page) event_jin_jian_landing_page,
-ifnull(round(sum(charge)/sum(event_jin_jian_landing_page),3),0) event_jin_jian_landing_page_cost,
+round(sum(charge)/sum(event_jin_jian_landing_page),3) event_jin_jian_landing_page_cost,
 sum(event_credit_grant_landing_page) event_credit_grant_landing_page,
-ifnull(round(sum(charge)/sum(event_credit_grant_landing_page),3),0) event_credit_grant_landing_page_cost,
-ifnull(round(sum(event_credit_grant_landing_page)/{day_count}*100,3),0) event_credit_grant_landing_page_ratio,
+round(sum(charge)/sum(event_credit_grant_landing_page),3) event_credit_grant_landing_page_cost,
+round(sum(event_credit_grant_landing_page)/{day_count}*100,3) event_credit_grant_landing_page_ratio,
 sum(event_valid_clues) event_valid_clues,
-ifnull(round(sum(charge)/sum(event_valid_clues),3),0) event_valid_clues_cost,
+round(sum(charge)/sum(event_valid_clues),3) event_valid_clues_cost,
 sum(event_add_wechat) event_add_wechat,
-ifnull(round(sum(charge)/sum(event_add_wechat),3),0) event_add_wechat_cost,
-ifnull(round(sum(event_add_wechat)/sum(bclick)*100,3),0) event_add_wechat_ratio,
+round(sum(charge)/sum(event_add_wechat),3) event_add_wechat_cost,
+round(sum(event_add_wechat)/sum(bclick)*100,3) event_add_wechat_ratio,
 sum(event_get_through) event_get_through,
-ifnull(round(sum(charge)/sum(event_get_through),3),0) event_get_through_cost,
-ifnull(round(sum(event_get_through)/sum(bclick)*100,3),0) event_get_through_ratio,
+round(sum(charge)/sum(event_get_through),3) event_get_through_cost,
+round(sum(event_get_through)/sum(bclick)*100,3) event_get_through_ratio,
 sum(event_app_invoked) event_app_invoked,
-ifnull(round(sum(charge)/sum(event_app_invoked),3),0) event_app_invoked_cost ,
-ifnull(round(sum(event_app_invoked)/sum(bclick)*100,3),0) event_app_invoked_ratio,
-ifnull(round(sum(event_credit_grant_landing_ratio)/{day_count}*100,3),0) event_credit_grant_landing_ratio,
-ifnull(round(sum(play_5s_ratio)/{day_count}*100,3),0) play_5s_ratio,
-ifnull(round(sum(play_end_ratio)/{day_count}*100,3),0) play_end_ratio,
+round(sum(charge)/sum(event_app_invoked),3) event_app_invoked_cost,
+round(sum(event_app_invoked)/sum(bclick)*100,3) event_app_invoked_ratio,
+round(sum(event_credit_grant_landing_ratio)/{day_count}*100,3) event_credit_grant_landing_ratio,
+round(sum(play_5s_ratio)/{day_count}*100,3) play_5s_ratio,
+round(sum(play_end_ratio)/{day_count}*100,3) play_end_ratio,
 sum(event_new_user_pay) event_new_user_pay,
-ifnull(round(sum(charge)/sum(event_new_user_pay),3),0) event_new_user_pay_cost,
-ifnull(round(sum(event_new_user_pay)/sum(activation)*100,3),0) event_new_user_pay_ratio,
-ifnull(round(sum(click_1k_cost)/{day_count}*100,3),0) click_1k_cost,
+round(sum(charge)/sum(event_new_user_pay),3) event_new_user_pay_cost,
+round(sum(event_new_user_pay)/sum(activation)*100,3) event_new_user_pay_ratio,
+round(sum(click_1k_cost)/{day_count}*100) click_1k_cost,
 sum(ad_product_cnt) ad_product_cnt,
 sum(event_goods_view) event_goods_view,
 sum(merchant_reco_fans) merchant_reco_fans,
-ifnull(round(sum(event_order_amount_roi)/{day_count}*100,3),0) event_order_amount_roi,
-ifnull(round(sum(charge)/sum(event_goods_view) ,3),0) event_goods_view_cost,
-ifnull(round(sum(charge)/sum(merchant_reco_fans),3),0) merchant_reco_fans_cost,
+round(sum(event_order_amount_roi)/{day_count}*100,3) event_order_amount_roi,
+round(sum(charge)/sum(event_goods_view),3) event_goods_view_cost,
+round(sum(charge)/sum(merchant_reco_fans),3) merchant_reco_fans_cost,
 sum(event_button_click) event_button_click,
-ifnull(round(sum(charge)/sum(event_button_click),3),0) event_button_click_cost,
-ifnull(round(sum(event_button_click)/sum(bclick)*100,3),0) event_button_click_ratio ,
-ifnull(round(sum(event_order_paid_roi)/{day_count}*100,3),0) event_order_paid_roi,
+round(sum(charge)/sum(event_button_click),3) event_button_click_cost,
+round(sum(event_button_click)/sum(bclick)*100,3) event_button_click_ratio,
+round(sum(event_order_paid_roi)/{day_count}*100,3) event_order_paid_roi,
 sum(event_new_user_jinjian_app) event_new_user_jinjian_app,
-ifnull(round(sum(charge)/sum(event_new_user_jinjian_app),3),0) event_new_user_jinjian_app_cost,
-ifnull(round(sum(event_new_user_jinjian_app)/sum(form_count)*100,3),0) event_new_user_jinjian_app_roi ,
+round(sum(charge)/sum(event_new_user_jinjian_app),3) event_new_user_jinjian_app_cost,
+round(sum(event_new_user_jinjian_app)/sum(form_count)*100,3) event_new_user_jinjian_app_roi,
 sum(event_new_user_credit_grant_app) event_new_user_credit_grant_app,
-ifnull(round(sum(charge)/sum(event_new_user_credit_grant_app),3),0)  event_new_user_credit_grant_app_cost,
-ifnull(round(sum(event_new_user_credit_grant_app)/sum(form_count)*100,3),0) event_new_user_credit_grant_app_roi,
+round(sum(charge)/sum(event_new_user_credit_grant_app),3) event_new_user_credit_grant_app_cost,
+round(sum(event_new_user_credit_grant_app)/sum(form_count)*100,3) event_new_user_credit_grant_app_roi,
 sum(event_new_user_jinjian_page) event_new_user_jinjian_page,
-ifnull(round(sum(charge)/sum(event_new_user_jinjian_page),3),0) event_new_user_jinjian_page_cost,
-ifnull(round(sum(event_new_user_jinjian_page)/sum(form_count)*100,3),0) event_new_user_jinjian_page_roi ,
+round(sum(charge)/sum(event_new_user_jinjian_page),3) event_new_user_jinjian_page_cost,
+round(sum(event_new_user_jinjian_page)/sum(form_count)*100,3) event_new_user_jinjian_page_roi,
 sum(event_new_user_credit_grant_page) event_new_user_credit_grant_page,
-ifnull(round(sum(charge)/sum(event_new_user_credit_grant_page) ,3),0) event_new_user_credit_grant_page_cost,
-ifnull(round(sum(event_new_user_credit_grant_page)/sum(form_count)*100,3),0) event_new_user_credit_grant_page_roi,
-sum(event_appoint_form)  event_appoint_form,
-ifnull(round(sum(charge)/sum(event_appoint_form),3),0) event_appoint_form_cost,
-ifnull(round(sum(event_appoint_form)/sum(form_count)*100,3),0) event_appoint_form_ratio,
+round(sum(charge)/sum(event_new_user_credit_grant_page),3) event_new_user_credit_grant_page_cost,
+round(sum(event_new_user_credit_grant_page)/sum(form_count)*100,3) event_new_user_credit_grant_page_roi,
+sum(event_appoint_form) event_appoint_form,
+round(sum(charge)/sum(event_appoint_form),3) event_appoint_form_cost,
+round(sum(event_appoint_form)/sum(form_count)*100,3) event_appoint_form_ratio,
 sum(event_appoint_jump_click) event_appoint_jump_click,
-ifnull(round(sum(charge)/sum(event_appoint_jump_click),3),0)  event_appoint_jump_click_cost,
-ifnull(round(sum(event_appoint_jump_click_cost)/sum(form_count)*100,3),0) event_appoint_jump_click_ratio,
+round(sum(charge)/sum(event_appoint_jump_click),3) event_appoint_jump_click_cost,
+round(sum(event_appoint_jump_click_cost)/sum(form_count)*100,3) event_appoint_jump_click_ratio,
 sum(union_event_pay_purchase_amount_7d) union_event_pay_purchase_amount_7d,
-ifnull(round(sum(union_event_pay_purchase_amount_7d_roi)/{day_count}*100,3),0) union_event_pay_purchase_amount_7d_roi,
+round(sum(union_event_pay_purchase_amount_7d_roi)/{day_count}*100,3) union_event_pay_purchase_amount_7d_roi,
 sum(event_order_successed) event_order_successed,
-ifnull(round(sum(ad_photo_played_10s_ratio)/{day_count}*100,3),0) ad_photo_played_10s_ratio,
-ifnull(round(sum(charge)/sum(key_action),3),0) key_action_cost,
-ifnull(round(sum(charge)/sum(event_add_shopping_cart) ,3),0) event_add_shopping_cart_cost ,
-ifnull(round(sum(event_ad_watch_times)/sum(bclick)*100,3),0) event_ad_watch_times_ratio,
-ifnull(round(sum(charge)/sum(event_outbound_call),3),0) event_outbound_call_cost,
+round(sum(ad_photo_played_10s_ratio)/{day_count}*100,3) ad_photo_played_10s_ratio,
+round(sum(charge)/sum(key_action),3) key_action_cost,
+round(sum(charge)/sum(event_add_shopping_cart),3) event_add_shopping_cart_cost,
+round(sum(event_ad_watch_times)/sum(bclick)*100,3) event_ad_watch_times_ratio,
+round(sum(charge)/sum(event_outbound_call),3) event_outbound_call_cost,
 sum(event_outbound_call) event_outbound_call,
-ifnull(round(sum(action_new_ratio)/{day_count}*100,3),0) action_new_ratio,
+round(sum(action_new_ratio)/{day_count}*100,3) action_new_ratio,
 sum(event_watch_app_ad) event_watch_app_ad,
-ifnull(round(sum(charge)/sum(event_multi_conversion) ,3),0) event_multi_conversion_cost,
+round(sum(charge)/sum(event_multi_conversion),3) event_multi_conversion_cost,
 sum(event_phone_card_activate) event_phone_card_activate,
-ifnull(round(sum(ad_photo_played_75percent_ratio)/{day_count}*100,3),0) ad_photo_played_75percent_ratio,
-ifnull(round(sum(key_action)/sum(activation)*100,3),0) key_action_ratio ,
-ifnull(round(sum(charge)/sum(event_ad_watch_times),3),0) event_ad_watch_times_cost,
+round(sum(ad_photo_played_75percent_ratio)/{day_count}*100,3) ad_photo_played_75percent_ratio,
+round(sum(key_action)/sum(activation)*100,3) key_action_ratio,
+round(sum(charge)/sum(event_ad_watch_times),3) event_ad_watch_times_cost,
 sum(event_add_shopping_cart) event_add_shopping_cart,
 sum(key_action) key_action,
 sum(event_multi_conversion) event_multi_conversion,
@@ -153,21 +288,33 @@ sum(event_wechat_connected) event_wechat_connected,
 sum(event_dsp_gift_form) event_dsp_gift_form,
 sum(event_intention_confirmed) event_intention_confirmed,
 sum(event_phone_get_through) event_phone_get_through,
-ifnull(round(sum(event_multi_conversion)/sum(activation)*100,3),0)  event_multi_conversion_ratio,
+round(sum(event_multi_conversion)/sum(activation)*100,3) event_multi_conversion_ratio,
 sum(event_measurement_house) event_measurement_house,
-ifnull(round(sum(ad_photo_played_2s_ratio)/{day_count}*100,3),0) ad_photo_played_2s_ratio,
-ifnull(round(sum(event_outbound_call_ratio)/sum(activation)*100,3),0) event_outbound_call_ratio,
+round(sum(ad_photo_played_2s_ratio)/{day_count}*100,3) ad_photo_played_2s_ratio,
+round(sum(event_outbound_call_ratio)/sum(activation)*100,3) event_outbound_call_ratio,
 sum(event_ad_watch_times) event_ad_watch_times
- from  
- (select * from  media_api.kuaishou_account_report_daily  
- where stat_date >= date_format(date_sub({date_time}, INTERVAL {day_count}-1 DAY),'%Y%m%d')
- ) report
-LEFT JOIN `jeecg-boot`.ctop_user_allocation cua ON report.advertiser_id = cua.account_id
-LEFT JOIN `jeecg-boot`.ctop_project cp ON cua.project_id = cp.id
-LEFT JOIN `jeecg-boot`.ctop_product cn ON cp.product_id = cn.id
-LEFT JOIN `jeecg-boot`.user_company uc ON uc.user_id = cua.user_id
+from
+(select * from media_api.kuaishou_account_report_daily where stat_date>=date_format(date_sub({date_time},INTERVAL {day_count}-1 DAY),'%Y%m%d')) report
+LEFT JOIN`jeecg-boot`.ctop_user_allocation cua ON report.advertiser_id=cua.account_id
+LEFT JOIN`jeecg-boot`.ctop_project cp ON cua.project_id=cp.id
+LEFT JOIN`jeecg-boot`.ctop_product cn ON cp.product_id=cn.id
+LEFT JOIN`jeecg-boot`.user_company uc ON uc.user_id=cua.user_id
 GROUP BY
-cp.product_id;
+uc.company_id,
+cp.product_id) new
+LEFT JOIN
+(select ifnull(b.product_id,'-') product_id,sum(a.charge) charge from
+(select advertiser_id account_id,sum(charge) charge from media_api.kuaishou_account_report_daily
+where stat_date between date_format(date_sub(date_format(date_sub({date_time},INTERVAL {day_count} DAY),'%Y%m%d'),INTERVAL {day_count}-1 DAY),'%Y%m%d')
+and date_format(date_sub({date_time},INTERVAL {day_count} DAY),'%Y%m%d')
+group by advertiser_id) a
+left join
+(SELECT cua.account_id,cp.product_id,cp.id project_id
+FROM`jeecg-boot`.ctop_user_allocation cua
+LEFT JOIN`jeecg-boot`.ctop_project cp ON cua.project_id=cp.id) b
+on a.account_id=b.account_id
+group by b.product_id) ratio
+on new.product_id=ratio.product_id;
             """.format(date_time=self.date_time, day_count=self.day_count)
             print(sql)
             TidbConn.upsert(conn=self.conn, sql_buff=sql)

+ 243 - 93
Apietl/kwai_etl/Q4Task/AppKuaishouProjectReportBusi.py

@@ -23,34 +23,171 @@ class AppKuaishouProjectReportBusi:
             sql = """
 SET sql_mode = 'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
 REPLACE INTO application.app_kuaishou_project_report_busi_{day_count}d
-SELECT
-concat(
-date_format(date_sub({date_time}, INTERVAL ({day_count} -1 )  DAY),'%Y%m%d'),'-',{date_time}) time,
-ifnull(uc.company_id,'-') company_id,
-CASE WHEN uc.company_id = 'd57fecdcf7a94d009736d9c850731582' THEN '北京汇创思拓数字科技有限公司'
-       WHEN uc.company_id = '6608ed13c0dd42de93c790dbdf124234' THEN '广州汇创思拓数字科技有限公司'
-       WHEN uc.company_id = '4b10089775c040119e139087517aed88' THEN '上海汇创思拓数字科技有限公司' ELSE uc.company_name END company_name,
- ifnull(cp.product_id,'-') product_id,
- cn.product_name,
- ifnull(cua.project_id,'-') project_id,
- cp.project_name,
- count(report.advertiser_id) account_num,
- sum(charge) charge,
+select
+new.start_end_time,
+new.company_id,
+new.company_name,
+new.product_id,
+new.product_name,
+new.project_id,
+new.project_name,
+new.account_num,
+new.charge,
+ifnull(ratio.charge,0) last_charge,
+ifnull(round((new.charge-ratio.charge)/ratio.charge*100,3),0) charge_ratio,
+new.show,
+new.photo_click,
+new.aclick,
+new.bclick,
+new.photo_click_ratio,
+new.play_3s_ratio,
+new.action_ratio,
+new.impression_1k_cost,
+new.photo_click_cost,
+new.action_cost,
+new.share,
+new.comment,
+new.like,
+new.follow,
+new.cancel_follow,
+new.report,
+new.block,
+new.negative,
+new.submit,
+new.download_started,
+new.download_completed,
+new.activation,
+new.event_pay_first_day,
+new.event_pay_purchase_amount_first_day,
+new.event_pay_first_day_roi,
+new.event_pay,
+new.event_pay_purchase_amount,
+new.event_pay_roi,
+new.event_register,
+new.event_register_cost,
+new.event_register_ratio,
+new.event_jin_jian_app,
+new.event_jin_jian_app_cost,
+new.event_credit_grant_app,
+new.event_credit_grant_app_cost,
+new.event_credit_grant_app_ratio,
+new.event_order_paid,
+new.event_order_paid_purchase_amount,
+new.event_order_paid_cost,
+new.event_next_day_stay,
+new.event_next_day_stay_cost,
+new.event_next_day_stay_ratio,
+new.form_count,
+new.form_cost,
+new.form_action_ratio,
+new.event_jin_jian_landing_page,
+new.event_jin_jian_landing_page_cost,
+new.event_credit_grant_landing_page,
+new.event_credit_grant_landing_page_cost,
+new.event_credit_grant_landing_page_ratio,
+new.event_valid_clues,
+new.event_valid_clues_cost,
+new.event_add_wechat,
+new.event_add_wechat_cost,
+new.event_add_wechat_ratio,
+new.event_get_through,
+new.event_get_through_cost,
+new.event_get_through_ratio,
+new.event_app_invoked,
+new.event_app_invoked_cost,
+new.event_app_invoked_ratio,
+new.event_credit_grant_landing_ratio,
+new.play_5s_ratio,
+new.play_end_ratio,
+new.event_new_user_pay,
+new.event_new_user_pay_cost,
+new.event_new_user_pay_ratio,
+new.click_1k_cost,
+new.ad_product_cnt,
+new.event_goods_view,
+new.merchant_reco_fans,
+new.event_order_amount_roi,
+new.event_goods_view_cost,
+new.merchant_reco_fans_cost,
+new.event_button_click,
+new.event_button_click_cost,
+new.event_button_click_ratio,
+new.event_order_paid_roi,
+new.event_new_user_jinjian_app,
+new.event_new_user_jinjian_app_cost,
+new.event_new_user_jinjian_app_roi,
+new.event_new_user_credit_grant_app,
+new.event_new_user_credit_grant_app_cost,
+new.event_new_user_credit_grant_app_roi,
+new.event_new_user_jinjian_page,
+new.event_new_user_jinjian_page_cost,
+new.event_new_user_jinjian_page_roi,
+new.event_new_user_credit_grant_page,
+new.event_new_user_credit_grant_page_cost,
+new.event_new_user_credit_grant_page_roi,
+new.event_appoint_form,
+new.event_appoint_form_cost,
+new.event_appoint_form_ratio,
+new.event_appoint_jump_click,
+new.event_appoint_jump_click_cost,
+new.event_appoint_jump_click_ratio,
+new.union_event_pay_purchase_amount_7d,
+new.union_event_pay_purchase_amount_7d_roi,
+new.event_order_successed,
+new.ad_photo_played_10s_ratio,
+new.key_action_cost,
+new.event_add_shopping_cart_cost,
+new.event_ad_watch_times_ratio,
+new.event_outbound_call_cost,
+new.event_outbound_call,
+new.action_new_ratio,
+new.event_watch_app_ad,
+new.event_multi_conversion_cost,
+new.event_phone_card_activate,
+new.ad_photo_played_75percent_ratio,
+new.key_action_ratio,
+new.event_ad_watch_times_cost,
+new.event_add_shopping_cart,
+new.key_action,
+new.event_multi_conversion,
+new.event_wechat_connected,
+new.event_dsp_gift_form,
+new.event_intention_confirmed,
+new.event_phone_get_through,
+new.event_multi_conversion_ratio,
+new.event_measurement_house,
+new.ad_photo_played_2s_ratio,
+new.event_outbound_call_ratio,
+new.event_ad_watch_times
+from
+(SELECT
+concat(date_format(date_sub({date_time},INTERVAL {day_count}-1 DAY),'%Y%m%d'),'-',{date_time}) start_end_time,
+uc.company_id,
+CASE WHEN uc.company_id='d57fecdcf7a94d009736d9c850731582' THEN '北京汇创思拓数字科技有限公司'
+WHEN uc.company_id='6608ed13c0dd42de93c790dbdf124234' THEN '广州汇创思拓数字科技有限公司'
+WHEN uc.company_id='4b10089775c040119e139087517aed88' THEN '上海汇创思拓数字科技有限公司'
+ELSE uc.company_name END company_name,
+cp.product_id,
+cn.product_name,
+cua.project_id,
+cp.project_name,
+count(report.advertiser_id) account_num,
+sum(charge) charge,
 sum(`show`) `show`,
-sum(photo_click)photo_click,
-sum(aclick)  aclick,
+sum(photo_click) photo_click,
+sum(aclick) aclick,
 sum(bclick) bclick,
-ifnull(round(sum(photo_click)/sum(`show`),3),0) photo_click_ratio,
-ifnull(round(sum(play_3s_ratio)/{day_count}*100,3),0)  play_3s_ratio,
-ifnull(round(sum(bclick)/sum(aclick)*100,3),0) action_ratio,
-ifnull(round(sum(impression_1k_cost)/{day_count},3),0) impression_1k_cost,
-ifnull(round(sum(charge)/sum(photo_click),3),0) photo_click_cost,
-ifnull(round(sum(charge)/sum(bclick),3),0) action_cost,
+round(sum(photo_click)/sum(`show`),3) photo_click_ratio,
+round(sum(play_3s_ratio)/{day_count}*100,3) play_3s_ratio,
+round(sum(bclick)/sum(aclick)*100,3) action_ratio,
+round(sum(impression_1k_cost)/{day_count},3) impression_1k_cost,
+round(sum(charge)/sum(photo_click),3) photo_click_cost,
+round(sum(charge)/sum(bclick),3) action_cost,
 sum(`share`) `share`,
 sum(`comment`) `comment`,
-sum(`like`)  `like`,
+sum(`like`) `like`,
 sum(follow) follow,
-sum(cancel_follow)  cancel_follow,
+sum(cancel_follow) cancel_follow,
 sum(report) report,
 sum(block) block,
 sum(negative) negative,
@@ -60,94 +197,94 @@ sum(download_completed) download_completed,
 sum(activation) activation,
 sum(event_pay_first_day) event_pay_first_day,
 sum(event_pay_purchase_amount_first_day) event_pay_purchase_amount_first_day,
-ifnull(round(sum(event_pay_purchase_amount_first_day)/sum(charge)*100,3),0) event_pay_first_day_roi ,
+round(sum(event_pay_purchase_amount_first_day)/sum(charge)*100,3) event_pay_first_day_roi,
 sum(event_pay) event_pay,
 sum(event_pay_purchase_amount) event_pay_purchase_amount,
-ifnull(round(sum(event_pay_purchase_amount)/sum(charge)*100,3),0) event_pay_roi,
+round(sum(event_pay_purchase_amount)/sum(charge)*100,3) event_pay_roi,
 sum(event_register) event_register,
-ifnull(round(sum(charge)/sum(event_register),3),0) event_register_cost,
-ifnull(round(sum(event_register)/sum(activation)*100,3),0) event_register_ratio,
+round(sum(charge)/sum(event_register),3) event_register_cost,
+round(sum(event_register)/sum(activation)*100,3) event_register_ratio,
 sum(event_jin_jian_app) event_jin_jian_app,
-ifnull(round(sum(charge)/sum(event_jin_jian_app),3),0) event_jin_jian_app_cost,
+round(sum(charge)/sum(event_jin_jian_app),3) event_jin_jian_app_cost,
 sum(event_credit_grant_app) event_credit_grant_app,
-ifnull(round(sum(charge)/sum(event_credit_grant_app),3),0) event_credit_grant_app_cost ,
-ifnull(round(sum(event_credit_grant_app_ratio)/{day_count}*100,3),0) event_credit_grant_app_ratio,
+round(sum(charge)/sum(event_credit_grant_app),3) event_credit_grant_app_cost,
+round(sum(event_credit_grant_app_ratio)/{day_count}*100,3) event_credit_grant_app_ratio,
 sum(event_order_paid) event_order_paid,
 sum(event_order_paid_purchase_amount) event_order_paid_purchase_amount,
-ifnull(round(sum(event_order_paid_purchase_amount)/sum(event_order_paid) ,3),0) event_order_paid_cost,
+round(sum(event_order_paid_purchase_amount)/sum(event_order_paid),3) event_order_paid_cost,
 sum(event_next_day_stay) event_next_day_stay,
-ifnull(round(sum(charge)/sum(event_next_day_stay),3),0) event_next_day_stay_cost,
-ifnull(round(sum(event_next_day_stay)/sum(activation)*100,3),0) event_next_day_stay_ratio,
+round(sum(charge)/sum(event_next_day_stay),3) event_next_day_stay_cost,
+round(sum(event_next_day_stay)/sum(activation)*100,3) event_next_day_stay_ratio,
 sum(form_count) form_count,
-ifnull(round(sum(charge)/sum(form_count),3),0) form_cost,
-ifnull(round(sum(form_count)/sum(`bclick`)*100,3),0) form_action_ratio,
+round(sum(charge)/sum(form_count),3) form_cost,
+round(sum(form_count)/sum(`bclick`)*100,3) form_action_ratio,
 sum(event_jin_jian_landing_page) event_jin_jian_landing_page,
-ifnull(round(sum(charge)/sum(event_jin_jian_landing_page),3),0) event_jin_jian_landing_page_cost,
+round(sum(charge)/sum(event_jin_jian_landing_page),3) event_jin_jian_landing_page_cost,
 sum(event_credit_grant_landing_page) event_credit_grant_landing_page,
-ifnull(round(sum(charge)/sum(event_credit_grant_landing_page),3),0) event_credit_grant_landing_page_cost,
-ifnull(round(sum(event_credit_grant_landing_page)/{day_count}*100,3),0) event_credit_grant_landing_page_ratio,
+round(sum(charge)/sum(event_credit_grant_landing_page),3) event_credit_grant_landing_page_cost,
+round(sum(event_credit_grant_landing_page)/{day_count}*100,3) event_credit_grant_landing_page_ratio,
 sum(event_valid_clues) event_valid_clues,
-ifnull(round(sum(charge)/sum(event_valid_clues),3),0) event_valid_clues_cost,
+round(sum(charge)/sum(event_valid_clues),3) event_valid_clues_cost,
 sum(event_add_wechat) event_add_wechat,
-ifnull(round(sum(charge)/sum(event_add_wechat),3),0) event_add_wechat_cost,
-ifnull(round(sum(event_add_wechat)/sum(bclick)*100,3),0) event_add_wechat_ratio,
+round(sum(charge)/sum(event_add_wechat),3) event_add_wechat_cost,
+round(sum(event_add_wechat)/sum(bclick)*100,3) event_add_wechat_ratio,
 sum(event_get_through) event_get_through,
-ifnull(round(sum(charge)/sum(event_get_through),3),0) event_get_through_cost,
-ifnull(round(sum(event_get_through)/sum(bclick)*100,3),0) event_get_through_ratio,
+round(sum(charge)/sum(event_get_through),3) event_get_through_cost,
+round(sum(event_get_through)/sum(bclick)*100,3) event_get_through_ratio,
 sum(event_app_invoked) event_app_invoked,
-ifnull(round(sum(charge)/sum(event_app_invoked),3),0) event_app_invoked_cost ,
-ifnull(round(sum(event_app_invoked)/sum(bclick)*100,3),0) event_app_invoked_ratio,
-ifnull(round(sum(event_credit_grant_landing_ratio)/{day_count}*100,3),0) event_credit_grant_landing_ratio,
-ifnull(round(sum(play_5s_ratio)/{day_count}*100,3),0) play_5s_ratio,
-ifnull(round(sum(play_end_ratio)/{day_count}*100,3),0) play_end_ratio,
+round(sum(charge)/sum(event_app_invoked),3) event_app_invoked_cost,
+round(sum(event_app_invoked)/sum(bclick)*100,3) event_app_invoked_ratio,
+round(sum(event_credit_grant_landing_ratio)/{day_count}*100,3) event_credit_grant_landing_ratio,
+round(sum(play_5s_ratio)/{day_count}*100,3) play_5s_ratio,
+round(sum(play_end_ratio)/{day_count}*100,3) play_end_ratio,
 sum(event_new_user_pay) event_new_user_pay,
-ifnull(round(sum(charge)/sum(event_new_user_pay),3),0) event_new_user_pay_cost,
-ifnull(round(sum(event_new_user_pay)/sum(activation)*100,3),0) event_new_user_pay_ratio,
-ifnull(round(sum(click_1k_cost)/{day_count}*100,3),0) click_1k_cost,
+round(sum(charge)/sum(event_new_user_pay),3) event_new_user_pay_cost,
+round(sum(event_new_user_pay)/sum(activation)*100,3) event_new_user_pay_ratio,
+round(sum(click_1k_cost)/{day_count}*100) click_1k_cost,
 sum(ad_product_cnt) ad_product_cnt,
 sum(event_goods_view) event_goods_view,
 sum(merchant_reco_fans) merchant_reco_fans,
-ifnull(round(sum(event_order_amount_roi)/{day_count}*100,3),0) event_order_amount_roi,
-ifnull(round(sum(charge)/sum(event_goods_view) ,3),0) event_goods_view_cost,
-ifnull(round(sum(charge)/sum(merchant_reco_fans),3),0) merchant_reco_fans_cost,
+round(sum(event_order_amount_roi)/{day_count}*100,3) event_order_amount_roi,
+round(sum(charge)/sum(event_goods_view),3) event_goods_view_cost,
+round(sum(charge)/sum(merchant_reco_fans),3) merchant_reco_fans_cost,
 sum(event_button_click) event_button_click,
-ifnull(round(sum(charge)/sum(event_button_click),3),0) event_button_click_cost,
-ifnull(round(sum(event_button_click)/sum(bclick)*100,3),0) event_button_click_ratio ,
-ifnull(round(sum(event_order_paid_roi)/{day_count}*100,3),0) event_order_paid_roi,
+round(sum(charge)/sum(event_button_click),3) event_button_click_cost,
+round(sum(event_button_click)/sum(bclick)*100,3) event_button_click_ratio,
+round(sum(event_order_paid_roi)/{day_count}*100,3) event_order_paid_roi,
 sum(event_new_user_jinjian_app) event_new_user_jinjian_app,
-ifnull(round(sum(charge)/sum(event_new_user_jinjian_app),3),0) event_new_user_jinjian_app_cost,
-ifnull(round(sum(event_new_user_jinjian_app)/sum(form_count)*100,3),0) event_new_user_jinjian_app_roi ,
+round(sum(charge)/sum(event_new_user_jinjian_app),3) event_new_user_jinjian_app_cost,
+round(sum(event_new_user_jinjian_app)/sum(form_count)*100,3) event_new_user_jinjian_app_roi,
 sum(event_new_user_credit_grant_app) event_new_user_credit_grant_app,
-ifnull(round(sum(charge)/sum(event_new_user_credit_grant_app),3),0)  event_new_user_credit_grant_app_cost,
-ifnull(round(sum(event_new_user_credit_grant_app)/sum(form_count)*100,3),0) event_new_user_credit_grant_app_roi,
+round(sum(charge)/sum(event_new_user_credit_grant_app),3) event_new_user_credit_grant_app_cost,
+round(sum(event_new_user_credit_grant_app)/sum(form_count)*100,3) event_new_user_credit_grant_app_roi,
 sum(event_new_user_jinjian_page) event_new_user_jinjian_page,
-ifnull(round(sum(charge)/sum(event_new_user_jinjian_page),3),0) event_new_user_jinjian_page_cost,
-ifnull(round(sum(event_new_user_jinjian_page)/sum(form_count)*100,3),0) event_new_user_jinjian_page_roi ,
+round(sum(charge)/sum(event_new_user_jinjian_page),3) event_new_user_jinjian_page_cost,
+round(sum(event_new_user_jinjian_page)/sum(form_count)*100,3) event_new_user_jinjian_page_roi,
 sum(event_new_user_credit_grant_page) event_new_user_credit_grant_page,
-ifnull(round(sum(charge)/sum(event_new_user_credit_grant_page) ,3),0) event_new_user_credit_grant_page_cost,
-ifnull(round(sum(event_new_user_credit_grant_page)/sum(form_count)*100,3),0) event_new_user_credit_grant_page_roi,
-sum(event_appoint_form)  event_appoint_form,
-ifnull(round(sum(charge)/sum(event_appoint_form),3),0) event_appoint_form_cost,
-ifnull(round(sum(event_appoint_form)/sum(form_count)*100,3),0) event_appoint_form_ratio,
+round(sum(charge)/sum(event_new_user_credit_grant_page),3) event_new_user_credit_grant_page_cost,
+round(sum(event_new_user_credit_grant_page)/sum(form_count)*100,3) event_new_user_credit_grant_page_roi,
+sum(event_appoint_form) event_appoint_form,
+round(sum(charge)/sum(event_appoint_form),3) event_appoint_form_cost,
+round(sum(event_appoint_form)/sum(form_count)*100,3) event_appoint_form_ratio,
 sum(event_appoint_jump_click) event_appoint_jump_click,
-ifnull(round(sum(charge)/sum(event_appoint_jump_click),3),0)  event_appoint_jump_click_cost,
-ifnull(round(sum(event_appoint_jump_click_cost)/sum(form_count)*100,3),0) event_appoint_jump_click_ratio,
+round(sum(charge)/sum(event_appoint_jump_click),3) event_appoint_jump_click_cost,
+round(sum(event_appoint_jump_click_cost)/sum(form_count)*100,3) event_appoint_jump_click_ratio,
 sum(union_event_pay_purchase_amount_7d) union_event_pay_purchase_amount_7d,
-ifnull(round(sum(union_event_pay_purchase_amount_7d_roi)/{day_count}*100,3),0) union_event_pay_purchase_amount_7d_roi,
+round(sum(union_event_pay_purchase_amount_7d_roi)/{day_count}*100,3) union_event_pay_purchase_amount_7d_roi,
 sum(event_order_successed) event_order_successed,
-ifnull(round(sum(ad_photo_played_10s_ratio)/{day_count}*100,3),0) ad_photo_played_10s_ratio,
-ifnull(round(sum(charge)/sum(key_action),3),0) key_action_cost,
-ifnull(round(sum(charge)/sum(event_add_shopping_cart) ,3),0) event_add_shopping_cart_cost ,
-ifnull(round(sum(event_ad_watch_times)/sum(bclick)*100,3),0) event_ad_watch_times_ratio,
-ifnull(round(sum(charge)/sum(event_outbound_call),3),0) event_outbound_call_cost,
+round(sum(ad_photo_played_10s_ratio)/{day_count}*100,3) ad_photo_played_10s_ratio,
+round(sum(charge)/sum(key_action),3) key_action_cost,
+round(sum(charge)/sum(event_add_shopping_cart),3) event_add_shopping_cart_cost,
+round(sum(event_ad_watch_times)/sum(bclick)*100,3) event_ad_watch_times_ratio,
+round(sum(charge)/sum(event_outbound_call),3) event_outbound_call_cost,
 sum(event_outbound_call) event_outbound_call,
-ifnull(round(sum(action_new_ratio)/{day_count}*100,3),0) action_new_ratio,
+round(sum(action_new_ratio)/{day_count}*100,3) action_new_ratio,
 sum(event_watch_app_ad) event_watch_app_ad,
-ifnull(round(sum(charge)/sum(event_multi_conversion) ,3),0) event_multi_conversion_cost,
+round(sum(charge)/sum(event_multi_conversion),3) event_multi_conversion_cost,
 sum(event_phone_card_activate) event_phone_card_activate,
-ifnull(round(sum(ad_photo_played_75percent_ratio)/{day_count}*100,3),0) ad_photo_played_75percent_ratio,
-ifnull(round(sum(key_action)/sum(activation)*100,3),0) key_action_ratio ,
-ifnull(round(sum(charge)/sum(event_ad_watch_times),3),0) event_ad_watch_times_cost,
+round(sum(ad_photo_played_75percent_ratio)/{day_count}*100,3) ad_photo_played_75percent_ratio,
+round(sum(key_action)/sum(activation)*100,3) key_action_ratio,
+round(sum(charge)/sum(event_ad_watch_times),3) event_ad_watch_times_cost,
 sum(event_add_shopping_cart) event_add_shopping_cart,
 sum(key_action) key_action,
 sum(event_multi_conversion) event_multi_conversion,
@@ -155,21 +292,34 @@ sum(event_wechat_connected) event_wechat_connected,
 sum(event_dsp_gift_form) event_dsp_gift_form,
 sum(event_intention_confirmed) event_intention_confirmed,
 sum(event_phone_get_through) event_phone_get_through,
-ifnull(round(sum(event_multi_conversion)/sum(activation)*100,3),0)  event_multi_conversion_ratio,
+round(sum(event_multi_conversion)/sum(activation)*100,3) event_multi_conversion_ratio,
 sum(event_measurement_house) event_measurement_house,
-ifnull(round(sum(ad_photo_played_2s_ratio)/{day_count}*100,3),0) ad_photo_played_2s_ratio,
-ifnull(round(sum(event_outbound_call_ratio)/sum(activation)*100,3),0) event_outbound_call_ratio,
+round(sum(ad_photo_played_2s_ratio)/{day_count}*100,3) ad_photo_played_2s_ratio,
+round(sum(event_outbound_call_ratio)/sum(activation)*100,3) event_outbound_call_ratio,
 sum(event_ad_watch_times) event_ad_watch_times
- from  
- (select * from  media_api.kuaishou_account_report_daily  
- where stat_date >= date_format(date_sub({date_time}, INTERVAL {day_count} -1  DAY),'%Y%m%d')
- ) report
-LEFT JOIN `jeecg-boot`.ctop_user_allocation cua ON report.advertiser_id = cua.account_id
-LEFT JOIN `jeecg-boot`.ctop_project cp ON cua.project_id = cp.id
-LEFT JOIN `jeecg-boot`.ctop_product cn ON cp.product_id = cn.id
-LEFT JOIN `jeecg-boot`.user_company uc ON uc.user_id = cua.user_id
+from
+(select * from media_api.kuaishou_account_report_daily where stat_date>=date_format(date_sub({date_time},INTERVAL {day_count}-1 DAY),'%Y%m%d')) report
+LEFT JOIN`jeecg-boot`.ctop_user_allocation cua ON report.advertiser_id=cua.account_id
+LEFT JOIN`jeecg-boot`.ctop_project cp ON cua.project_id=cp.id
+LEFT JOIN`jeecg-boot`.ctop_product cn ON cp.product_id=cn.id
+LEFT JOIN`jeecg-boot`.user_company uc ON uc.user_id=cua.user_id
 GROUP BY
-cua.project_id;
+uc.company_id,
+cp.product_id,
+cua.project_id) new
+LEFT JOIN
+(select ifnull(b.project_id,'-') project_id,ifnull(b.product_id,'-') product_id,sum(a.charge) charge from
+(select advertiser_id account_id,sum(charge) charge from media_api.kuaishou_account_report_daily
+where stat_date between date_format(date_sub(date_format(date_sub({date_time},INTERVAL {day_count} DAY),'%Y%m%d'),INTERVAL {day_count}-1 DAY),'%Y%m%d')
+and date_format(date_sub({date_time},INTERVAL {day_count} DAY),'%Y%m%d')
+group by advertiser_id) a
+left join
+(SELECT cua.account_id,cp.product_id,cp.id project_id
+FROM`jeecg-boot`.ctop_user_allocation cua
+LEFT JOIN`jeecg-boot`.ctop_project cp ON cua.project_id=cp.id) b
+on a.account_id=b.account_id
+group by b.project_id) ratio
+on new.project_id=ratio.project_id;
         """.format(date_time=self.date_time, day_count=self.day_count)
             TidbConn.upsert(conn=self.conn, sql_buff=sql)
             self.conn.commit()

+ 8 - 0
Apietl/kwai_etl/Q4Task/AppKuaishouUnitReportBusi.py

@@ -48,6 +48,8 @@ bei.user_id,
 bei.leader_id,
 bei.org_code,
 report.charge,
+ifnull(ratio.charge,0) last_charge,
+ifnull(round((report.charge-ratio.charge)/ratio.charge*100,3),0) charge_ratio,
 report.`show`,
 report.photo_click,
 report.aclick,
@@ -336,6 +338,12 @@ WHERE
 cua.account_id IS NOT NULL
 and cua.account_id ={account_id}) bei 
 ON report.account_id = bei.account_id
+LEFT JOIN
+(select advertiser_id account_id,unit_id,round(sum(charge),3) charge from `media_api`.kuaishou_unit_report_daily a  
+where stat_date between date_format(date_sub(date_format(date_sub({date_time}, INTERVAL {day_count} DAY),'%Y%m%d'), INTERVAL {day_count}-1 DAY),'%Y%m%d') 
+and date_format(date_sub({date_time}, INTERVAL {day_count} DAY),'%Y%m%d')
+group by advertiser_id,unit_id) ratio
+on report.account_id = ratio.account_id and report.unit_id=ratio.unit_id
 ;""".format(date_time=self.date_time, day_count=self.day_count, account_id=account_id[0])
                     print(sql)
                     TidbConn.upsert(conn=self.conn, sql_buff=sql)

+ 12 - 3
Apietl/kwai_etl/Q4Task/AppKuaishouVideoReportBusi.py

@@ -29,7 +29,7 @@ group by account_id """
                     sql = """SET sql_mode = 'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
 REPLACE INTO application.app_kuaishou_video_report_busi_{day_count}d 
 SELECT
-concat(date_format(date_sub({date_time}, INTERVAL (7 -1 )  DAY),'%Y%m%d'),'-',{date_time}) start_end_time,
+concat(date_format(date_sub({date_time}, INTERVAL ({day_count} -1 )  DAY),'%Y%m%d'),'-',{date_time}) start_end_time,
 report.account_id,
 report.photo_id,
 report.image_token,
@@ -62,6 +62,8 @@ CASE WHEN cmi.material_name IS NOT NULL THEN cmi.material_name
 case when video.channel_type = 1 then '素造' else '内部' end as  chl_name, #渠道名称
 up.company_id clip_company_id, #剪辑公司
 report.charge ,
+ifnull(ratio.charge,0) last_charge,
+ifnull(round((report.charge-ratio.charge)/ratio.charge*100,3),0) charge_ratio,
 report.`show`,
 report.photo_click,
 report.aclick ,
@@ -324,7 +326,7 @@ where  stat_date >= date_format(date_sub({date_time}, INTERVAL ({day_count} -1 )
 and advertiser_id={account_id}
 group by 
 advertiser_id,
-photo_id)report
+photo_id) report
 left join media_api.kuaishou_video_list video
 on report.account_id = video.account_id and report.photo_id = video.photo_id 
 LEFT JOIN (select  code,material_name from `jeecg-boot`.ctop_material_info where status=1) cmi ON video.signature = cmi.CODE
@@ -342,7 +344,14 @@ LEFT JOIN `jeecg-boot`.sys_user clip ON clip.id = cma.clip_id
 LEFT JOIN `jeecg-boot`.sys_user shot ON shot.id = cma.shot_id
 LEFT JOIN `jeecg-boot`.sys_user plan ON plan.id = cma.plan_id
 LEFT JOIN `jeecg-boot`.sys_user leader ON leader.id = cma.leader_id
-LEFT JOIN `jeecg-boot`.user_company up ON up.user_id = cma.clip_id where cua.account_id={account_id};
+LEFT JOIN `jeecg-boot`.user_company up ON up.user_id = cma.clip_id 
+LEFT JOIN 
+(select advertiser_id account_id,photo_id,round(sum(charge),3) charge from `media_api`.kuaishou_material_video_report_daily a  
+where stat_date between date_format(date_sub(date_format(date_sub({date_time}, INTERVAL {day_count} DAY),'%Y%m%d'), INTERVAL {day_count}-1 DAY),'%Y%m%d') 
+and date_format(date_sub({date_time}, INTERVAL {day_count} DAY),'%Y%m%d') 
+group by advertiser_id,photo_id) ratio
+on report.account_id=ratio.account_id and report.photo_id=ratio.photo_id
+where cua.account_id={account_id};
 """.format(date_time=self.date_time, day_count=self.day_count, account_id=account_id[0])
                     print(sql)
                     TidbConn.upsert(conn=self.conn, sql_buff=sql)