-- dict_分析视角

WITH T1 AS (
    SELECT '片区' AS 维度
    UNION ALL
    SELECT '业务条线' AS 维度
/*UNION ALL
SELECT '生命周期' AS 维度*/
)
SELECT 维度 FROM T1
WHERE 1=1


-- report_1_按片区得分

SELECT
    organ_name,
    SUM(IFNULL(index_mydf,0)) AS 满意得分
FROM ods_wuye_complaint_half_year
WHERE yr = '${p_yr}'
  AND t_quarter = '${p_jd}'
GROUP BY organ_name


-- report_1_按业务条线得分


SELECT
    business AS 业务条线,
    index_df AS 最终得分
FROM ods_wuye_complaint_business
WHERE yr = '${p_yr}'
  AND jd = '${p_jd}'


-- report_表格1

    WITH T1 AS (/*各片区目标*/
SELECT DISTINCT
	organ_name,
	organ_target
FROM ods_wuye_satisfaction_target
WHERE yr = '${p_yr}'
),T2 AS (/*各片区得分*/
SELECT
	organ_name,
	index_wl_yxybl AS 稳老有效样本量,
	index_wl_myl AS 稳老满意量,
	index_mwl_yxybl AS 磨稳老有效样本量,
	index_mwl_myl AS 磨稳老满意量,
	index_mydf AS 满意得分
FROM ods_wuye_complaint_half_year
WHERE yr = '${p_yr}'
	AND t_quarter = '${p_jd}'
)
SELECT
    T1.organ_name AS 片区名称,
    '${p_jd}' AS 季度,
    T2.满意得分,
    T1.organ_target AS 片区目标,
    T2.磨稳老有效样本量,
    T2.磨稳老满意量
FROM T1 LEFT JOIN T2 ON T1.organ_name = T2.organ_name


-- report_表格2

SELECT
    organ_name AS 片区名称,
    SUM(CASE WHEN business = '安全秩序管理' THEN index_df ELSE 0 END ) AS 安全秩序管理,
    SUM(CASE WHEN business = '保洁服务' THEN index_df ELSE 0 END ) AS 保洁服务,
    SUM(CASE WHEN business = '绿化维护' THEN index_df ELSE 0 END ) AS 绿化维护,
    SUM(CASE WHEN business = '客服服务' THEN index_df ELSE 0 END ) AS 客服服务,
    SUM(CASE WHEN business = '上门维修服务' THEN index_df ELSE 0 END ) AS 上门维修服务
FROM ods_wuye_complaint_business
WHERE yr = '${p_yr}'
  AND jd = '${p_jd}'
GROUP BY organ_name
