CREATE TABLE dim_early_warning_organ_d(

    organ_name               VARCHAR(50)   COMMENT '片区名称'
    ,km_comple_rate           DECIMAL(6,4)  COMMENT '客满得分完成率'
    ,ts_close_rate            DECIMAL(6,4)  COMMENT '投诉关闭率'
    ,report_close_rate        DECIMAL(6,4)  COMMENT '报事关闭率'
    ,cu_coll_comple_rate      DECIMAL(6,4)  COMMENT '当期收缴完成率'
    ,pr_coll_comple_rate      DECIMAL(6,4)  COMMENT '往期收缴完成率'
    ,total_coll_comple_rate   DECIMAL(6,4)  COMMENT '综合收缴完成率'
    ,xing_revenue_rate        DECIMAL(6,4)  COMMENT '新零售收入完成率'
    ,space_operate_rate       DECIMAL(6,4)  COMMENT '空间运营收入完成率'
    ,meiju_revenue_rate       DECIMAL(6,4)  COMMENT '美居收入完成率(片区)'
    ,rent_revenue_rate        DECIMAL(6,4)  COMMENT '资产租售收入完成率(片区)'
    ,firth_revenue_rate       DECIMAL(6,4) COMMENT '四大业务收入完成率(片区)'
    ,payment_rate             DECIMAL(6,4)  COMMENT '回款率(片区)'
    ,account_rate             DECIMAL(6,4)  COMMENT '销户率(片区)'
    ,device_space_rate        DECIMAL(6,4)  COMMENT '设备空间巡查完成率'
    ,water_electric_rate      DECIMAL(6,4)  COMMENT '水电费收缴率'
)COMMENT = '片区预警指标数据';

 SELECT * FROM dim_early_warning_organ_d;
INSERT INTO dim_early_warning_organ_d
SELECT
    T.organ_name AS organ_name
     ,SUM(km_comple_rate) AS km_comple_rate -- 客满得分完成率
     ,SUM(ts_close_rate)  AS ts_close_rate -- 投诉关闭率
     ,SUM(report_close_rate) AS report_close_rate -- 报事关闭率
     ,SUM(cu_coll_comple_rate) AS cu_coll_comple_rate -- 当期收缴完成率
     ,SUM(pr_coll_comple_rate) AS pr_coll_comple_rate -- 往期收缴完成率
     ,SUM(total_coll_comple_rate) AS total_coll_comple_rate -- 综合收缴完成率
     ,SUM(xing_revenue_rate) AS xing_revenue_rate -- 新零售收入完成率
     ,SUM(space_operate_rate) AS space_operate_rate -- 空间运营收入完成率
     ,SUM(meiju_revenue_rate) AS meiju_revenue_rate -- 美居收入完成率(片区)
     ,SUM(rent_revenue_rate)   AS rent_revenue_rate -- 资产租售收入完成率(片区)
     ,SUM(firth_revenue_rate) AS  firth_revenue_rate -- 四大业务收入完成率(片区)
     ,SUM(payment_rate) AS  payment_rate   -- 回款率(片区)
     ,SUM(account_rate) AS account_rate -- 销户率(片区)
     ,SUM(device_space_rate) AS device_space_rate -- 设备空间巡查完成率
     ,SUM(water_electric_rate) AS water_electric_rate -- 水电费收缴率
