
${if(p_type='人力',"","/*")}

SELECT
    A.organ_name   AS  organ_comm_name
     ,A.actual_cost        AS actual_cost  -- 成本
     ,B.index_income_fact  AS index_income_fact -- 实际收入
     ,ROW_NUMBER() OVER (ORDER BY  A.actual_cost / B.index_income_fact DESC ) AS 排名 -- 费率排名
     ,A.actual_cost /  B.index_income_fact AS fee_rate      -- 费率
     ,A.budget_actual_cost / B.index_income_target AS target_fee_rate -- 费率目标
FROM
    (
-- 费控人力成本科目汇总表
        SELECT
            A1.organ_code
             ,A1.organ_name
             --    ${IF(LEN(p_area)>0,"dimsubjectname", "organ_name")} AS organ_comm_name
             ,SUM(CASE WHEN fee_type IN('实际值','目标值') THEN total_fee ELSE 0 END) / 10000 AS actual_cost -- 人力成本
             ,SUM(CASE WHEN fee_type = '目标值' THEN total_fee ELSE 0 END) / 10000 AS budget_actual_cost -- 人力成本预算值
        FROM
            (
                SELECT
                    *
                FROM dw.dws_cost_ac_summary_d -- 费控人力成本科目汇总表
                WHERE CONCAT(yearname,'-',monthname) = '${LEFT(MONTHDELTA(p_ym,-1),7)}'  -- 取上月数据
            )A1
                LEFT JOIN
            (
                SELECT
                    organ_code    -- erp片区编码
                     ,organ_name  -- erp片区名称
                     ,cost_comm_id
                     ,cost_comm_name
                FROM dim_organ_mapping_kunan_tmp
                GROUP BY
                    organ_code
                       ,organ_name
                       ,cost_comm_id
                       ,cost_comm_name
            )A2
            ON A1.organ_code = A2.organ_code
        --   AND A1.dimsubjectcode = A2.cost_comm_id
        WHERE 1=1
        --     ${IF(LEN(p_area)>0," AND A2.id IN ('"+JOINARRAY(p_area,"','")+"')", "")}
        GROUP BY
            A1.organ_code
               ,A1.organ_name
        --   ${IF(LEN(p_area)>0,"dimsubjectname", "organ_name")}
    )A
        LEFT JOIN
    (-- 公司实际收入
        SELECT
             -- ${IF(LEN(p_area)>0,"comm_name", "organ_name")} AS  organ_comm_name
            organ_code
             ,SUM(index_income_fact) AS index_income_fact -- 实际收入
             ,SUM(index_income_target) AS index_income_target -- 收入目标
        FROM ods_caiwu_feecollection
        WHERE category = '物业'
          AND yr_month =  '${LEFT(MONTHDELTA(p_ym,-1),7)}'
        GROUP BY
            --  ${IF(LEN(p_area)>0,"comm_name", "organ_name")}
            organ_code
    )B
    ON A.organ_code = B.organ_code

    ${if(p_type='人力',"","*/")}

    ${if(p_type='行政',"","/*")}



SELECT
    A.organ_name   AS  organ_comm_name
     ,A.actual_cost        AS actual_cost  -- 成本
     ,B.index_income_fact  AS index_income_fact -- 实际收入
     ,ROW_NUMBER() OVER (ORDER BY  A.actual_cost / B.index_income_fact DESC ) AS 排名 -- 费率排名
     ,A.actual_cost /  B.index_income_fact AS fee_rate      -- 费率
     ,A.budget_actual_cost / B.index_income_target AS target_fee_rate -- 费率目标
