-- 【月报事率】
select
    incident_ym
     , sum(response_timely_incident_num) / sum(incident_num) response_timely_rate
     , sum(completed_incident_num) / sum(incident_num) completed_rate
     , sum(completed_incident_num - completed_not_close_incident_num) / sum(incident_num) close_rate
     , sum(follow_up_incident_num) / sum(incident_num) follow_up_rate
     , sum(delay_incident_num) / sum(incident_num) delay_rate
     , sum(delay_incident_num) delay_incident_num
     , sum(follow_up_satisfied_incident_num) / sum(follow_up_incident_num) follow_up_satisfied_rate
     , sum(follow_up_evaluate_incident_num) / sum(follow_up_incident_num) follow_up_satisfied_rate1 --

     -- 回访满意量 / -- 回访量
     , sum(tousu_close_incident_num) / sum(tousu_incident_num) tousu_close_rate
     , sum(tousu_incident_num) tousu_incident_num
FROM dws_estate_incident_info_m
where left(incident_ym,4) = '${left(p_ym,4)}'
  AND incident_ym <> DATE_FORMAT(CURDATE(), '%Y%m')
    ${IF(LEN(p_area)>0," AND organ_code IN ('"+JOINARRAY(p_area,"','")+"')", "")}
    ${if(left(fine_role,2)=="项目","and comm_name = '"+GETUSERDEPARTMENTS(3)+"'","")}
group by incident_ym
order by incident_ym



follow_up_satisfied_rate, EXACT(
                incident_ym,
                CONCATENATE(
                                year(TODAY()),  -- 当前年
                                if(
                                        LEN(month(today())-1) = 1, --
                                        CONCATENATE("0",month(today())-1),
                                        month(today())-1
                                  )
                )
             )

EXACT(text1,text2):检测两组文本是否相同。如果完全相同，EXACT函数返回TRUE；否则，返回FALSE。EXACT函数可以区分大小写，但忽略格式的不同。同时也可以利用EXACT函数来检测输入文档的文字。
Text1:需要比较的第一组文本。
Text2:需要比较的第二组文本。
示例:
EXACT("Spreadsheet","Spreadsheet")等于TRUE。
EXACT("Spreadsheet","S preadsheet")等于FALSE。
EXACT("Spreadsheet","spreadsheet")等于FALSE。

-- 【客满得分汇总】

select
    yr,
    t_quarter,
    organ_code,
    organ_name,
    index_myd_target,
    index_mydf,
    index_mydf / index_myd_target myd_complete_rate
select * from ods_wuye_complaint_half_year
where 1=1 and yr = '2024' and t_quarter = '二季度'
${if(len(organ)=0,"and organ_code = '0000'","and organ_code = '"+organ+"'")}





-- 【report_各片区得分】

    WITH T1 AS (/*各片区得分目标*/
SELECT DISTINCT
	organ_code,
	organ_name,
	organ_target
FROM ods_wuye_satisfaction_target
WHERE yr = '${LEFT(p_ym,4)}'
	AND organ_target > 0
),T2 AS (/*各片区得分情况*/
SELECT DISTINCT
	organ_code,
	organ_name,
	index_mydf,
	IFNULL(index_wl_yxybl,0) + IFNULL(index_mwl_yxybl,0) AS 样本量,
	IFNULL(index_wl_myl,0) + IFNULL(index_mwl_myl,0) AS 满意量
FROM ods_wuye_complaint_half_year
WHERE yr = '${SWITCH(MONTH(CONCATENATE(p_ym,"-01")),"1",LEFT(p_ym,4)-1,"2",LEFT(p_ym,4)-1,"3",LEFT(p_ym,4)-1,"4",LEFT(p_ym,4),"5",LEFT(p_ym,4),"6",LEFT(p_ym,4),"7",LEFT(p_ym,4),"8",LEFT(p_ym,4),"9",LEFT(p_ym,4),"10",LEFT(p_ym,4),"11",LEFT(p_ym,4),"12",LEFT(p_ym,4))}'
	AND t_quarter = '${SWITCH(MONTH(CONCATENATE(p_ym,"-01")),"1","四季度","2","四季度","3","四季度","4","一季度","5","一季度","6","一季度","7","二季度","8","二季度","9","二季度","10","三季度","11","三季度","12","三季度")}'
)
SELECT
    ROW_NUMBER() OVER (ORDER BY T2.index_mydf DESC) AS 序号,
    T1.organ_code,
    T1.organ_name,
    T1.organ_target AS 目标,
    T2.index_mydf AS 满意度得分,
    T2.样本量,
    T2.满意量
FROM T1 LEFT JOIN T2 ON T1.organ_code = T2.organ_code
WHERE T1.organ_target IS NOT NULL
    ${if(left(fine_role,6)!="领悦集团总部","and T1.organ_name = '"+GETUSERDEPARTMENTS(2)+"'","")}
ORDER BY 满意度得分 DESC
;


-- 不选片区展示总得分
${IF(OR(LEN(p_area)>0,left(fine_role,2)=="项目"),"/*", "")}
select
    ifnull(index_mydf,'-') as index_mydf
     ,IFNULL((index_mydf / index_myd_target),'-') as myd_complete_rate
from ods_wuye_complaint_half_year
where 1=1
and  yr = '${SWITCH(MONTH(CONCATENATE(p_ym,"-01")),"1",LEFT(p_ym,4)-1,"2",LEFT(p_ym,4)-1,"3",LEFT(p_ym,4)-1,"4",LEFT(p_ym,4),"5",LEFT(p_ym,4),"6",LEFT(p_ym,4),"7",LEFT(p_ym,4),"8",LEFT(p_ym,4),"9",LEFT(p_ym,4),"10",LEFT(p_ym,4),"11",LEFT(p_ym,4),"12",LEFT(p_ym,4))}'
AND t_quarter = '${SWITCH(MONTH(CONCATENATE(p_ym,"-01")),"1","四季度","2","四季度","3","四季度","4","一季度","5","一季度","6","一季度","7","二季度","8","二季度","9","二季度","10","三季度","11","三季度","12","三季度")}'
and organ_code = '0000'
${IF(OR(LEN(p_area)>0,left(fine_role,2)=="项目"),"*/", "")}

-- 选择片区显示片区得分 选择多个片区只会返回得分最高的片区
${IF(AND(LEN(p_area)>0,left(fine_role,2)!="项目"),"", "/*")}
select
    ifnull(index_mydf,'-') as index_mydf
     ,IFNULL((index_mydf / index_myd_target),'-') as myd_complete_rate
from ods_wuye_complaint_half_year
where 1=1
and  yr = '${SWITCH(MONTH(CONCATENATE(p_ym,"-01")),"1",LEFT(p_ym,4)-1,"2",LEFT(p_ym,4)-1,"3",LEFT(p_ym,4)-1,"4",LEFT(p_ym,4),"5",LEFT(p_ym,4),"6",LEFT(p_ym,4),"7",LEFT(p_ym,4),"8",LEFT(p_ym,4),"9",LEFT(p_ym,4),"10",LEFT(p_ym,4),"11",LEFT(p_ym,4),"12",LEFT(p_ym,4))}'
AND t_quarter = '${SWITCH(MONTH(CONCATENATE(p_ym,"-01")),"1","四季度","2","四季度","3","四季度","4","一季度","5","一季度","6","一季度","7","二季度","8","二季度","9","二季度","10","三季度","11","三季度","12","三季度")}'
${IF(LEN(p_area)>0,"AND organ_code IN('"+JOINARRAY(p_area,"','")+"')", "")}  -- 如果选择的片区 筛选片区的数据
${if(left(fine_role,6)!="领悦集团总部","and organ_name = '"+GETUSERDEPARTMENTS(2)+"'","")}
ORDER BY index_mydf desc
${IF(AND(LEN(p_area)>0,left(fine_role,2)!="项目"),"", "*/")}
-- 如果是项目负责人 需显示项目得分情况
${IF(left(fine_role,2)=="项目","", "/*")}
WITH T3 AS (
    -- 各项目目标
    SELECT
    comm_name
    ,comm_id
    ,comm_target --  目标
    FROM ods_wuye_satisfaction_target  -- 年度维护目标表  包含片区项目的目标值
    WHERE yr = '${LEFT(p_ym,4)}'
    AND comm_type = '内盘'
    AND comm_name NOT LIKE '%案场%'
    AND comm_name NOT LIKE '%撤场%'
    AND comm_name NOT LIKE '%禁用%'
    AND comm_target IS NOT NULL
    ${IF(LEN(p_area)>0," AND organ_code IN ('"+JOINARRAY(p_area,"','")+"')", "")}  -- 如果选择的片区 筛选片区的数据
    ${if(left(fine_role,2)=="项目","and comm_name = '"+GETUSERDEPARTMENTS(3)+"'","")}
),
T4 AS (
    -- 各项目得分
    SELECT
     comm_name
    ,comm_id
    ,index_dc_myddf -- 得分
    FROM ods_wuye_complaint
    WHERE yr = '${SWITCH(MONTH(CONCATENATE(p_ym,"-01")),"1",LEFT(p_ym,4)-1,"2",LEFT(p_ym,4)-1,"3",LEFT(p_ym,4)-1,"4",LEFT(p_ym,4),"5",LEFT(p_ym,4),"6",LEFT(p_ym,4),"7",LEFT(p_ym,4),"8",LEFT(p_ym,4),"9",LEFT(p_ym,4),"10",LEFT(p_ym,4),"11",LEFT(p_ym,4),"12",LEFT(p_ym,4))}'
    AND jd = '${SWITCH(MONTH(CONCATENATE(p_ym,"-01")),"1","四季度","2","四季度","3","四季度","4","一季度","5","一季度","6","一季度","7","二季度","8","二季度","9","二季度","10","三季度","11","三季度","12","三季度")}'
    ${if(left(fine_role,2)=="项目","and comm_name = '"+GETUSERDEPARTMENTS(3)+"'","")}
)
SELECT
IFNULL(T4.index_dc_myddf,'-') as index_dc_myddf -- 得分
,IFNULL((T4.index_dc_myddf / T3.comm_target),'-') AS  index_dc_myddf_rate -- 得分完成率
FROM T3
LEFT JOIN T4
ON T3.comm_id = T4.comm_id
ORDER BY T4.index_dc_myddf DESC
${IF(left(fine_role,2)=="项目","", "*/")}