FROM
    (
        SELECT
            T1.organ_name AS organ_name
             ,IF(IFNULL(T1.organ_target,0)=0,0,T2.index_mydf / T1.organ_target)  AS km_comple_rate -- 客满得分完成率
             ,0              AS ts_close_rate -- 投诉关闭率
             ,0              AS report_close_rate -- 报事关闭率
             ,0              AS cu_coll_comple_rate -- 当期收缴完成率
             ,0              AS pr_coll_comple_rate -- 往期收缴完成率
             ,0              AS total_coll_comple_rate -- 综合收缴完成率
             ,0              AS xing_revenue_rate -- 新零售收入完成率
             ,0              AS space_operate_rate -- 空间运营收入完成率
             ,0              AS meiju_revenue_rate -- 美居收入完成率(片区)
             ,0              AS rent_revenue_rate -- 资产租售收入完成率(片区)
             ,0              AS firth_revenue_rate  -- 四大业务收入完成率(片区)
             ,0              AS payment_rate     -- 回款率(片区)
             ,0              AS account_rate -- 销户率(片区)
             ,0              AS device_space_rate -- 设备空间巡查完成率
             ,0              AS water_electric_rate -- 水电费收缴率
        FROM
            (
                SELECT
                    DISTINCT
                    organ_name -- 片区名称
                           ,organ_target -- 目标得分
                FROM ods_wuye_satisfaction_target
                WHERE yr = LEFT(CURRENT_DATE,4)
                  AND organ_target IS NOT NULL
            )T1
                LEFT JOIN
            (
                /*各片区得分*/
                SELECT
                    organ_name
                     ,SUM(IFNULL(index_mydf,0))  AS index_mydf
                FROM ods_wuye_complaint_half_year
                WHERE yr = LEFT(CURRENT_DATE,4)
                  AND t_quarter = '二季度'
                -- AND t_quarter = CASE WHEN QUARTER(CURDATE()) = 1 THEN '一季度'
                --                      WHEN QUARTER(CURDATE()) = 2 THEN '二季度'
                --                      WHEN QUARTER(CURDATE()) = 3 THEN '三季度'
                --                      WHEN QUARTER(CURDATE()) = 4 THEN '四季度' END
                --
                GROUP BY organ_name
            )T2
            ON T1.organ_name = T2.organ_name
        UNION ALL
-- 投诉关闭率/报事关闭率
        SELECT
            organ_name   AS organ_name
             ,0              AS km_comple_rate -- 客满得分完成率
             ,IF(IFNULL(sum(tousu_incident_num),0)=0,0,sum(tousu_close_incident_num) / sum(tousu_incident_num))  AS ts_close_rate -- 投诉关闭率
             ,IF(IFNULL(sum(incident_num),0)=0,0,sum(completed_incident_num - completed_not_close_incident_num) / sum(incident_num)) AS report_close_rate -- 报事关闭率
             ,0              AS cu_coll_comple_rate -- 当期收缴完成率
             ,0              AS pr_coll_comple_rate -- 往期收缴完成率
             ,0              AS total_coll_comple_rate -- 综合收缴完成率
             ,0              AS xing_revenue_rate -- 新零售收入完成率
             ,0              AS space_operate_rate -- 空间运营收入完成率
             ,0              AS meiju_revenue_rate -- 美居收入完成率(片区)
             ,0              AS rent_revenue_rate -- 资产租售收入完成率(片区)
             ,0              AS firth_revenue_rate  -- 四大业务收入完成率(片区)
             ,0              AS payment_rate     -- 回款率(片区)
             ,0              AS account_rate -- 销户率(片区)
             ,0              AS device_space_rate -- 设备空间巡查完成率
             ,0              AS water_electric_rate -- 水电费收缴率
        FROM dws_estate_incident_info_m
        WHERE incident_ym =  date_format(current_date,'%Y%m')
        GROUP BY
            organ_name
        UNION ALL
-- 收缴率
        SELECT
            T1.organ_name  AS organ_name
             ,0              AS km_comple_rate -- 客满得分完成率
             ,0              AS ts_close_rate -- 投诉关闭率
             ,0              AS report_close_rate -- 报事关闭率
             ,SUM(T1.cu_collection_rate) / SUM(T2.index_dq_target) AS cu_coll_comple_rate -- 当期收缴完成率
             ,SUM(T1.pr_collection_rate) / SUM(T2.index_wq_target) AS pr_coll_comple_rate -- 往期收缴完成率
             ,0              AS total_coll_comple_rate -- 综合收缴完成率
             ,0              AS xing_revenue_rate -- 新零售收入完成率
             ,0              AS space_operate_rate -- 空间运营收入完成率
             ,0              AS meiju_revenue_rate -- 美居收入完成率(片区)
             ,0              AS rent_revenue_rate -- 资产租售收入完成率(片区)
             ,0              AS firth_revenue_rate  -- 四大业务收入完成率(片区)
             ,0              AS payment_rate     -- 回款率(片区)
             ,0              AS account_rate -- 销户率(片区)
             ,0              AS device_space_rate -- 设备空间巡查完成率
             ,0              AS water_electric_rate -- 水电费收缴率
        FROM
            (
                SELECT
                    organ_code
                     ,organ_name
                     ,sum(ifnull(pr_paid_cu_amt,0) + ifnull(cu_paid_cu_amt,0) - ifnull(pr_offset_cu_amt,0) - ifnull(cu_offset_cu_amt,0)) / sum(ifnull(cu_receivable_amt,0) + ifnull(pr_exempt_cu_amt,0) + ifnull(cu_exempt_cu_amt,0)) cu_collection_rate
                     ,sum((- ifnull(cu_paid_pr_amt,0) - ifnull(cu_offset_pr_amt,0))) / sum(ifnull(pr_total_receivable_amt,0) + ifnull(pr_total_received_amt,0) + ifnull(cu_early_exempt_pr_amt,0) + ifnull(cu_exempt_pr_amt,0)) pr_collection_rate
                     ,sum(ifnull(pr_paid_cu_amt,0) + ifnull(cu_paid_cu_amt,0) - ifnull(pr_offset_cu_amt,0) - ifnull(cu_offset_cu_amt,0)- ifnull(cu_offset_pr_amt,0) - ifnull(cu_paid_pr_amt,0)) / sum(ifnull(cu_receivable_amt,0) + ifnull(pr_exempt_cu_amt,0) + ifnull(cu_exempt_cu_amt,0) + ifnull(pr_total_receivable_amt,0) + ifnull(pr_total_received_amt,0) + ifnull(cu_early_exempt_pr_amt,0) + ifnull(cu_exempt_pr_amt,0)) total_collection_rate
                from dwd_finance_fees_serial_d
                where ParentCostCode = '0001'
                  and ym = left(current_date,7)
                  and organ_name not in ('东湖商管','领悦总部','演示机构')
                  and comm_name not like '%天富%'
                  and comm_name not in('【喀什】融合国际新城','【贵阳】鹏淇·宏桥公园（撤场）')
                GROUP BY
                    organ_code
                       ,organ_name
            )T1
                LEFT JOIN
            (
                select
                    organ_code
                     ,index_dq_target
                     ,index_wq_target
                from ods_finance_collection_target_area
                WHERE ym = left(current_date,7)
            )T2
            ON T1.organ_code = T2.organ_code
        GROUP BY
            T1.organ_name
        UNION ALL
-- 综合收缴率
        SELECT
            T1.organ_name  AS organ_name
             ,0              AS km_comple_rate -- 客满得分完成率
             ,0              AS ts_close_rate -- 投诉关闭率
             ,0              AS report_close_rate -- 报事关闭率
             ,0              AS cu_coll_comple_rate -- 当期收缴完成率
             ,0              AS pr_coll_comple_rate -- 往期收缴完成率
             ,SUM(T1.index_receipts) / SUM(T2.index_dj_target)             AS total_coll_comple_rate -- 综合收缴完成率
             ,0              AS xing_revenue_rate -- 新零售收入完成率
             ,0              AS space_operate_rate -- 空间运营收入完成率
             ,0              AS meiju_revenue_rate -- 美居收入完成率(片区)
             ,0              AS rent_revenue_rate -- 资产租售收入完成率(片区)
             ,0              AS firth_revenue_rate  -- 四大业务收入完成率(片区)
             ,0              AS payment_rate     -- 回款率(片区)
             ,0              AS account_rate -- 销户率(片区)
             ,0              AS device_space_rate -- 设备空间巡查完成率
             ,0              AS water_electric_rate -- 水电费收缴率
        FROM
            (
                SELECT
                    organ_code
                     ,organ_name
                     ,SUM(index_receivable) /  SUM(index_receipts) AS index_receipts
                FROM ods_caiwu_profit
                -- where `year_month` = date_format(current_date,'%Y%m')
                where `year_month` =  '202408'
                  and comm_name not in('【喀什】融合国际新城','【贵阳】鹏淇·宏桥公园（撤场）')
                GROUP BY
                    organ_code
                       ,organ_name
            )T1
                LEFT JOIN
            (
                select
                    organ_code
                     ,index_dj_target
                from ods_finance_collection_target_area
                WHERE ym = left(current_date,7)
            )T2
            ON T1.organ_code = T2.organ_code
        GROUP BY  T1.organ_name
-- 收入完成率(片区)
        UNION ALL
        SELECT
            organ_name  AS organ_name
             ,0              AS km_comple_rate -- 客满得分完成率
             ,0              AS ts_close_rate -- 投诉关闭率
             ,0              AS report_close_rate -- 报事关闭率
             ,0              AS cu_coll_comple_rate -- 当期收缴完成率
             ,0              AS pr_coll_comple_rate -- 往期收缴完成率
             ,0              AS total_coll_comple_rate -- 综合收缴完成率
             ,IFNULL(CASE WHEN SUM(CASE WHEN revenue_type = '创新' AND business_type = '目标' THEN COALESCE(revenue_value,0) ELSE 0 END) = 0 THEN 0
                          ELSE SUM(CASE WHEN revenue_type = '创新' AND business_type = '收入' THEN COALESCE(revenue_value,0) ELSE 0 END)
                              / SUM(CASE WHEN revenue_type = '创新' AND business_type = '目标' THEN COALESCE(revenue_value,0) ELSE 0 END)
                         END,0)               AS xing_revenue_rate -- 新零售收入完成率
             ,IFNULL(CASE WHEN SUM(CASE WHEN revenue_type = '附属' AND business_type = '目标' THEN COALESCE(revenue_value,0) ELSE 0 END) = 0 THEN 0
                          ELSE SUM(CASE WHEN revenue_type = '附属' AND business_type = '收入' THEN COALESCE(revenue_value,0) ELSE 0 END)
                              / SUM(CASE WHEN revenue_type = '附属' AND business_type = '目标' THEN COALESCE(revenue_value,0) ELSE 0 END)
                         END,0)    AS space_operate_rate -- 空间运营收入完成率
             ,IFNULL(CASE WHEN SUM(CASE WHEN revenue_type = '拎包' AND business_type = '目标' THEN COALESCE(revenue_value,0) ELSE 0 END) = 0 THEN 0
                          ELSE SUM(CASE WHEN revenue_type = '拎包' AND business_type = '收入' THEN COALESCE(revenue_value,0) ELSE 0 END)
                              / SUM(CASE WHEN revenue_type = '拎包' AND business_type = '目标' THEN COALESCE(revenue_value,0) ELSE 0 END)
                         END,0)              AS meiju_revenue_rate -- 美居收入完成率(片区)
             ,IFNULL(CASE WHEN SUM(CASE WHEN revenue_type = '资产' AND business_type = '目标' THEN COALESCE(revenue_value,0) ELSE 0 END)  = 0 THEN 0
                          ELSE SUM(CASE WHEN revenue_type = '资产' AND business_type = '收入' THEN COALESCE(revenue_value,0) ELSE 0 END)
                              / SUM(CASE WHEN revenue_type = '资产' AND business_type = '目标' THEN COALESCE(revenue_value,0) ELSE 0 END)
                         END,0)              AS rent_revenue_rate -- 资产租售收入完成率(片区)
             ,IFNULL(CASE WHEN SUM(CASE WHEN business_type = '目标' THEN COALESCE(revenue_value,0) ELSE 0 END) = 0 THEN 0
                          ELSE SUM(CASE WHEN business_type = '收入' THEN COALESCE(revenue_value,0) ELSE 0 END)
                              / SUM(CASE WHEN business_type = '目标' THEN COALESCE(revenue_value,0) ELSE 0 END)
                         END,0)               AS firth_revenue_rate  -- 四大业务收入完成率(片区)
             ,0              AS payment_rate     -- 回款率(片区)
             ,0              AS account_rate -- 销户率(片区)
             ,0              AS device_space_rate -- 设备空间巡查完成率
             ,0              AS water_electric_rate -- 水电费收缴率
        FROM  dw.ods_innoveco_metrics_finish_d
        WHERE business_type in('收入','目标') -- 业务类型
          AND YM = LEFT(current_date,7)
        GROUP BY
            organ_name
        UNION ALL
-- 回款率/销户率
        SELECT
            organ_name   AS organ_name
             ,0              AS km_comple_rate -- 客满得分完成率
             ,0              AS ts_close_rate -- 投诉关闭率
             ,0              AS report_close_rate -- 报事关闭率
             ,0              AS cu_coll_comple_rate -- 当期收缴完成率
             ,0              AS pr_coll_comple_rate -- 往期收缴完成率
             ,0              AS total_coll_comple_rate -- 综合收缴完成率
             ,0              AS xing_revenue_rate -- 新零售收入完成率
             ,0              AS space_operate_rate -- 空间运营收入完成率
             ,0              AS meiju_revenue_rate -- 美居收入完成率(片区)
             ,0              AS rent_revenue_rate -- 资产租售收入完成率(片区)
             ,0              AS firth_revenue_rate  -- 四大业务收入完成率(片区)
             ,SUM(IFNULL(payment_coll_amt,0)) / SUM(IFNULL(trans_amt,0)) AS payment_rate     -- 回款率(片区)
             ,SUM(IFNULL(payment_coll_num,0)) / SUM(IFNULL(trans_household_num,0)) AS account_rate -- 销户率(片区)
             ,0              AS device_space_rate -- 设备空间巡查完成率
             ,0              AS water_electric_rate -- 水电费收缴率
        FROM dws_law_tran_coll_d
        WHERE ymd <= CURRENT_DATE()
        GROUP BY
            organ_name
        UNION ALL
-- 设备空间巡查完成率
        SELECT
            organ_name     AS organ_name
             ,0              AS km_comple_rate -- 客满得分完成率
             ,0              AS ts_close_rate -- 投诉关闭率
             ,0              AS report_close_rate -- 报事关闭率
             ,0              AS cu_coll_comple_rate -- 当期收缴完成率
             ,0              AS pr_coll_comple_rate -- 往期收缴完成率
             ,0              AS total_coll_comple_rate -- 综合收缴完成率
             ,0              AS xing_revenue_rate -- 新零售收入完成率
             ,0              AS space_operate_rate -- 空间运营收入完成率
             ,0              AS meiju_revenue_rate -- 美居收入完成率(片区)
             ,0              AS rent_revenue_rate -- 资产租售收入完成率(片区)
             ,0              AS firth_revenue_rate  -- 四大业务收入完成率(片区)
             ,0              AS payment_rate     -- 回款率(片区)
             ,0               AS account_rate -- 销户率(片区)
             ,SUM(task_finish_num) / SUM(task_num) AS device_space_rate -- 设备空间巡查完成率
             ,0              AS water_electric_rate -- 水电费收缴率
        FROM dws_engine_equipment_inspect_task_m
        where task_ym = date_format(current_date,'%Y%m')
        GROUP BY
            organ_name
        UNION ALL
-- 水电费收缴率
        SELECT
            organ_name     AS organ_name
             ,0              AS km_comple_rate -- 客满得分完成率
             ,0              AS ts_close_rate -- 投诉关闭率
             ,0              AS report_close_rate -- 报事关闭率
             ,0              AS cu_coll_comple_rate -- 当期收缴完成率
             ,0              AS pr_coll_comple_rate -- 往期收缴完成率
             ,0              AS total_coll_comple_rate -- 综合收缴完成率
             ,0              AS xing_revenue_rate -- 新零售收入完成率
             ,0              AS space_operate_rate -- 空间运营收入完成率
             ,0              AS meiju_revenue_rate -- 美居收入完成率(片区)
             ,0              AS rent_revenue_rate -- 资产租售收入完成率(片区)
             ,0              AS firth_revenue_rate  -- 四大业务收入完成率(片区)
             ,0              AS payment_rate     -- 回款率(片区)
             ,0              AS account_rate -- 销户率(片区)
             ,0              AS device_space_rate -- 设备空间巡查完成率
             ,sum((ifnull(pr_paid_cu_amt,0) + ifnull(cu_paid_cu_amt,0) - ifnull(pr_offset_cu_amt,0) - ifnull(cu_offset_cu_amt,0))+(- ifnull(cu_paid_pr_amt,0) - ifnull(cu_offset_pr_amt,0))) / sum((ifnull(cu_receivable_amt,0) + ifnull(pr_exempt_cu_amt,0) + ifnull(cu_exempt_cu_amt,0))+(ifnull(pr_total_receivable_amt,0) + ifnull(pr_total_received_amt,0) + ifnull(cu_early_exempt_pr_amt,0) + ifnull(cu_exempt_pr_amt,0)))  AS water_electric_rate -- 水电费收缴率
        FROM dwd_finance_fees_serial_d
        WHERE ParentCostCode = '0008'
          AND MiddleCostCode in ('00080001','00080002','00080003','00080004','00080005','00080006','00080007','00080008','00080017','00080018','00080019','00080022','00080023','00080024','00080027','00080028','00080032','00080035')
          AND ym = LEFT(current_date,7)
        GROUP BY organ_name
    )T
JOIN (SELECT organ_name FROM dim_organ_mapping GROUP BY organ_name)t1 ON T.organ_name = t1.organ_name
GROUP BY T.organ_name
;