FROM
    (
-- 费控人力成本科目汇总表
        SELECT
             --    ${IF(LEN(p_area)>0,"A2.comm_name", "A1.organ_name")} AS organ_comm_name
             --    ${IF(LEN(p_area)>0,",A2.comm_id", ",A1.organ_code")} AS organ_comm_code
            A1.organ_code
             ,A1.organ_name
             ,SUM(CASE WHEN fee_type IN('实际值','目标值') THEN total_fee ELSE 0 END) / 10000 AS actual_cost -- 人力成本
             ,SUM(CASE WHEN fee_type = '目标值' THEN total_fee ELSE 0 END) / 10000 AS budget_actual_cost -- 人力成本预算值

        FROM
            (
                SELECT
                    *
                FROM dw.dws_cost_lc_summary_d
                WHERE CONCAT(yearname,'-',monthname) = '${LEFT(MONTHDELTA(p_ym,-1),7)}'
            )A1
                LEFT JOIN
            (
                SELECT
                    organ_code    -- erp片区编码
                     ,organ_name  -- erp片区名称
                     ,comm_id
                     ,comm_name
                     ,cost_comm_id
                     ,cost_comm_name
                FROM dim_organ_mapping_kunan_tmp
                GROUP BY
                    organ_code
                       ,organ_name
                       ,comm_id
                       ,comm_name
                       ,cost_comm_id
                       ,cost_comm_name
            )A2
            ON A1.organ_code = A2.organ_code
        --   AND A1.dimsubjectcode = A2.cost_comm_id
        WHERE 1=1
        --  ${IF(LEN(p_area)>0," AND A2.organ_code IN ('"+JOINARRAY(p_area,"','")+"')", "")}
        GROUP BY
            A1.organ_code
               ,A1.organ_name
        --     ${IF(LEN(p_area)>0,"A2.comm_name", "A1.organ_name")}
        --   ${IF(LEN(p_area)>0,",A2.comm_id", ",A1.organ_code")}
    )A
        LEFT JOIN
    (-- 公司实际收入
        SELECT
             -- ${IF(LEN(p_area)>0,"comm_id", "organ_code")} AS  organ_comm_code
            organ_code
             ,SUM(index_income_fact) AS index_income_fact -- 实际收入
             ,SUM(index_income_target) AS index_income_target -- 收入目标
        FROM ods_caiwu_feecollection
        WHERE category = '物业'
          AND yr_month =  '${LEFT(MONTHDELTA(p_ym,-1),7)}'
        GROUP BY
            organ_code
        -- ${IF(LEN(p_area)>0,"comm_id", "organ_code")}
    )B
    ON A.organ_code  = B.organ_code

    ${if(p_type='行政',"","*/")}



-- ======================================
-- 使用填报数据来源
-- ======================================