-- 【片区项目投诉】



SELECT
    ROW_NUMBER() OVER (ORDER BY tousu_incident_num ASC) AS 序号
    -- ,a.p_area as p_area
    ,REPLACE(REPLACE(a.p_area,'片区', ''),'公司','') AS p_area
    ,a.incident_ym as incident_ym
    ,a.tousu_close_incident_num as tousu_close_incident_num
from
(
        select
            ${if(len(p_area) == 0,"organ_name","comm_name")} as p_area
             , incident_ym
             , SUM(IFNULL(tousu_incident_num,0)) AS tousu_incident_num
             , SUM(IFNULL(tousu_close_incident_num,0)) AS tousu_close_incident_num
        FROM dws_estate_incident_info_m
        where incident_ym = '${REPLACE(p_ym,"-","")}'
            ${if(left(fine_role,2)=="项目","and comm_name = '"+GETUSERDEPARTMENTS(3)+"'","")}
            ${IF(LEN(p_area)>0," and organ_code IN ('"+JOINARRAY(p_area,"','")+"')", "")}
        GROUP BY ${if(len(p_area) == 0,"organ_name","comm_name")},incident_ym
        having SUM(IFNULL(tousu_incident_num,0)) != 0
)a
ORDER BY tousu_incident_num ASC

-- 【片区项目投诉_倒数】



select
    t.*
from
    (
        SELECT
            ROW_NUMBER() OVER (ORDER BY tousu_incident_num ASC) AS rk
             -- ,a.p_area as p_area
             ,REPLACE(REPLACE(a.p_area,'片区', ''),'公司','') AS p_area
             ,a.incident_ym as incident_ym
             ,a.tousu_close_incident_num as tousu_close_incident_num
        from(
                select
                    ${if(len(p_area) == 0,"organ_name","comm_name")} as p_area
                     , incident_ym
                     , SUM(IFNULL(tousu_incident_num,0)) AS tousu_incident_num
                     , SUM(IFNULL(tousu_close_incident_num,0)) AS tousu_close_incident_num
                FROM dws_estate_incident_info_m
                where incident_ym = '${REPLACE(p_ym,"-","")}'
                    ${if(left(fine_role,2)=="项目","and comm_name = '"+GETUSERDEPARTMENTS(3)+"'","")}
                    ${IF(LEN(p_area)>0," and organ_code IN ('"+JOINARRAY(p_area,"','")+"')", "")}
                GROUP BY ${if(len(p_area) == 0,"organ_name","comm_name")},incident_ym
                having SUM(IFNULL(tousu_incident_num,0)) != 0
            ) a
        ORDER BY tousu_incident_num DESC
        limit 3
    )t
order by rk


SELECT
REPLACE(REPLACE(t.p_area,'片区', ''),'公司','') AS p_area
,t.户数 AS 户数
FROM
(
    SELECT
    ${if(len(p_area) == 0,"T1.organ_name","T1.comm_name")} as p_area
    ,count(T2.RoomName) AS 户数
    FROM dim_organ_mapping T1
    INNER JOIN dim_room_d T2
    ON T1.comm_id = T2.CommID
    ${IF(LEN(p_area)>0," where organ_code IN ('"+JOINARRAY(p_area,"','")+"')", "")}
    ${if(left(fine_role,2)=="项目","and comm_name = '"+GETUSERDEPARTMENTS(3)+"'","")}
    GROUP BY ${if(len(p_area) == 0,"T1.organ_name","T1.comm_name")}
)t


-- 【report_当年客满得分情况_按月】




