dicemy's picture
Upload 655 files
e8c001c verified
|
Raw
History Blame Contribute Delete
3.75 kB
metadata
id: offline-compute_PySpark_pyspark_001
name: 被动客服满意度归因环比对比
category: offline-compute/PySpark
timeout_seconds: 900
modality: pure-text
engine: pyspark

Prompt

我需要你生成一段 PySpark 代码,产出"被动客服会话满意度归因因素"的环比对比表。

业务背景与目标:被动客服每天都会按 reason / reason_detail / session_type 维度产出一份满意度归因明细(dwd_ww_kf_session_satify_attrib)。运营希望对每一条归因因素同时做两个维度的对比:(1) 与 14 天前同一 reason+reason_detail+session_type 的指标做差值;(2) 与上月相同类型日期(工作日 vs 工作日、周末 vs 周末)的平均指标做差值。本任务把当天、14 天前、上月这三组数据拉齐后,逐条计算满意度差值、占比差值、贡献率,输出到下游对比表里供运营复盘。

输入表

  • internal_platform_db.caseR1_dwd_ww_kf_session_satify_attrib_v3

数据范围与过滤条件(业务说法)

  • 运行日期 statdate 固定为 '20260513'(沙箱化日期,原任务由调度框架传入)。
  • 14 天前日期 = statdate - 14 天 = '20260429'。
  • 上月相同类型日期:根据 statdate 的工作日/周末属性,从上一个自然月里取出所有同类型日期组成日期列表(例如 statdate 是周三,则取上月所有工作日);用该列表 IN 过滤主表得到"上月同类型样本"。
  • 三组切片都从同一张明细表过滤得到,分别注册成临时视图 today / 14d / month_avg。

表关联关系

  • left join:当天视图 t0 ⋈ 14 天前视图 t1 on (t0.reason=t1.reason AND t0.reason_detail=t1.reason_detail AND t0.session_type=t1.session_type) → df_compare_14d。
  • left join:当天视图 t0 ⋈ 上月平均视图 t1 on 同上 → df_compare_month。
  • df_compare_14d 与 df_compare_month 通过 union all 拼接成 df_all 一次性写入。

聚合与计算规则(需体现在 SQL/PySpark 中)

  1. 上月平均聚合:对上月切片,先按 session_type 计算月级总量 all_month_session_cnt = sum(all_session_cnt) 和加权满意度 all_month_session_score = sum(all_session_satify_score*all_session_cnt) / sum(all_session_cnt);再按 (session_type, reason, reason_detail) 聚合,得到月度 satify_score = sum(session_cntsatify_score)/sum(session_cnt)、session_rate = sum(session_cnt)/all_month_session_cnt、satify_attrib = (sum(session_cntsatify_score*session_rate/100)/sum(session_cnt))/all_month_session_score - 1。
  2. 派生差值字段(每条 reason+reason_detail+session_type 行同时计算 14 天对比和上月对比两组):
    • all_session_satify_diff = t0.all_session_satify_score - t1.all_session_satify_score
    • satify_score_diff = t0.satify_score - t1.satify_score
    • session_rate_diff = t0.session_rate - t1.session_rate
    • satify_attrib_diff = (t0.satify_scoret0.session_rate - t1.satify_scoret1.session_rate) / 100
    • satify_attrib_rate = satify_attrib_diff / all_session_satify_diff(贡献率)
    • self_satify_attrib_rate = (t0.satify_scoret0.session_rate) / (t1.satify_scoret1.session_rate) - 1(自身环比)
    • compare_satify_attrib_diff = t0.satify_attrib - t1.satify_attrib
  3. compare_interval 字段:14 天对比那批固定写 '14天前';上月对比那批固定写 '上月'。
  4. 输出 imp_date 全部填当天 statdate '20260513'。

输出要求

  • 目标表:internal_platform_db.caseR1_dwd_ww_kf_session_satify_attrib_compare_v3
  • 写入策略:INSERT OVERWRITE TABLE,使用显式列名清单(22 列)避免列序错位。
  • 如果目标表不存在,请先按 Hive 标准建表(ORC 存储),再写入数据。