-- 片区-人力
${if(and(len(p_area)=0,p_type='人力'),"", "/*")}
SELECT
    A.organ_name            AS organ_comm -- 片区
    ,0                      AS year_fee_rate_target -- 人力费率年度目标值
    ,SUM(ifnull(B.index_market_target,0)) AS index_market_target-- 收入累计目标值
    ,SUM(ifnull(A.lc_target_fee,0)) AS target_fee -- 人力成本累计目标值
    ,SUM(ifnull(C.lc_fee_rate_target,0)) AS fee_rate_target -- 人力成本费率目标值
    ,SUM(ifnull(B.index_income_fact,0))  AS index_income_fact -- 实际收入累计值
    ,SUM(ifnull(A.lc_total_fee,0)) AS total_fee -- 人力成本累计实际
    ,CASE WHEN sum(ifnull(B.index_income_fact,0)) = 0 THEN 0
    ELSE SUM(A.lc_total_fee) / SUM(B.index_income_fact) END AS fee_rate -- 费率实际值

        FROM
            (        -- 费控人力行政成本填报
                SELECT
                    organ_code  -- ERP片区id
                     ,organ_name -- erp 片区名称
                     ,coalesce(SUM(CASE WHEN cost_type = '人力成本' THEN actual_amount ELSE 0 END),0) AS lc_total_fee -- 人力成本费用
                     ,coalesce(SUM(CASE WHEN cost_type = '人力成本' THEN budget_amount ELSE 0 END),0) AS lc_target_fee -- 人力成本预算费用
                     ,coalesce(SUM(CASE WHEN cost_type = '行政成本' THEN actual_amount ELSE 0 END),0) AS ac_total_fee  -- 行政成本费用
                     ,coalesce(SUM(CASE WHEN cost_type = '行政成本' THEN budget_amount ELSE 0 END),0) AS ac_target_fee  -- 行政成本预算费用
                from dw.ods_cost_collection_lc_ac
                WHERE ym  >= '${p_startym}'
                AND ym <= '${p_ym}'
                GROUP BY organ_code, organ_name
            )A
            LEFT JOIN
            (
                SELECT
                    organ_code
                     ,organ_name
                     ,hr_organ_id
                     ,hr_organ_name
                FROM dim_organ_mapping
                WHERE length(hr_organ_id) > 0
                GROUP BY
                organ_code
                ,organ_name
                ,hr_organ_id
                ,hr_organ_name
                UNION ALL
                SELECT
                '0000' AS organ_code    -- erp片区编码
                ,''    AS organ_name  -- erp片区名称
                ,'153'    AS hr_organ_id  -- hr片区编码
                ,'领悦集团总部'    AS hr_organ_name  -- hr片区名称
            )A1
            ON A.organ_code = A1.organ_code
            LEFT JOIN
            (-- 公司实际收入
                SELECT
               organ_code
               ,SUM(index_market_target)  AS index_market_target
               ,SUM(index_income_fact) AS index_income_fact
               FROM
               (
                SELECT
                    organ_code
                     ,SUM(index_market_target)  AS index_market_target -- 收入目标值
                     ,SUM(index_income_fact) AS index_income_fact -- 实际收入
                FROM ods_caiwu_feecollection
                WHERE category = '物业'
                AND organ_code not in('5503','5501')
                AND yr_month >= '${p_ym}'
                AND yr_month <= '${p_startym}'
                GROUP BY
                organ_code
                UNION ALL
                    SELECT
                     '0108' AS organ_code
                     ,SUM(index_market_target)  AS index_market_target -- 收入目标值
                     ,SUM(index_income_fact) AS index_income_fact -- 实际收入
                FROM ods_caiwu_feecollection
                WHERE category = '物业'
                AND yr_month >= '${p_ym}'
                AND yr_month <= '${p_startym}'
                AND organ_code in('5503','5501') -- 集团办公区、集团总部片区归到商写片区
                )t
                group by organ_code
            )B
            ON A1.organ_code  = B.organ_code
            LEFT JOIN
            (-- 费控月度目标值
                SELECT
                    organ_code
                     ,lc_fee_rate_target    -- 人力成本费率目标
                     ,ac_fee_rate_target    -- 行政成本费率目标
                FROM ods_hr_collection_target_organ
                WHERE ym = '${p_ym}'
            )C
            ON A.organ_code = C.organ_code
           -- LEFT JOIN
           -- (-- 费控年度目标值
           --     SELECT
           --         organ_code
           --         ,lc_fee_rate_target    -- 人力成本费率目标
           --         ,ac_fee_rate_target    -- 行政成本费率目标
           --     FROM ods_hr_collection_target_organ
           --     WHERE ym = '${p_ym}'
           -- )D
           -- ON A.organ_code = D.organ_code
        GROUP BY A.organ_name


${if(and(len(p_area)=0,p_type='人力'),"", "*/")}


${if(and(len(p_area)=0,p_type='行政'),"", "/*")}