/*有片区求各项目的内外盘得分、目标*/
/*无片区求各片区的得分、目标*/
WITH T1 AS (
    /*各片区得分目标*/
    SELECT
    DISTINCT
    organ_name AS 维度,
    organ_target AS 目标,
    (SELECT
DISTINCT
km_target
FROM ods_wuye_gszt
WHERE km_target IS NOT NULL
AND yr = '${LEFT(p_ym,4)}') AS total_target, -- 公司整体目标得分
    yr          AS yr, -- 年
    '1月'           AS m	-- 月
    FROM ods_wuye_satisfaction_target
    WHERE yr = '${LEFT(p_ym,4)}'
    AND organ_target IS NOT NULL
    ${IF(LEN(p_area)>0," AND 1=2", "")}
    UNION ALL
    SELECT
    DISTINCT
    organ_name AS 维度,
    organ_target AS 目标,
   (SELECT
    DISTINCT
    km_target
    FROM ods_wuye_gszt
    WHERE km_target IS NOT NULL
    AND yr = '${LEFT(p_ym,4)}') AS total_target, -- 公司整体目标得分
    yr          AS yr, -- 年
    '2月'           AS m	-- 月
    FROM ods_wuye_satisfaction_target
    WHERE yr = '${LEFT(p_ym,4)}'
    AND organ_target IS NOT NULL
    ${IF(LEN(p_area)>0," AND 1=2", "")}
    UNION ALL
    SELECT
    DISTINCT
    organ_name AS 维度,
    organ_target AS 目标,
   (SELECT
    DISTINCT
    km_target
    FROM ods_wuye_gszt
    WHERE km_target IS NOT NULL
    AND yr = '${LEFT(p_ym,4)}') AS total_target, -- 公司整体目标得分
    yr          AS yr, -- 年
    '3月'           AS m	-- 月
    FROM ods_wuye_satisfaction_target
    WHERE yr = '${LEFT(p_ym,4)}'
    AND organ_target IS NOT NULL
    ${IF(LEN(p_area)>0," AND 1=2", "")}
    UNION ALL
    SELECT
    DISTINCT
    organ_name AS 维度,
    organ_target AS 目标,
   (SELECT
    DISTINCT
    km_target
    FROM ods_wuye_gszt
    WHERE km_target IS NOT NULL
    AND yr = '${LEFT(p_ym,4)}') AS total_target, -- 公司整体目标得分
    yr          AS yr, -- 年
    '4月'           AS m	-- 月
    FROM ods_wuye_satisfaction_target
    WHERE yr = '${LEFT(p_ym,4)}'
    AND organ_target IS NOT NULL
    ${IF(LEN(p_area)>0," AND 1=2", "")}
    UNION ALL
    SELECT
    DISTINCT
    organ_name AS 维度,
    organ_target AS 目标,
   (SELECT
    DISTINCT
    km_target
    FROM ods_wuye_gszt
    WHERE km_target IS NOT NULL
    AND yr = '${LEFT(p_ym,4)}') AS total_target, -- 公司整体目标得分
    yr          AS yr, -- 年
    '5月'           AS m	-- 月
    FROM ods_wuye_satisfaction_target
    WHERE yr = '${LEFT(p_ym,4)}'
    AND organ_target IS NOT NULL
    ${IF(LEN(p_area)>0," AND 1=2", "")}
    UNION ALL
    SELECT
    DISTINCT
    organ_name AS 维度,
    organ_target AS 目标,
   (SELECT
    DISTINCT
    km_target
    FROM ods_wuye_gszt
    WHERE km_target IS NOT NULL
    AND yr = '${LEFT(p_ym,4)}') AS total_target, -- 公司整体目标得分
    yr          AS yr, -- 年
    '6月'           AS m	-- 月
    FROM ods_wuye_satisfaction_target
    WHERE yr = '${LEFT(p_ym,4)}'
    AND organ_target IS NOT NULL
    ${IF(LEN(p_area)>0," AND 1=2", "")}
    UNION ALL
    SELECT
    DISTINCT
    organ_name AS 维度,
    organ_target AS 目标,
   (SELECT
    DISTINCT
    km_target
    FROM ods_wuye_gszt
    WHERE km_target IS NOT NULL
    AND yr = '${LEFT(p_ym,4)}') AS total_target, -- 公司整体目标得分
    yr          AS yr, -- 年
    '7月'           AS m	-- 月
    FROM ods_wuye_satisfaction_target
    WHERE yr = '${LEFT(p_ym,4)}'
    AND organ_target IS NOT NULL
    ${IF(LEN(p_area)>0," AND 1=2", "")}
    UNION ALL
    SELECT
    DISTINCT
    organ_name AS 维度,
    organ_target AS 目标,
   (SELECT
    DISTINCT
    km_target
    FROM ods_wuye_gszt
    WHERE km_target IS NOT NULL
    AND yr = '${LEFT(p_ym,4)}') AS total_target, -- 公司整体目标得分
    yr          AS yr, -- 年
    '8月'           AS m	-- 月
    FROM ods_wuye_satisfaction_target
    WHERE yr = '${LEFT(p_ym,4)}'
    AND organ_target IS NOT NULL
    ${IF(LEN(p_area)>0," AND 1=2", "")}
    UNION ALL
    SELECT
    DISTINCT
    organ_name AS 维度,
    organ_target AS 目标,
   (SELECT
    DISTINCT
    km_target
    FROM ods_wuye_gszt
    WHERE km_target IS NOT NULL
    AND yr = '${LEFT(p_ym,4)}') AS total_target, -- 公司整体目标得分
    yr          AS yr, -- 年
    '9月'           AS m	-- 月
    FROM ods_wuye_satisfaction_target
    WHERE yr = '${LEFT(p_ym,4)}'
    AND organ_target IS NOT NULL
    ${IF(LEN(p_area)>0," AND 1=2", "")}
    UNION ALL
    SELECT
    DISTINCT
    organ_name AS 维度,
    organ_target AS 目标,
   (SELECT
    DISTINCT
    km_target
    FROM ods_wuye_gszt
    WHERE km_target IS NOT NULL
    AND yr = '${LEFT(p_ym,4)}') AS total_target, -- 公司整体目标得分
    yr          AS yr, -- 年
    '10月'           AS m	-- 月
    FROM ods_wuye_satisfaction_target
    WHERE yr = '${LEFT(p_ym,4)}'
    AND organ_target IS NOT NULL
    ${IF(LEN(p_area)>0," AND 1=2", "")}
    UNION ALL
    SELECT
    DISTINCT
    organ_name AS 维度,
    organ_target AS 目标,
   (SELECT
    DISTINCT
    km_target
    FROM ods_wuye_gszt
    WHERE km_target IS NOT NULL
    AND yr = '${LEFT(p_ym,4)}') AS total_target, -- 公司整体目标得分
    yr          AS yr, -- 年
    '11月'           AS m	-- 月
    FROM ods_wuye_satisfaction_target
    WHERE yr = '${LEFT(p_ym,4)}'
    AND organ_target IS NOT NULL
    ${IF(LEN(p_area)>0," AND 1=2", "")}
    UNION ALL
    SELECT
    DISTINCT
    organ_name AS 维度,
    organ_target AS 目标,
   (SELECT
    DISTINCT
    km_target
    FROM ods_wuye_gszt
    WHERE km_target IS NOT NULL
    AND yr = '${LEFT(p_ym,4)}') AS total_target, -- 公司整体目标得分
    yr          AS yr, -- 年
    '12月'           AS m	-- 月
    FROM ods_wuye_satisfaction_target
    WHERE yr = '${LEFT(p_ym,4)}'
    AND organ_target IS NOT NULL
    ${IF(LEN(p_area)>0," AND 1=2", "")}

    ),T2 AS (
    /*各片区得分*/
    SELECT
    DISTINCT
    organ_name AS 维度,
    SUM(index_wl_yxybl) AS  wl_yxybl, -- 稳老有效样本量
    SUM(index_wl_myl) AS  wl_myl,  -- 稳老满意量
    index_wl_qz as wl_qz, -- 稳老权重
    SUM(index_mwl_yxybl) AS  mwl_yxybl, -- 磨稳老有效样本量
    SUM(index_mwl_myl) AS  mwl_myl, -- 磨稳老满意量
    index_mwl_qz as mwl_qz, -- 磨稳老权重
    SUM(IFNULL(index_mydf,0)) AS 得分,
    '1月'           AS m	-- 月
    FROM ods_wuye_complaint_half_year
    WHERE t_quarter = '一季度'
    AND  yr = '${LEFT(p_ym,4)}'
-- AND yr = '${SWITCH(MONTH(CONCATENATE(p_ym,"-01")),"1",LEFT(p_ym,4)-1,"2",LEFT(p_ym,4)-1,"3",LEFT(p_ym,4)-1,"4",LEFT(p_ym,4),"5",LEFT(p_ym,4),"6",LEFT(p_ym,4),"7",LEFT(p_ym,4),"8",LEFT(p_ym,4),"9",LEFT(p_ym,4),"10",LEFT(p_ym,4),"11",LEFT(p_ym,4),"12",LEFT(p_ym,4))}'
   -- AND t_quarter = '${SWITCH(MONTH(CONCATENATE(p_ym,"-01")),"1","四季度","2","四季度","3","四季度","4","一季度","5","一季度","6","一季度","7","二季度","8","二季度","9","二季度","10","三季度","11","三季度","12","三季度")}'
    ${IF(LEN(p_area)>0," AND 1=2", "")}
    GROUP BY organ_name,index_wl_qz,index_mwl_qz
    UNION ALL
    SELECT
    DISTINCT
    organ_name AS 维度,
    SUM(index_wl_yxybl) AS  wl_yxybl, -- 稳老有效样本量
    SUM(index_wl_myl) AS  wl_myl,  -- 稳老满意量
    index_wl_qz as wl_qz, -- 稳老权重
    SUM(index_mwl_yxybl) AS  mwl_yxybl, -- 磨稳老有效样本量
    SUM(index_mwl_myl) AS  mwl_myl, -- 磨稳老满意量
    index_mwl_qz as mwl_qz, -- 磨稳老权重
    SUM(IFNULL(index_mydf,0)) AS 得分,
    '1月'           AS m	-- 月
    FROM ods_wuye_complaint_half_year
    WHERE t_quarter = '一季度'
    AND  yr = '${LEFT(p_ym,4)}'
--  AND yr = '${SWITCH(MONTH(CONCATENATE(p_ym,"-01")),"1",LEFT(p_ym,4)-1,"2",LEFT(p_ym,4)-1,"3",LEFT(p_ym,4)-1,"4",LEFT(p_ym,4),"5",LEFT(p_ym,4),"6",LEFT(p_ym,4),"7",LEFT(p_ym,4),"8",LEFT(p_ym,4),"9",LEFT(p_ym,4),"10",LEFT(p_ym,4),"11",LEFT(p_ym,4),"12",LEFT(p_ym,4))}'
  --  AND t_quarter = '${SWITCH(MONTH(CONCATENATE(p_ym,"-01")),"1","四季度","2","四季度","3","四季度","4","一季度","5","一季度","6","一季度","7","二季度","8","二季度","9","二季度","10","三季度","11","三季度","12","三季度")}'
    ${IF(LEN(p_area)>0," AND 1=2", "")}
    GROUP BY organ_name,index_wl_qz,index_mwl_qz
    UNION ALL
    SELECT
    DISTINCT
    organ_name AS 维度,
    SUM(index_wl_yxybl) AS  wl_yxybl, -- 稳老有效样本量
    SUM(index_wl_myl) AS  wl_myl,  -- 稳老满意量
    index_wl_qz as wl_qz, -- 稳老权重
    SUM(index_mwl_yxybl) AS  mwl_yxybl, -- 磨稳老有效样本量
    SUM(index_mwl_myl) AS  mwl_myl, -- 磨稳老满意量
    index_mwl_qz as mwl_qz, -- 磨稳老权重
    SUM(IFNULL(index_mydf,0)) AS 得分,
    '2月'           AS m	-- 月
    FROM ods_wuye_complaint_half_year
    WHERE t_quarter = '一季度'
    AND  yr = '${LEFT(p_ym,4)}'
    -- AND yr = '${SWITCH(MONTH(CONCATENATE(p_ym,"-01")),"1",LEFT(p_ym,4)-1,"2",LEFT(p_ym,4)-1,"3",LEFT(p_ym,4)-1,"4",LEFT(p_ym,4),"5",LEFT(p_ym,4),"6",LEFT(p_ym,4),"7",LEFT(p_ym,4),"8",LEFT(p_ym,4),"9",LEFT(p_ym,4),"10",LEFT(p_ym,4),"11",LEFT(p_ym,4),"12",LEFT(p_ym,4))}'
    -- AND t_quarter = '${SWITCH(MONTH(CONCATENATE(p_ym,"-01")),"1","四季度","2","四季度","3","四季度","4","一季度","5","一季度","6","一季度","7","二季度","8","二季度","9","二季度","10","三季度","11","三季度","12","三季度")}'
    ${IF(LEN(p_area)>0," AND 1=2", "")}
    GROUP BY organ_name,index_wl_qz,index_mwl_qz
    UNION ALL
    SELECT
    DISTINCT
    organ_name AS 维度,
    SUM(index_wl_yxybl) AS  wl_yxybl, -- 稳老有效样本量
    SUM(index_wl_myl) AS  wl_myl,  -- 稳老满意量
    index_wl_qz as wl_qz, -- 稳老权重
    SUM(index_mwl_yxybl) AS  mwl_yxybl, -- 磨稳老有效样本量
    SUM(index_mwl_myl) AS  mwl_myl, -- 磨稳老满意量
    index_mwl_qz as mwl_qz, -- 磨稳老权重
    SUM(IFNULL(index_mydf,0)) AS 得分,
    '3月'           AS m	-- 月
    FROM ods_wuye_complaint_half_year
    WHERE t_quarter = '一季度'
    AND  yr = '${LEFT(p_ym,4)}'
    -- AND yr = '${SWITCH(MONTH(CONCATENATE(p_ym,"-01")),"1",LEFT(p_ym,4)-1,"2",LEFT(p_ym,4)-1,"3",LEFT(p_ym,4)-1,"4",LEFT(p_ym,4),"5",LEFT(p_ym,4),"6",LEFT(p_ym,4),"7",LEFT(p_ym,4),"8",LEFT(p_ym,4),"9",LEFT(p_ym,4),"10",LEFT(p_ym,4),"11",LEFT(p_ym,4),"12",LEFT(p_ym,4))}'
    -- AND t_quarter = '${SWITCH(MONTH(CONCATENATE(p_ym,"-01")),"1","四季度","2","四季度","3","四季度","4","一季度","5","一季度","6","一季度","7","二季度","8","二季度","9","二季度","10","三季度","11","三季度","12","三季度")}'
    ${IF(LEN(p_area)>0," AND 1=2", "")}
    GROUP BY organ_name,index_wl_qz,index_mwl_qz
    UNION ALL
    SELECT
    DISTINCT
    organ_name AS 维度,
    SUM(index_wl_yxybl) AS  wl_yxybl, -- 稳老有效样本量
    SUM(index_wl_myl) AS  wl_myl,  -- 稳老满意量
    index_wl_qz as wl_qz, -- 稳老权重
    SUM(index_mwl_yxybl) AS  mwl_yxybl, -- 磨稳老有效样本量
    SUM(index_mwl_myl) AS  mwl_myl, -- 磨稳老满意量
    index_mwl_qz as mwl_qz, -- 磨稳老权重
    SUM(IFNULL(index_mydf,0)) AS 得分,
    '4月'           AS m	-- 月
    FROM ods_wuye_complaint_half_year
    WHERE t_quarter = '二季度'
    AND  yr = '${LEFT(p_ym,4)}'
   -- AND yr = '${SWITCH(MONTH(CONCATENATE(p_ym,"-01")),"1",LEFT(p_ym,4)-1,"2",LEFT(p_ym,4)-1,"3",LEFT(p_ym,4)-1,"4",LEFT(p_ym,4),"5",LEFT(p_ym,4),"6",LEFT(p_ym,4),"7",LEFT(p_ym,4),"8",LEFT(p_ym,4),"9",LEFT(p_ym,4),"10",LEFT(p_ym,4),"11",LEFT(p_ym,4),"12",LEFT(p_ym,4))}'
   -- AND t_quarter = '${SWITCH(MONTH(CONCATENATE(p_ym,"-01")),"1","四季度","2","四季度","3","四季度","4","一季度","5","一季度","6","一季度","7","二季度","8","二季度","9","二季度","10","三季度","11","三季度","12","三季度")}'
    ${IF(LEN(p_area)>0," AND 1=2", "")}
    GROUP BY organ_name,index_wl_qz,index_mwl_qz
    UNION ALL
    SELECT
    DISTINCT
    organ_name AS 维度,
    SUM(index_wl_yxybl) AS  wl_yxybl, -- 稳老有效样本量
    SUM(index_wl_myl) AS  wl_myl,  -- 稳老满意量
    index_wl_qz as wl_qz, -- 稳老权重
    SUM(index_mwl_yxybl) AS  mwl_yxybl, -- 磨稳老有效样本量
    SUM(index_mwl_myl) AS  mwl_myl, -- 磨稳老满意量
    index_mwl_qz as mwl_qz, -- 磨稳老权重
    SUM(IFNULL(index_mydf,0)) AS 得分,
    '5月'           AS m	-- 月
    FROM ods_wuye_complaint_half_year
    WHERE t_quarter = '二季度'
    AND  yr = '${LEFT(p_ym,4)}'
   -- AND yr = '${SWITCH(MONTH(CONCATENATE(p_ym,"-01")),"1",LEFT(p_ym,4)-1,"2",LEFT(p_ym,4)-1,"3",LEFT(p_ym,4)-1,"4",LEFT(p_ym,4),"5",LEFT(p_ym,4),"6",LEFT(p_ym,4),"7",LEFT(p_ym,4),"8",LEFT(p_ym,4),"9",LEFT(p_ym,4),"10",LEFT(p_ym,4),"11",LEFT(p_ym,4),"12",LEFT(p_ym,4))}'
   -- AND t_quarter = '${SWITCH(MONTH(CONCATENATE(p_ym,"-01")),"1","四季度","2","四季度","3","四季度","4","一季度","5","一季度","6","一季度","7","二季度","8","二季度","9","二季度","10","三季度","11","三季度","12","三季度")}'
    ${IF(LEN(p_area)>0," AND 1=2", "")}
    GROUP BY organ_name,index_wl_qz,index_mwl_qz
    UNION ALL
    SELECT
    DISTINCT
    organ_name AS 维度,
    SUM(index_wl_yxybl) AS  wl_yxybl, -- 稳老有效样本量
    SUM(index_wl_myl) AS  wl_myl,  -- 稳老满意量
    index_wl_qz as wl_qz, -- 稳老权重
    SUM(index_mwl_yxybl) AS  mwl_yxybl, -- 磨稳老有效样本量
    SUM(index_mwl_myl) AS  mwl_myl, -- 磨稳老满意量
    index_mwl_qz as mwl_qz, -- 磨稳老权重
    SUM(IFNULL(index_mydf,0)) AS 得分,
    '6月'           AS m	-- 月
    FROM ods_wuye_complaint_half_year
    WHERE t_quarter = '二季度'
    AND  yr = '${LEFT(p_ym,4)}'
   -- AND yr = '${SWITCH(MONTH(CONCATENATE(p_ym,"-01")),"1",LEFT(p_ym,4)-1,"2",LEFT(p_ym,4)-1,"3",LEFT(p_ym,4)-1,"4",LEFT(p_ym,4),"5",LEFT(p_ym,4),"6",LEFT(p_ym,4),"7",LEFT(p_ym,4),"8",LEFT(p_ym,4),"9",LEFT(p_ym,4),"10",LEFT(p_ym,4),"11",LEFT(p_ym,4),"12",LEFT(p_ym,4))}'
   -- AND t_quarter = '${SWITCH(MONTH(CONCATENATE(p_ym,"-01")),"1","四季度","2","四季度","3","四季度","4","一季度","5","一季度","6","一季度","7","二季度","8","二季度","9","二季度","10","三季度","11","三季度","12","三季度")}'
    ${IF(LEN(p_area)>0," AND 1=2", "")}
    GROUP BY organ_name,index_wl_qz,index_mwl_qz
    UNION ALL
    SELECT
    DISTINCT
    organ_name AS 维度,
    SUM(index_wl_yxybl) AS  wl_yxybl, -- 稳老有效样本量
    SUM(index_wl_myl) AS  wl_myl,  -- 稳老满意量
    index_wl_qz as wl_qz, -- 稳老权重
    SUM(index_mwl_yxybl) AS  mwl_yxybl, -- 磨稳老有效样本量
    SUM(index_mwl_myl) AS  mwl_myl, -- 磨稳老满意量
    index_mwl_qz as mwl_qz, -- 磨稳老权重
    SUM(IFNULL(index_mydf,0)) AS 得分,
    '7月'           AS m	-- 月
    FROM ods_wuye_complaint_half_year
    WHERE t_quarter = '三季度'
    AND  yr = '${LEFT(p_ym,4)}'
   -- AND yr = '${SWITCH(MONTH(CONCATENATE(p_ym,"-01")),"1",LEFT(p_ym,4)-1,"2",LEFT(p_ym,4)-1,"3",LEFT(p_ym,4)-1,"4",LEFT(p_ym,4),"5",LEFT(p_ym,4),"6",LEFT(p_ym,4),"7",LEFT(p_ym,4),"8",LEFT(p_ym,4),"9",LEFT(p_ym,4),"10",LEFT(p_ym,4),"11",LEFT(p_ym,4),"12",LEFT(p_ym,4))}'
   -- AND t_quarter = '${SWITCH(MONTH(CONCATENATE(p_ym,"-01")),"1","四季度","2","四季度","3","四季度","4","一季度","5","一季度","6","一季度","7","二季度","8","二季度","9","二季度","10","三季度","11","三季度","12","三季度")}'
    ${IF(LEN(p_area)>0," AND 1=2", "")}
    GROUP BY organ_name,index_wl_qz,index_mwl_qz
    UNION ALL
    SELECT
    DISTINCT
    organ_name AS 维度,
    SUM(index_wl_yxybl) AS  wl_yxybl, -- 稳老有效样本量
    SUM(index_wl_myl) AS  wl_myl,  -- 稳老满意量
    index_wl_qz as wl_qz, -- 稳老权重
    SUM(index_mwl_yxybl) AS  mwl_yxybl, -- 磨稳老有效样本量
    SUM(index_mwl_myl) AS  mwl_myl, -- 磨稳老满意量
    index_mwl_qz as mwl_qz, -- 磨稳老权重
    SUM(IFNULL(index_mydf,0)) AS 得分,
    '8月'           AS m	-- 月
    FROM ods_wuye_complaint_half_year
    WHERE t_quarter = '三季度'
    AND  yr = '${LEFT(p_ym,4)}'
   -- AND yr = '${SWITCH(MONTH(CONCATENATE(p_ym,"-01")),"1",LEFT(p_ym,4)-1,"2",LEFT(p_ym,4)-1,"3",LEFT(p_ym,4)-1,"4",LEFT(p_ym,4),"5",LEFT(p_ym,4),"6",LEFT(p_ym,4),"7",LEFT(p_ym,4),"8",LEFT(p_ym,4),"9",LEFT(p_ym,4),"10",LEFT(p_ym,4),"11",LEFT(p_ym,4),"12",LEFT(p_ym,4))}'
  --  AND t_quarter = '${SWITCH(MONTH(CONCATENATE(p_ym,"-01")),"1","四季度","2","四季度","3","四季度","4","一季度","5","一季度","6","一季度","7","二季度","8","二季度","9","二季度","10","三季度","11","三季度","12","三季度")}'
    ${IF(LEN(p_area)>0," AND 1=2", "")}
    GROUP BY organ_name,index_wl_qz,index_mwl_qz
    UNION ALL
    SELECT
    DISTINCT
    organ_name AS 维度,
    SUM(index_wl_yxybl) AS  wl_yxybl, -- 稳老有效样本量
    SUM(index_wl_myl) AS  wl_myl,  -- 稳老满意量
    index_wl_qz as wl_qz, -- 稳老权重
    SUM(index_mwl_yxybl) AS  mwl_yxybl, -- 磨稳老有效样本量
    SUM(index_mwl_myl) AS  mwl_myl, -- 磨稳老满意量
    index_mwl_qz as mwl_qz, -- 磨稳老权重
    SUM(IFNULL(index_mydf,0)) AS 得分,
    '9月'           AS m	-- 月
    FROM ods_wuye_complaint_half_year
    WHERE t_quarter = '三季度'
    AND  yr = '${LEFT(p_ym,4)}'
   -- AND yr = '${SWITCH(MONTH(CONCATENATE(p_ym,"-01")),"1",LEFT(p_ym,4)-1,"2",LEFT(p_ym,4)-1,"3",LEFT(p_ym,4)-1,"4",LEFT(p_ym,4),"5",LEFT(p_ym,4),"6",LEFT(p_ym,4),"7",LEFT(p_ym,4),"8",LEFT(p_ym,4),"9",LEFT(p_ym,4),"10",LEFT(p_ym,4),"11",LEFT(p_ym,4),"12",LEFT(p_ym,4))}'
  --  AND t_quarter = '${SWITCH(MONTH(CONCATENATE(p_ym,"-01")),"1","四季度","2","四季度","3","四季度","4","一季度","5","一季度","6","一季度","7","二季度","8","二季度","9","二季度","10","三季度","11","三季度","12","三季度")}'
    ${IF(LEN(p_area)>0," AND 1=2", "")}
    GROUP BY organ_name,index_wl_qz,index_mwl_qz
    UNION ALL
    SELECT
    DISTINCT
    organ_name AS 维度,
    SUM(index_wl_yxybl) AS  wl_yxybl, -- 稳老有效样本量
    SUM(index_wl_myl) AS  wl_myl,  -- 稳老满意量
    index_wl_qz as wl_qz, -- 稳老权重
    SUM(index_mwl_yxybl) AS  mwl_yxybl, -- 磨稳老有效样本量
    SUM(index_mwl_myl) AS  mwl_myl, -- 磨稳老满意量
    index_mwl_qz as mwl_qz, -- 磨稳老权重
    SUM(IFNULL(index_mydf,0)) AS 得分,
    '10月'           AS m	-- 月
    FROM ods_wuye_complaint_half_year
    WHERE t_quarter = '四季度'
    AND  yr = '${LEFT(p_ym,4)}'
   -- AND yr = '${SWITCH(MONTH(CONCATENATE(p_ym,"-01")),"1",LEFT(p_ym,4)-1,"2",LEFT(p_ym,4)-1,"3",LEFT(p_ym,4)-1,"4",LEFT(p_ym,4),"5",LEFT(p_ym,4),"6",LEFT(p_ym,4),"7",LEFT(p_ym,4),"8",LEFT(p_ym,4),"9",LEFT(p_ym,4),"10",LEFT(p_ym,4),"11",LEFT(p_ym,4),"12",LEFT(p_ym,4))}'
   -- AND t_quarter = '${SWITCH(MONTH(CONCATENATE(p_ym,"-01")),"1","四季度","2","四季度","3","四季度","4","一季度","5","一季度","6","一季度","7","二季度","8","二季度","9","二季度","10","三季度","11","三季度","12","三季度")}'
    ${IF(LEN(p_area)>0," AND 1=2", "")}
    GROUP BY organ_name,index_wl_qz,index_mwl_qz
    UNION ALL
    SELECT
    DISTINCT
    organ_name AS 维度,
    SUM(index_wl_yxybl) AS  wl_yxybl, -- 稳老有效样本量
    SUM(index_wl_myl) AS  wl_myl,  -- 稳老满意量
    index_wl_qz as wl_qz, -- 稳老权重
    SUM(index_mwl_yxybl) AS  mwl_yxybl, -- 磨稳老有效样本量
    SUM(index_mwl_myl) AS  mwl_myl, -- 磨稳老满意量
    index_mwl_qz as mwl_qz, -- 磨稳老权重
    SUM(IFNULL(index_mydf,0)) AS 得分,
    '11月'           AS m	-- 月
    FROM ods_wuye_complaint_half_year
    WHERE t_quarter = '四季度'
    AND  yr = '${LEFT(p_ym,4)}'
  --  AND yr = '${SWITCH(MONTH(CONCATENATE(p_ym,"-01")),"1",LEFT(p_ym,4)-1,"2",LEFT(p_ym,4)-1,"3",LEFT(p_ym,4)-1,"4",LEFT(p_ym,4),"5",LEFT(p_ym,4),"6",LEFT(p_ym,4),"7",LEFT(p_ym,4),"8",LEFT(p_ym,4),"9",LEFT(p_ym,4),"10",LEFT(p_ym,4),"11",LEFT(p_ym,4),"12",LEFT(p_ym,4))}'
  --  AND t_quarter = '${SWITCH(MONTH(CONCATENATE(p_ym,"-01")),"1","四季度","2","四季度","3","四季度","4","一季度","5","一季度","6","一季度","7","二季度","8","二季度","9","二季度","10","三季度","11","三季度","12","三季度")}'
    ${IF(LEN(p_area)>0," AND 1=2", "")}
    GROUP BY organ_name,index_wl_qz,index_mwl_qz
    UNION ALL
    SELECT
    DISTINCT
    organ_name AS 维度,
    SUM(index_wl_yxybl) AS  wl_yxybl, -- 稳老有效样本量
    SUM(index_wl_myl) AS  wl_myl,  -- 稳老满意量
    index_wl_qz as wl_qz, -- 稳老权重
    SUM(index_mwl_yxybl) AS  mwl_yxybl, -- 磨稳老有效样本量
    SUM(index_mwl_myl) AS  mwl_myl, -- 磨稳老满意量
    index_mwl_qz as mwl_qz, -- 磨稳老权重
    SUM(IFNULL(index_mydf,0)) AS 得分,
    '12月'           AS m	-- 月
    FROM ods_wuye_complaint_half_year
    WHERE t_quarter = '四季度'
    AND  yr = '${LEFT(p_ym,4)}'
  --  AND yr = '${SWITCH(MONTH(CONCATENATE(p_ym,"-01")),"1",LEFT(p_ym,4)-1,"2",LEFT(p_ym,4)-1,"3",LEFT(p_ym,4)-1,"4",LEFT(p_ym,4),"5",LEFT(p_ym,4),"6",LEFT(p_ym,4),"7",LEFT(p_ym,4),"8",LEFT(p_ym,4),"9",LEFT(p_ym,4),"10",LEFT(p_ym,4),"11",LEFT(p_ym,4),"12",LEFT(p_ym,4))}'
  --  AND t_quarter = '${SWITCH(MONTH(CONCATENATE(p_ym,"-01")),"1","四季度","2","四季度","3","四季度","4","一季度","5","一季度","6","一季度","7","二季度","8","二季度","9","二季度","10","三季度","11","三季度","12","三季度")}'
    ${IF(LEN(p_area)>0," AND 1=2", "")}
    GROUP BY organ_name,index_wl_qz,index_mwl_qz

    ),T3 AS (
    /*各项目目标*/
    SELECT
    comm_name AS 维度,
    comm_target AS 目标,
   (SELECT
    DISTINCT
    km_target
    FROM ods_wuye_gszt
    WHERE km_target IS NOT NULL
    AND yr = '${LEFT(p_ym,4)}') AS total_target, -- 公司整体目标得分
    yr,
    '1月'           AS m	-- 月
    FROM ods_wuye_satisfaction_target
    WHERE yr = '${LEFT(p_ym,4)}'
    ${IF(p_type = "内盘","AND comm_type = '内盘'", "AND comm_type = '外盘'")}
    AND comm_name NOT LIKE '%案场%'
    AND comm_name NOT LIKE '%撤场%'
    AND comm_name NOT LIKE '%禁用%'
    AND comm_target IS NOT NULL
    ${IF(LEN(p_area)>0," AND organ_code IN ('"+JOINARRAY(p_area,"','")+"')", "")}
    ${IF(LEN(p_area)>0,"", "AND 1=2")}
    UNION ALL
    SELECT
    comm_name AS 维度,
    comm_target AS 目标,
   (SELECT
    DISTINCT
    km_target
    FROM ods_wuye_gszt
    WHERE km_target IS NOT NULL
    AND yr = '${LEFT(p_ym,4)}') AS total_target, -- 公司整体目标得分
    yr,
    '2月'           AS m	-- 月
    FROM ods_wuye_satisfaction_target
    WHERE yr = '${LEFT(p_ym,4)}'
    ${IF(p_type = "内盘","AND comm_type = '内盘'", "AND comm_type = '外盘'")}
    AND comm_name NOT LIKE '%案场%'
    AND comm_name NOT LIKE '%撤场%'
    AND comm_name NOT LIKE '%禁用%'
    AND comm_target IS NOT NULL
    ${IF(LEN(p_area)>0," AND organ_code IN ('"+JOINARRAY(p_area,"','")+"')", "")}
    ${IF(LEN(p_area)>0,"", "AND 1=2")}
    UNION ALL
    SELECT
    comm_name AS 维度,
    comm_target AS 目标,
   (SELECT
    DISTINCT
    km_target
    FROM ods_wuye_gszt
    WHERE km_target IS NOT NULL
    AND yr = '${LEFT(p_ym,4)}') AS total_target, -- 公司整体目标得分
    yr,
    '3月'           AS m	-- 月
    FROM ods_wuye_satisfaction_target
    WHERE yr = '${LEFT(p_ym,4)}'
    ${IF(p_type = "内盘","AND comm_type = '内盘'", "AND comm_type = '外盘'")}
    AND comm_name NOT LIKE '%案场%'
    AND comm_name NOT LIKE '%撤场%'
    AND comm_name NOT LIKE '%禁用%'
    AND comm_target IS NOT NULL
    ${IF(LEN(p_area)>0," AND organ_code IN ('"+JOINARRAY(p_area,"','")+"')", "")}
    ${IF(LEN(p_area)>0,"", "AND 1=2")}
    UNION ALL
    SELECT
    comm_name AS 维度,
    comm_target AS 目标,
   (SELECT
    DISTINCT
    km_target
    FROM ods_wuye_gszt
    WHERE km_target IS NOT NULL
    AND yr = '${LEFT(p_ym,4)}') AS total_target, -- 公司整体目标得分
    yr,
    '4月'           AS m	-- 月
    FROM ods_wuye_satisfaction_target
    WHERE yr = '${LEFT(p_ym,4)}'
    ${IF(p_type = "内盘","AND comm_type = '内盘'", "AND comm_type = '外盘'")}
    AND comm_name NOT LIKE '%案场%'
    AND comm_name NOT LIKE '%撤场%'
    AND comm_name NOT LIKE '%禁用%'
    AND comm_target IS NOT NULL
    ${IF(LEN(p_area)>0," AND organ_code IN ('"+JOINARRAY(p_area,"','")+"')", "")}
    ${IF(LEN(p_area)>0,"", "AND 1=2")}
    UNION ALL
    SELECT
    comm_name AS 维度,
    comm_target AS 目标,
   (SELECT
    DISTINCT
    km_target
    FROM ods_wuye_gszt
    WHERE km_target IS NOT NULL
    AND yr = '${LEFT(p_ym,4)}') AS total_target, -- 公司整体目标得分
    yr,
    '5月'           AS m	-- 月
    FROM ods_wuye_satisfaction_target
    WHERE yr = '${LEFT(p_ym,4)}'
    ${IF(p_type = "内盘","AND comm_type = '内盘'", "AND comm_type = '外盘'")}
    AND comm_name NOT LIKE '%案场%'
    AND comm_name NOT LIKE '%撤场%'
    AND comm_name NOT LIKE '%禁用%'
    AND comm_target IS NOT NULL
    ${IF(LEN(p_area)>0," AND organ_code IN ('"+JOINARRAY(p_area,"','")+"')", "")}
    ${IF(LEN(p_area)>0,"", "AND 1=2")}
    UNION ALL
    SELECT
    comm_name AS 维度,
    comm_target AS 目标,
   (SELECT
    DISTINCT
    km_target
    FROM ods_wuye_gszt
    WHERE km_target IS NOT NULL
    AND yr = '${LEFT(p_ym,4)}') AS total_target, -- 公司整体目标得分
    yr,
    '6月'           AS m	-- 月
    FROM ods_wuye_satisfaction_target
    WHERE yr = '${LEFT(p_ym,4)}'
    ${IF(p_type = "内盘","AND comm_type = '内盘'", "AND comm_type = '外盘'")}
    AND comm_name NOT LIKE '%案场%'
    AND comm_name NOT LIKE '%撤场%'
    AND comm_name NOT LIKE '%禁用%'
    AND comm_target IS NOT NULL
    ${IF(LEN(p_area)>0," AND organ_code IN ('"+JOINARRAY(p_area,"','")+"')", "")}
    ${IF(LEN(p_area)>0,"", "AND 1=2")}
    UNION ALL
    SELECT
    comm_name AS 维度,
    comm_target AS 目标,
   (SELECT
    DISTINCT
    km_target
    FROM ods_wuye_gszt
    WHERE km_target IS NOT NULL
    AND yr = '${LEFT(p_ym,4)}') AS total_target, -- 公司整体目标得分
    yr,
    '7月'           AS m	-- 月
    FROM ods_wuye_satisfaction_target
    WHERE yr = '${LEFT(p_ym,4)}'
    ${IF(p_type = "内盘","AND comm_type = '内盘'", "AND comm_type = '外盘'")}
    AND comm_name NOT LIKE '%案场%'
    AND comm_name NOT LIKE '%撤场%'
    AND comm_name NOT LIKE '%禁用%'
    AND comm_target IS NOT NULL
    ${IF(LEN(p_area)>0," AND organ_code IN ('"+JOINARRAY(p_area,"','")+"')", "")}
    ${IF(LEN(p_area)>0,"", "AND 1=2")}
    UNION ALL
    SELECT
    comm_name AS 维度,
    comm_target AS 目标,
   (SELECT
    DISTINCT
    km_target
    FROM ods_wuye_gszt
    WHERE km_target IS NOT NULL
    AND yr = '${LEFT(p_ym,4)}') AS total_target, -- 公司整体目标得分
    yr,
    '8月'           AS m	-- 月
    FROM ods_wuye_satisfaction_target
    WHERE yr = '${LEFT(p_ym,4)}'
    ${IF(p_type = "内盘","AND comm_type = '内盘'", "AND comm_type = '外盘'")}
    AND comm_name NOT LIKE '%案场%'
    AND comm_name NOT LIKE '%撤场%'
    AND comm_name NOT LIKE '%禁用%'
    AND comm_target IS NOT NULL
    ${IF(LEN(p_area)>0," AND organ_code IN ('"+JOINARRAY(p_area,"','")+"')", "")}
    ${IF(LEN(p_area)>0,"", "AND 1=2")}
    UNION ALL
    SELECT
    comm_name AS 维度,
    comm_target AS 目标,
   (SELECT
    DISTINCT
    km_target
    FROM ods_wuye_gszt
    WHERE km_target IS NOT NULL
    AND yr = '${LEFT(p_ym,4)}') AS total_target, -- 公司整体目标得分
    yr,
    '9月'           AS m	-- 月
    FROM ods_wuye_satisfaction_target
    WHERE yr = '${LEFT(p_ym,4)}'
    ${IF(p_type = "内盘","AND comm_type = '内盘'", "AND comm_type = '外盘'")}
    AND comm_name NOT LIKE '%案场%'
    AND comm_name NOT LIKE '%撤场%'
    AND comm_name NOT LIKE '%禁用%'
    AND comm_target IS NOT NULL
    ${IF(LEN(p_area)>0," AND organ_code IN ('"+JOINARRAY(p_area,"','")+"')", "")}
    ${IF(LEN(p_area)>0,"", "AND 1=2")}
    UNION ALL
    SELECT
    comm_name AS 维度,
    comm_target AS 目标,
   (SELECT
    DISTINCT
    km_target
    FROM ods_wuye_gszt
    WHERE km_target IS NOT NULL
    AND yr = '${LEFT(p_ym,4)}') AS total_target, -- 公司整体目标得分
    yr,
    '10月'           AS m	-- 月
    FROM ods_wuye_satisfaction_target
    WHERE yr = '${LEFT(p_ym,4)}'
    ${IF(p_type = "内盘","AND comm_type = '内盘'", "AND comm_type = '外盘'")}
    AND comm_name NOT LIKE '%案场%'
    AND comm_name NOT LIKE '%撤场%'
    AND comm_name NOT LIKE '%禁用%'
    AND comm_target IS NOT NULL
    ${IF(LEN(p_area)>0," AND organ_code IN ('"+JOINARRAY(p_area,"','")+"')", "")}
    ${IF(LEN(p_area)>0,"", "AND 1=2")}
    UNION ALL
    SELECT
    comm_name AS 维度,
    comm_target AS 目标,
   (SELECT
    DISTINCT
    km_target
    FROM ods_wuye_gszt
    WHERE km_target IS NOT NULL
    AND yr = '${LEFT(p_ym,4)}') AS total_target, -- 公司整体目标得分
    yr,
    '11月'           AS m	-- 月
    FROM ods_wuye_satisfaction_target
    WHERE yr = '${LEFT(p_ym,4)}'
    ${IF(p_type = "内盘","AND comm_type = '内盘'", "AND comm_type = '外盘'")}
    AND comm_name NOT LIKE '%案场%'
    AND comm_name NOT LIKE '%撤场%'
    AND comm_name NOT LIKE '%禁用%'
    AND comm_target IS NOT NULL
    ${IF(LEN(p_area)>0," AND organ_code IN ('"+JOINARRAY(p_area,"','")+"')", "")}
    ${IF(LEN(p_area)>0,"", "AND 1=2")}
    UNION ALL
    SELECT
    comm_name AS 维度,
    comm_target AS 目标,
   (SELECT
    DISTINCT
    km_target
    FROM ods_wuye_gszt
    WHERE km_target IS NOT NULL
    AND yr = '${LEFT(p_ym,4)}') AS total_target, -- 公司整体目标得分
    yr,
    '12月'           AS m	-- 月
    FROM ods_wuye_satisfaction_target
    WHERE yr = '${LEFT(p_ym,4)}'
    ${IF(p_type = "内盘","AND comm_type = '内盘'", "AND comm_type = '外盘'")}
    AND comm_name NOT LIKE '%案场%'
    AND comm_name NOT LIKE '%撤场%'
    AND comm_name NOT LIKE '%禁用%'
    AND comm_target IS NOT NULL
    ${IF(LEN(p_area)>0," AND organ_code IN ('"+JOINARRAY(p_area,"','")+"')", "")}
    ${IF(LEN(p_area)>0,"", "AND 1=2")}

    ),T4 AS (
    /*各项目得分*/
    SELECT
    comm_name AS 维度,
    index_dc_myddf AS 得分,
    index_dc_ybl as wl_yxybl, -- 地产有效样本量
    index_dc_myl as wl_myl,  -- 地产满意量
    index_dc_qz as wl_qz, -- 地产权重
    index_wy_ybl as mwl_yxybl, -- 物业有效样本量（总样本量）
    index_wy_myl as mwl_myl, -- 物业满意量
    index_wy_qz as mwl_qz, -- 权重80%
    yr,
    '1月'           AS m	-- 月
    FROM ods_wuye_complaint
    WHERE  jd = '一季度'
    AND  yr = '${LEFT(p_ym,4)}'
   -- AND yr = '${SWITCH(MONTH(CONCATENATE(p_ym,"-01")),"1",LEFT(p_ym,4)-1,"2",LEFT(p_ym,4)-1,"3",LEFT(p_ym,4)-1,"4",LEFT(p_ym,4),"5",LEFT(p_ym,4),"6",LEFT(p_ym,4),"7",LEFT(p_ym,4),"8",LEFT(p_ym,4),"9",LEFT(p_ym,4),"10",LEFT(p_ym,4),"11",LEFT(p_ym,4),"12",LEFT(p_ym,4))}'
  --  AND jd = '${SWITCH(MONTH(CONCATENATE(p_ym,"-01")),"1","四季度","2","四季度","3","四季度","4","一季度","5","一季度","6","一季度","7","二季度","8","二季度","9","二季度","10","三季度","11","三季度","12","三季度")}'
    ${IF(LEN(p_area)>0,"", "AND 1=2")}
    UNION ALL
    SELECT
    comm_name AS 维度,
    index_dc_myddf AS 得分,
    index_dc_ybl as wl_yxybl, -- 地产有效样本量
    index_dc_myl as wl_myl,  -- 地产满意量
    index_dc_qz as wl_qz, -- 地产权重
    index_wy_ybl as mwl_yxybl, -- 物业有效样本量（总样本量）
    index_wy_myl as mwl_myl, -- 物业满意量
    index_wy_qz as mwl_qz, -- 权重80%
    yr,
    '2月'           AS m	-- 月
    FROM ods_wuye_complaint
    WHERE  jd = '一季度'
    AND  yr = '${LEFT(p_ym,4)}'
--   AND yr = '${SWITCH(MONTH(CONCATENATE(p_ym,"-01")),"1",LEFT(p_ym,4)-1,"2",LEFT(p_ym,4)-1,"3",LEFT(p_ym,4)-1,"4",LEFT(p_ym,4),"5",LEFT(p_ym,4),"6",LEFT(p_ym,4),"7",LEFT(p_ym,4),"8",LEFT(p_ym,4),"9",LEFT(p_ym,4),"10",LEFT(p_ym,4),"11",LEFT(p_ym,4),"12",LEFT(p_ym,4))}'
  --  AND jd = '${SWITCH(MONTH(CONCATENATE(p_ym,"-01")),"1","四季度","2","四季度","3","四季度","4","一季度","5","一季度","6","一季度","7","二季度","8","二季度","9","二季度","10","三季度","11","三季度","12","三季度")}'
    ${IF(LEN(p_area)>0,"", "AND 1=2")}
    UNION ALL
    SELECT
    comm_name AS 维度,
    index_dc_myddf AS 得分,
    index_dc_ybl as wl_yxybl, -- 地产有效样本量
    index_dc_myl as wl_myl,  -- 地产满意量
    index_dc_qz as wl_qz, -- 地产权重
    index_wy_ybl as mwl_yxybl, -- 物业有效样本量（总样本量）
    index_wy_myl as mwl_myl, -- 物业满意量
    index_wy_qz as mwl_qz, -- 权重80%
    yr,
    '3月'           AS m	-- 月
    FROM ods_wuye_complaint
    WHERE  jd = '一季度'
    AND  yr = '${LEFT(p_ym,4)}'
-- AND yr = '${SWITCH(MONTH(CONCATENATE(p_ym,"-01")),"1",LEFT(p_ym,4)-1,"2",LEFT(p_ym,4)-1,"3",LEFT(p_ym,4)-1,"4",LEFT(p_ym,4),"5",LEFT(p_ym,4),"6",LEFT(p_ym,4),"7",LEFT(p_ym,4),"8",LEFT(p_ym,4),"9",LEFT(p_ym,4),"10",LEFT(p_ym,4),"11",LEFT(p_ym,4),"12",LEFT(p_ym,4))}'
  --  AND jd = '${SWITCH(MONTH(CONCATENATE(p_ym,"-01")),"1","四季度","2","四季度","3","四季度","4","一季度","5","一季度","6","一季度","7","二季度","8","二季度","9","二季度","10","三季度","11","三季度","12","三季度")}'
    ${IF(LEN(p_area)>0,"", "AND 1=2")}
    UNION ALL
    SELECT
    comm_name AS 维度,
    index_dc_myddf AS 得分,
    index_dc_ybl as wl_yxybl, -- 地产有效样本量
    index_dc_myl as wl_myl,  -- 地产满意量
    index_dc_qz as wl_qz, -- 地产权重
    index_wy_ybl as mwl_yxybl, -- 物业有效样本量（总样本量）
    index_wy_myl as mwl_myl, -- 物业满意量
    index_wy_qz as mwl_qz, -- 权重80%
    yr,
    '4月'           AS m	-- 月
    FROM ods_wuye_complaint
    WHERE  jd = '二季度'
    AND  yr = '${LEFT(p_ym,4)}'
--  AND yr = '${SWITCH(MONTH(CONCATENATE(p_ym,"-01")),"1",LEFT(p_ym,4)-1,"2",LEFT(p_ym,4)-1,"3",LEFT(p_ym,4)-1,"4",LEFT(p_ym,4),"5",LEFT(p_ym,4),"6",LEFT(p_ym,4),"7",LEFT(p_ym,4),"8",LEFT(p_ym,4),"9",LEFT(p_ym,4),"10",LEFT(p_ym,4),"11",LEFT(p_ym,4),"12",LEFT(p_ym,4))}'
  --  AND jd = '${SWITCH(MONTH(CONCATENATE(p_ym,"-01")),"1","四季度","2","四季度","3","四季度","4","一季度","5","一季度","6","一季度","7","二季度","8","二季度","9","二季度","10","三季度","11","三季度","12","三季度")}'
    ${IF(LEN(p_area)>0,"", "AND 1=2")}
    UNION ALL
    SELECT
    comm_name AS 维度,
    index_dc_myddf AS 得分,
    index_dc_ybl as wl_yxybl, -- 地产有效样本量
    index_dc_myl as wl_myl,  -- 地产满意量
    index_dc_qz as wl_qz, -- 地产权重
    index_wy_ybl as mwl_yxybl, -- 物业有效样本量（总样本量）
    index_wy_myl as mwl_myl, -- 物业满意量
    index_wy_qz as mwl_qz, -- 权重80%
    yr,
    '5月'           AS m	-- 月
    FROM ods_wuye_complaint
    WHERE  jd = '二季度'
    AND  yr = '${LEFT(p_ym,4)}'
--  AND yr = '${SWITCH(MONTH(CONCATENATE(p_ym,"-01")),"1",LEFT(p_ym,4)-1,"2",LEFT(p_ym,4)-1,"3",LEFT(p_ym,4)-1,"4",LEFT(p_ym,4),"5",LEFT(p_ym,4),"6",LEFT(p_ym,4),"7",LEFT(p_ym,4),"8",LEFT(p_ym,4),"9",LEFT(p_ym,4),"10",LEFT(p_ym,4),"11",LEFT(p_ym,4),"12",LEFT(p_ym,4))}'
  --  AND jd = '${SWITCH(MONTH(CONCATENATE(p_ym,"-01")),"1","四季度","2","四季度","3","四季度","4","一季度","5","一季度","6","一季度","7","二季度","8","二季度","9","二季度","10","三季度","11","三季度","12","三季度")}'
    ${IF(LEN(p_area)>0,"", "AND 1=2")}
    UNION ALL
    SELECT
    comm_name AS 维度,
    index_dc_myddf AS 得分,
    index_dc_ybl as wl_yxybl, -- 地产有效样本量
    index_dc_myl as wl_myl,  -- 地产满意量
    index_dc_qz as wl_qz, -- 地产权重
    index_wy_ybl as mwl_yxybl, -- 物业有效样本量（总样本量）
    index_wy_myl as mwl_myl, -- 物业满意量
    index_wy_qz as mwl_qz, -- 权重80%
    yr,
    '6月'           AS m	-- 月
    FROM ods_wuye_complaint
    WHERE  jd = '二季度'
    AND  yr = '${LEFT(p_ym,4)}'
--  AND yr = '${SWITCH(MONTH(CONCATENATE(p_ym,"-01")),"1",LEFT(p_ym,4)-1,"2",LEFT(p_ym,4)-1,"3",LEFT(p_ym,4)-1,"4",LEFT(p_ym,4),"5",LEFT(p_ym,4),"6",LEFT(p_ym,4),"7",LEFT(p_ym,4),"8",LEFT(p_ym,4),"9",LEFT(p_ym,4),"10",LEFT(p_ym,4),"11",LEFT(p_ym,4),"12",LEFT(p_ym,4))}'
  --  AND jd = '${SWITCH(MONTH(CONCATENATE(p_ym,"-01")),"1","四季度","2","四季度","3","四季度","4","一季度","5","一季度","6","一季度","7","二季度","8","二季度","9","二季度","10","三季度","11","三季度","12","三季度")}'
    ${IF(LEN(p_area)>0,"", "AND 1=2")}
    UNION ALL
    SELECT
    comm_name AS 维度,
    index_dc_myddf AS 得分,
    index_dc_ybl as wl_yxybl, -- 地产有效样本量
    index_dc_myl as wl_myl,  -- 地产满意量
    index_dc_qz as wl_qz, -- 地产权重
    index_wy_ybl as mwl_yxybl, -- 物业有效样本量（总样本量）
    index_wy_myl as mwl_myl, -- 物业满意量
    index_wy_qz as mwl_qz, -- 权重80%
    yr,
    '7月'           AS m	-- 月
    FROM ods_wuye_complaint
    WHERE  jd = '三季度'
    AND  yr = '${LEFT(p_ym,4)}'
--  AND yr = '${SWITCH(MONTH(CONCATENATE(p_ym,"-01")),"1",LEFT(p_ym,4)-1,"2",LEFT(p_ym,4)-1,"3",LEFT(p_ym,4)-1,"4",LEFT(p_ym,4),"5",LEFT(p_ym,4),"6",LEFT(p_ym,4),"7",LEFT(p_ym,4),"8",LEFT(p_ym,4),"9",LEFT(p_ym,4),"10",LEFT(p_ym,4),"11",LEFT(p_ym,4),"12",LEFT(p_ym,4))}'
   -- AND jd = '${SWITCH(MONTH(CONCATENATE(p_ym,"-01")),"1","四季度","2","四季度","3","四季度","4","一季度","5","一季度","6","一季度","7","二季度","8","二季度","9","二季度","10","三季度","11","三季度","12","三季度")}'
    ${IF(LEN(p_area)>0,"", "AND 1=2")}
    UNION ALL
    SELECT
    comm_name AS 维度,
    index_dc_myddf AS 得分,
    index_dc_ybl as wl_yxybl, -- 地产有效样本量
    index_dc_myl as wl_myl,  -- 地产满意量
    index_dc_qz as wl_qz, -- 地产权重
    index_wy_ybl as mwl_yxybl, -- 物业有效样本量（总样本量）
    index_wy_myl as mwl_myl, -- 物业满意量
    index_wy_qz as mwl_qz, -- 权重80%
    yr,
    '8月'           AS m	-- 月
    FROM ods_wuye_complaint
    WHERE  jd = '三季度'
    AND  yr = '${LEFT(p_ym,4)}'
   -- AND yr = '${SWITCH(MONTH(CONCATENATE(p_ym,"-01")),"1",LEFT(p_ym,4)-1,"2",LEFT(p_ym,4)-1,"3",LEFT(p_ym,4)-1,"4",LEFT(p_ym,4),"5",LEFT(p_ym,4),"6",LEFT(p_ym,4),"7",LEFT(p_ym,4),"8",LEFT(p_ym,4),"9",LEFT(p_ym,4),"10",LEFT(p_ym,4),"11",LEFT(p_ym,4),"12",LEFT(p_ym,4))}'
  --  AND jd = '${SWITCH(MONTH(CONCATENATE(p_ym,"-01")),"1","四季度","2","四季度","3","四季度","4","一季度","5","一季度","6","一季度","7","二季度","8","二季度","9","二季度","10","三季度","11","三季度","12","三季度")}'
    ${IF(LEN(p_area)>0,"", "AND 1=2")}
    UNION ALL
    SELECT
    comm_name AS 维度,
    index_dc_myddf AS 得分,
    index_dc_ybl as wl_yxybl, -- 地产有效样本量
    index_dc_myl as wl_myl,  -- 地产满意量
    index_dc_qz as wl_qz, -- 地产权重
    index_wy_ybl as mwl_yxybl, -- 物业有效样本量（总样本量）
    index_wy_myl as mwl_myl, -- 物业满意量
    index_wy_qz as mwl_qz, -- 权重80%
    yr,
    '9月'           AS m	-- 月
    FROM ods_wuye_complaint
    WHERE  jd = '三季度'
    AND  yr = '${LEFT(p_ym,4)}'
  --  AND yr = '${SWITCH(MONTH(CONCATENATE(p_ym,"-01")),"1",LEFT(p_ym,4)-1,"2",LEFT(p_ym,4)-1,"3",LEFT(p_ym,4)-1,"4",LEFT(p_ym,4),"5",LEFT(p_ym,4),"6",LEFT(p_ym,4),"7",LEFT(p_ym,4),"8",LEFT(p_ym,4),"9",LEFT(p_ym,4),"10",LEFT(p_ym,4),"11",LEFT(p_ym,4),"12",LEFT(p_ym,4))}'
 --   AND jd = '${SWITCH(MONTH(CONCATENATE(p_ym,"-01")),"1","四季度","2","四季度","3","四季度","4","一季度","5","一季度","6","一季度","7","二季度","8","二季度","9","二季度","10","三季度","11","三季度","12","三季度")}'
    ${IF(LEN(p_area)>0,"", "AND 1=2")}
    UNION ALL
    SELECT
    comm_name AS 维度,
    index_dc_myddf AS 得分,
    index_dc_ybl as wl_yxybl, -- 地产有效样本量
    index_dc_myl as wl_myl,  -- 地产满意量
    index_dc_qz as wl_qz, -- 地产权重
    index_wy_ybl as mwl_yxybl, -- 物业有效样本量（总样本量）
    index_wy_myl as mwl_myl, -- 物业满意量
    index_wy_qz as mwl_qz, -- 权重80%
    yr,
    '10月'           AS m	-- 月
    FROM ods_wuye_complaint
    WHERE  jd = '四季度'
    AND  yr = '${LEFT(p_ym,4)}'
 --   AND yr = '${SWITCH(MONTH(CONCATENATE(p_ym,"-01")),"1",LEFT(p_ym,4)-1,"2",LEFT(p_ym,4)-1,"3",LEFT(p_ym,4)-1,"4",LEFT(p_ym,4),"5",LEFT(p_ym,4),"6",LEFT(p_ym,4),"7",LEFT(p_ym,4),"8",LEFT(p_ym,4),"9",LEFT(p_ym,4),"10",LEFT(p_ym,4),"11",LEFT(p_ym,4),"12",LEFT(p_ym,4))}'
  --  AND jd = '${SWITCH(MONTH(CONCATENATE(p_ym,"-01")),"1","四季度","2","四季度","3","四季度","4","一季度","5","一季度","6","一季度","7","二季度","8","二季度","9","二季度","10","三季度","11","三季度","12","三季度")}'
    ${IF(LEN(p_area)>0,"", "AND 1=2")}
    UNION ALL
    SELECT
    comm_name AS 维度,
    index_dc_myddf AS 得分,
    index_dc_ybl as wl_yxybl, -- 地产有效样本量
    index_dc_myl as wl_myl,  -- 地产满意量
    index_dc_qz as wl_qz, -- 地产权重
    index_wy_ybl as mwl_yxybl, -- 物业有效样本量（总样本量）
    index_wy_myl as mwl_myl, -- 物业满意量
    index_wy_qz as mwl_qz, -- 权重80%
    yr,
    '11月'           AS m	-- 月
    FROM ods_wuye_complaint
    WHERE  jd = '四季度'
    AND  yr = '${LEFT(p_ym,4)}'
 --   AND yr = '${SWITCH(MONTH(CONCATENATE(p_ym,"-01")),"1",LEFT(p_ym,4)-1,"2",LEFT(p_ym,4)-1,"3",LEFT(p_ym,4)-1,"4",LEFT(p_ym,4),"5",LEFT(p_ym,4),"6",LEFT(p_ym,4),"7",LEFT(p_ym,4),"8",LEFT(p_ym,4),"9",LEFT(p_ym,4),"10",LEFT(p_ym,4),"11",LEFT(p_ym,4),"12",LEFT(p_ym,4))}'
  --  AND jd = '${SWITCH(MONTH(CONCATENATE(p_ym,"-01")),"1","四季度","2","四季度","3","四季度","4","一季度","5","一季度","6","一季度","7","二季度","8","二季度","9","二季度","10","三季度","11","三季度","12","三季度")}'
    ${IF(LEN(p_area)>0,"", "AND 1=2")}
    UNION ALL
    SELECT
    comm_name AS 维度,
    index_dc_myddf AS 得分,
    index_dc_ybl as wl_yxybl, -- 地产有效样本量
    index_dc_myl as wl_myl,  -- 地产满意量
    index_dc_qz as wl_qz, -- 地产权重
    index_wy_ybl as mwl_yxybl, -- 物业有效样本量（总样本量）
    index_wy_myl as mwl_myl, -- 物业满意量
    index_wy_qz as mwl_qz, -- 权重80%
    yr,
    '12月'           AS m	-- 月
    FROM ods_wuye_complaint
    WHERE  jd = '四季度'
    AND  yr = '${LEFT(p_ym,4)}'
    -- AND yr = '${SWITCH(MONTH(CONCATENATE(p_ym,"-01")),"1",LEFT(p_ym,4)-1,"2",LEFT(p_ym,4)-1,"3",LEFT(p_ym,4)-1,"4",LEFT(p_ym,4),"5",LEFT(p_ym,4),"6",LEFT(p_ym,4),"7",LEFT(p_ym,4),"8",LEFT(p_ym,4),"9",LEFT(p_ym,4),"10",LEFT(p_ym,4),"11",LEFT(p_ym,4),"12",LEFT(p_ym,4))}'
   --  AND jd = '${SWITCH(MONTH(CONCATENATE(p_ym,"-01")),"1","四季度","2","四季度","3","四季度","4","一季度","5","一季度","6","一季度","7","二季度","8","二季度","9","二季度","10","三季度","11","三季度","12","三季度")}'
    ${IF(LEN(p_area)>0,"", "AND 1=2")}
    )