SELECT
    A.organ_name            AS organ_comm -- 片区
    ,0                      AS year_fee_rate_target -- 行政费率年度目标值
    ,SUM(ifnull(B.index_market_target,0)) AS index_market_target-- 收入累计目标值
    ,SUM(ifnull(A.ac_target_fee,0)) AS target_fee -- 行政成本累计目标值
    ,SUM(ifnull(C.ac_fee_rate_target,0)) AS fee_rate_target -- 行政成本费率目标值
    ,SUM(ifnull(B.index_income_fact,0))  AS index_income_fact -- 实际收入累计值
    ,SUM(ifnull(A.ac_total_fee,0)) AS total_fee -- 行政成本累计实际
    ,CASE WHEN sum(ifnull(B.index_income_fact,0)) = 0 THEN 0
    ELSE SUM(A.ac_total_fee) / SUM(B.index_income_fact) END AS fee_rate -- 费率实际值

        FROM
            (        -- 费控人力行政成本填报
                SELECT
                    organ_code  -- ERP片区id
                     ,organ_name -- erp 片区名称
                     ,coalesce(SUM(CASE WHEN cost_type = '人力成本' THEN actual_amount ELSE 0 END),0) AS lc_total_fee -- 人力成本费用
                     ,coalesce(SUM(CASE WHEN cost_type = '人力成本' THEN budget_amount ELSE 0 END),0) AS lc_target_fee -- 人力成本预算费用
                     ,coalesce(SUM(CASE WHEN cost_type = '行政成本' THEN actual_amount ELSE 0 END),0) AS ac_total_fee  -- 行政成本费用
                     ,coalesce(SUM(CASE WHEN cost_type = '行政成本' THEN budget_amount ELSE 0 END),0) AS ac_target_fee  -- 行政成本预算费用
                from dw.ods_cost_collection_lc_ac
                WHERE ym  >= '${p_startym}'
                AND ym <= '${p_ym}'
                GROUP BY organ_code, organ_name
            )A
            LEFT JOIN
            (
                SELECT
                    organ_code
                     ,organ_name
                     ,hr_organ_id
                     ,hr_organ_name
                FROM dim_organ_mapping
                WHERE length(hr_organ_id) > 0
                GROUP BY
                organ_code
                ,organ_name
                ,hr_organ_id
                ,hr_organ_name
                UNION ALL
                SELECT
                '0000' AS organ_code    -- erp片区编码
                ,''    AS organ_name  -- erp片区名称
                ,'153'    AS hr_organ_id  -- hr片区编码
                ,'领悦集团总部'    AS hr_organ_name  -- hr片区名称
            )A1
            ON A.organ_code = A1.organ_code
            LEFT JOIN
            (-- 公司实际收入
               SELECT
               organ_code
               ,SUM(index_market_target)  AS index_market_target
               ,SUM(index_income_fact) AS index_income_fact
               FROM
               (
                SELECT
                    organ_code
                     ,SUM(index_market_target)  AS index_market_target -- 收入目标值
                     ,SUM(index_income_fact) AS index_income_fact -- 实际收入
                FROM ods_caiwu_feecollection
                WHERE category = '物业'
                  AND organ_code not in('5503','5501')
                AND yr_month >= '${p_ym}'
                AND yr_month <= '${p_startym}'
                GROUP BY
                organ_code
                UNION ALL
                    SELECT
                     '0108' AS organ_code
                     ,SUM(index_market_target)  AS index_market_target -- 收入目标值
                     ,SUM(index_income_fact) AS index_income_fact -- 实际收入
                FROM ods_caiwu_feecollection
                WHERE category = '物业'
                AND yr_month >= '${p_ym}'
                AND yr_month <= '${p_startym}'
                AND organ_code in('5503','5501') -- 集团办公区、集团总部片区归到商写片区
                )t
                group by organ_code
            )B
            ON A1.organ_code  = B.organ_code

            LEFT JOIN
            (-- 费控月度目标值
                SELECT
                    organ_code
                     ,lc_fee_rate_target    -- 人力成本费率目标
                     ,ac_fee_rate_target    -- 行政成本费率目标
                FROM ods_hr_collection_target_organ
                WHERE ym = '${p_ym}'
            )C
            ON A.organ_code = C.organ_code
           -- LEFT JOIN
           -- (-- 费控年度目标值
           --     SELECT
           --         organ_code
           --         ,lc_fee_rate_target    -- 人力成本费率目标
           --         ,ac_fee_rate_target    -- 行政成本费率目标
           --     FROM ods_hr_collection_target_organ
           --     WHERE ym = '${p_ym}'
           -- )D
           -- ON A.organ_code = D.organ_code
        GROUP BY A.organ_name
${if(and(len(p_area)=0,p_type='行政')," -- 片区排名开始", "*/")}


${if(and(len(p_area)>0,p_type='人力'),"", "/*")}

SELECT
    A1.hr_comm_name                AS organ_comm -- 片区
    ,SUM(C.lc_fee_rate_target)   AS year_fee_rate_target -- 人力费率年度目标值
    ,SUM(ifnull(B.index_market_target,0)) AS index_market_target-- 收入累计目标值
    ,SUM(ifnull(A.lc_target_fee,0)) AS target_fee -- 人力成本累计目标值
    ,SUM(ifnull(C.lc_fee_rate_target,0)) AS fee_rate_target -- 人力成本费率目标值
    ,SUM(ifnull(B.index_income_fact,0))  AS index_income_fact -- 实际收入累计值
    ,SUM(ifnull(A.lc_total_fee,0)) AS total_fee -- 人力成本累计实际
    ,CASE WHEN sum(ifnull(B.index_income_fact,0)) = 0 THEN 0
    ELSE SUM(A.lc_total_fee) / SUM(B.index_income_fact) END AS fee_rate -- 费率实际值

        FROM
            (        -- 费控人力行政成本填报
                SELECT
                    organ_code  -- ERP片区id
                     ,organ_name -- erp 片区名称
                     ,comm_id
                     ,comm_name
                     ,coalesce(SUM(CASE WHEN cost_type = '人力成本' THEN actual_amount ELSE 0 END),0) AS lc_total_fee -- 人力成本费用
                     ,coalesce(SUM(CASE WHEN cost_type = '人力成本' THEN budget_amount ELSE 0 END),0) AS lc_target_fee -- 人力成本预算费用
                     ,coalesce(SUM(CASE WHEN cost_type = '行政成本' THEN actual_amount ELSE 0 END),0) AS ac_total_fee  -- 行政成本费用
                     ,coalesce(SUM(CASE WHEN cost_type = '行政成本' THEN budget_amount ELSE 0 END),0) AS ac_target_fee  -- 行政成本预算费用
                from dw.ods_cost_collection_lc_ac
                WHERE ym  >= '${p_startym}'
                AND ym <= '${p_ym}'
                GROUP BY organ_code, organ_name,comm_id,comm_name
            )A
            LEFT JOIN
            (
                SELECT
                    organ_code
                     ,organ_name
                     ,comm_id
                     ,hr_organ_id
                     ,hr_organ_name
                     ,hr_comm_id
                     ,hr_comm_name
                FROM dim_organ_mapping
                WHERE length(hr_comm_id) > 0
                GROUP BY
                organ_code
                ,organ_name
                ,comm_id
                ,hr_organ_id
                ,hr_organ_name
                 ,hr_comm_id
                 ,hr_comm_name
                UNION ALL
                SELECT
                '0000' AS organ_code    -- erp片区编码
                ,''    AS organ_name  -- erp片区名称
                ,''    AS comm_id
                ,'153'    AS hr_organ_id  -- hr片区编码
                ,'领悦集团总部'    AS hr_organ_name  -- hr片区名称
                ,'153'           AS hr_comm_id
                ,'领悦集团总部'    AS hr_comm_name
            )A1
            ON A.organ_code = A1.organ_code
            AND A.comm_id = A1.hr_comm_id
            LEFT JOIN
            (-- 公司实际收入
                SELECT
                organ_code
                ,comm_id
                ,sum(index_market_target) as index_market_target
                ,sum(index_income_fact) as index_income_fact
                FROM
                (
                SELECT
                    organ_code
                    ,comm_id
                     ,SUM(index_market_target)  AS index_market_target -- 收入目标值
                     ,SUM(index_income_fact) AS index_income_fact -- 实际收入
                FROM ods_caiwu_feecollection
                WHERE category = '物业'
                  AND organ_code not in('5503','5501')
                AND yr_month >= '${p_ym}'
                AND yr_month <= '${p_startym}'
                GROUP BY
                organ_code,comm_id
                       UNION ALL
                    SELECT
                     '0108' AS organ_code -- 商写片区
                     ,'215' AS comm_id
                     ,SUM(index_market_target)  AS index_market_target -- 收入目标值
                     ,SUM(index_income_fact) AS index_income_fact -- 实际收入
                FROM ods_caiwu_feecollection
                WHERE category = '物业'
                AND yr_month >= '${p_ym}'
                AND yr_month <= '${p_startym}'
                AND organ_code in('5503','5501') -- 集团办公区、集团总部片区归到商写片区
                )t
                group by organ_code ,comm_id
            )B
            ON A1.organ_code  = B.organ_code
            AND A1.comm_id = B.comm_id
            LEFT JOIN
            (-- 费控月度目标值
                SELECT
                    organ_code
                    ,comm_id
                     ,lc_fee_rate_target    -- 人力成本费率目标
                     ,ac_fee_rate_target    -- 行政成本费率目标
                FROM ods_cost_collection_target
                WHERE ym = '${p_ym}'
            )C
            ON A1.organ_code = C.organ_code
            AND A1.hr_comm_id = C.comm_id
             LEFT JOIN
             (-- 费控年度目标值
                 SELECT
                     organ_code
                     ,comm_id
                     ,lc_fee_rate_target    -- 人力成本费率目标
                     ,ac_fee_rate_target    -- 行政成本费率目标
                 FROM ods_cost_collection_target
                 WHERE length(ym) = 4
                 AND left(ym,4) = left('${p_startym}',4)
             )D
             ON A1.organ_code = D.organ_code
             AND A1.hr_comm_id = D.comm_id
             where 1 = 1
  ${IF(LEN(p_area)>0," AND A1.organ_code IN ('"+JOINARRAY(p_area,"','")+"')", "")}
        GROUP BY A1.hr_comm_name


 ${if(and(len(p_area)>0,p_type='人力'),"", "*/")}


 ${if(and(len(p_area)>0,p_type='行政'),"", "/*")}