SELECT
    A.维度,
  --  A.目标,
    A.total_target,
 --   B.wl_yxybl, -- 有效样本量
  --  B.wl_myl, --  地产满意量
   -- B.wl_qz,  -- 地产权重
   -- B.mwl_yxybl,  -- 有效样本量（总样本量）
  --  B.mwl_myl,  -- 物业满意量
  --  B.mwl_qz, -- 权重80%
   -- B.得分,
    A.m,
   ((((sum(B.wl_myl) / sum(B.wl_yxybl)) *  0.2)) + ((sum(B.mwl_myl) / sum(B.mwl_yxybl)) * 0.8)) * 100 AS 新得分
FROM ${IF(LEN(p_area)>0,"T3 AS A", "T1 AS A")}
LEFT JOIN ${IF(LEN(p_area)>0,"T4 AS B", "T2 AS B")}
ON A.维度 = B.维度
and A.m = B.m
-- and A.yr = B.yr
WHERE A.目标 > 0
  AND B.得分 > 0
    ${if(left(fine_role,2)=="项目","and A.维度 = '"+GETUSERDEPARTMENTS(3)+"'","")}
GROUP BY  A.维度,A.total_target, A.m






-- 片区维度
select
a.ym as ym  -- 年月
,a.organ_code as organ_code -- 片区ID
,a.organ_name as organ_name -- 片区名称
,if(b.index_dq_target =0,0,a.cu_collection_rate / b.index_dq_target) as cu_coll_complete_rate -- 物业费当期收缴率
,if(b.index_wq_target = 0,0,a.pr_collection_rate / b.index_wq_target) as pr_coll_complete_rate -- 物业费往期收缴率
,a.collection_rate  as collection_rate-- 物业费收缴率
from
(
    SELECT

     ym
    ,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_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)) collection_rate
    from dwd_finance_fees_serial_d
    where ParentCostCode = '0001'
        --     ${if(left(fine_role,2)=="项目","and comm_name = '"+GETUSERDEPARTMENTS(3)+"'","")}
        --    ${IF(LEN(p_area)>0," AND organ_code IN ('"+JOINARRAY(p_area,"','")+"')", "")}
      ${IF(LEN(p_ym)>0," AND ym ='" + p_ym+ "'","")}
        --   ${IF(LEN(p_area)>0,"group by organ_code, ym","group by ym")}
    GROUP BY
     ym
    ,organ_code
    ,organ_name
)a
left join
(
        select
         organ_code
        ,organ_name
        ,ym
        ,index_dq_target -- 当期目标
        ,index_wq_target -- 往期目标
        from ods_finance_collection_target_area
   --     ${IF(LEN(p_area)==0,"where organ_code = '5501'","")}

)b
on a.ym = b.ym
AND a.organ_code = b.organ_code
-- ${IF(LEN(p_area)>0,"a.organ_code = b.organ_code and a.ym = b.ym", "a.ym = b.ym")}