SELECT
    A1.hr_comm_name                AS organ_comm -- 片区
    ,SUM(C.ac_fee_rate_target)   AS year_fee_rate_target -- 人力费率年度目标值
    ,SUM(ifnull(B.index_market_target,0)) AS index_market_target-- 收入累计目标值
    ,SUM(ifnull(A.ac_target_fee,0)) AS target_fee -- 人力成本累计目标值
    ,SUM(ifnull(C.ac_fee_rate_target,0)) AS fee_rate_target -- 人力成本费率目标值
    ,SUM(ifnull(B.index_income_fact,0))  AS index_income_fact -- 实际收入累计值
    ,SUM(ifnull(A.ac_total_fee,0)) AS total_fee -- 人力成本累计实际
    ,CASE WHEN sum(ifnull(B.index_income_fact,0)) = 0 THEN 0
    ELSE SUM(A.ac_total_fee) / SUM(B.index_income_fact) END AS fee_rate -- 费率实际值

        FROM
            (        -- 费控人力行政成本填报
                SELECT
                    organ_code  -- ERP片区id
                     ,organ_name -- erp 片区名称
                     ,comm_id
                     ,comm_name
                     ,coalesce(SUM(CASE WHEN cost_type = '人力成本' THEN actual_amount ELSE 0 END),0) AS lc_total_fee -- 人力成本费用
                     ,coalesce(SUM(CASE WHEN cost_type = '人力成本' THEN budget_amount ELSE 0 END),0) AS lc_target_fee -- 人力成本预算费用
                     ,coalesce(SUM(CASE WHEN cost_type = '行政成本' THEN actual_amount ELSE 0 END),0) AS ac_total_fee  -- 行政成本费用
                     ,coalesce(SUM(CASE WHEN cost_type = '行政成本' THEN budget_amount ELSE 0 END),0) AS ac_target_fee  -- 行政成本预算费用
                from dw.ods_cost_collection_lc_ac
                WHERE ym  >= '${p_startym}'
                AND ym <= '${p_ym}'
                GROUP BY organ_code, organ_name,comm_id,comm_name
            )A
            LEFT JOIN
            (
                SELECT
                    organ_code
                     ,organ_name
                     ,comm_id
                     ,hr_organ_id
                     ,hr_organ_name
                     ,hr_comm_id
                     ,hr_comm_name
                FROM dim_organ_mapping
                WHERE length(hr_comm_id) > 0
                GROUP BY
                organ_code
                ,organ_name
                ,comm_id
                ,hr_organ_id
                ,hr_organ_name
                 ,hr_comm_id
                 ,hr_comm_name
                UNION ALL
                SELECT
                '0000' AS organ_code    -- erp片区编码
                ,''    AS organ_name  -- erp片区名称
                ,''    AS comm_id
                ,'153'    AS hr_organ_id  -- hr片区编码
                ,'领悦集团总部'    AS hr_organ_name  -- hr片区名称
                ,'153'           AS hr_comm_id
                ,'领悦集团总部'    AS hr_comm_name
            )A1
            ON A.organ_code = A1.organ_code
            AND A.comm_id = A1.hr_comm_id
            LEFT JOIN
            (-- 公司实际收入
                SELECT
                organ_code
                ,comm_id
                ,sum(index_market_target) as index_market_target
                ,sum(index_income_fact) as index_income_fact
                FROM
                (
                SELECT
                    organ_code
                    ,comm_id
                     ,SUM(index_market_target)  AS index_market_target -- 收入目标值
                     ,SUM(index_income_fact) AS index_income_fact -- 实际收入
                FROM ods_caiwu_feecollection
                WHERE category = '物业'
                AND yr_month >= '${p_ym}'
                AND yr_month <= '${p_startym}'
                AND organ_code not in('5503','5501')
                GROUP BY
                organ_code,comm_id
                       UNION ALL
                    SELECT
                     '0108' AS organ_code -- 商写片区
                     ,'215' AS comm_id
                     ,SUM(index_market_target)  AS index_market_target -- 收入目标值
                     ,SUM(index_income_fact) AS index_income_fact -- 实际收入
                FROM ods_caiwu_feecollection
                WHERE category = '物业'
                AND yr_month >= '${p_ym}'
                AND yr_month <= '${p_startym}'
                AND organ_code in('5503','5501') -- 集团办公区、集团总部片区归到商写片区
                )t
                group by organ_code ,comm_id
            )B
            ON A1.organ_code  = B.organ_code
            AND A1.comm_id = B.comm_id
            LEFT JOIN
            (-- 费控月度目标值
                SELECT
                    organ_code
                    ,comm_id
                     ,lc_fee_rate_target    -- 人力成本费率目标
                     ,ac_fee_rate_target    -- 行政成本费率目标
                FROM ods_cost_collection_target
                WHERE ym = '${p_ym}'
            )C
            ON A1.organ_code = C.organ_code
            AND A1.hr_comm_id = C.comm_id
             LEFT JOIN
             (-- 费控年度目标值
                 SELECT
                     organ_code
                     ,comm_id
                     ,lc_fee_rate_target    -- 人力成本费率目标
                     ,ac_fee_rate_target    -- 行政成本费率目标
                 FROM ods_cost_collection_target
                 WHERE length(ym) = 4
                 AND left(ym,4) = left('${p_startym}',4)
             )D
             ON A1.organ_code = D.organ_code
             AND A1.hr_comm_id = D.comm_id
             where 1 = 1
  ${IF(LEN(p_area)>0," AND A1.organ_code IN ('"+JOINARRAY(p_area,"','")+"')", "")}
        GROUP BY A1.hr_comm_name


${if(and(len(p_area)>0,p_type='行政'),"", "*/")}



${if(p_type='行政',"", "/*")}

SELECT
ac_fee_rate_target     as fee_rate_target -- 人力成本费率目标
FROM ods_hr_collection_target_overall
WHERE ym = '${p_ym}'
${if(p_type='行政',"", "*/")}


${if(p_type='人力',"", "/*")}
SELECT
lc_fee_rate_target     as fee_rate_target -- 行政成本费率目标
FROM ods_hr_collection_target_overall
WHERE ym = '${p_ym}'
${if(p_type='人力',"", "*/")}