
${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(p_type='人力',"","/*")}


        SELECT
            A.organ_name            AS organ_name -- 片区
            ,A.comm_name             AS comm_name -- 项目
             ,SUM(D.lc_fee_rate_target)    AS lc_fee_rate_target -- 人力费率年度目标值
             ,SUM(B.index_market_target) AS index_market_target-- 收入累计目标值
             ,SUM(A.lc_target_fee) AS lc_target_fee -- 人力成本累计目标值
             ,SUM(C.lc_fee_rate_target) AS lc_fee_rate_target -- 人力成本费率目标值
             ,SUM(B.index_income_fact)  AS index_income_fact -- 实际收入累计值
             ,SUM(A.lc_total_fee) AS lc_total_fee -- 人力成本累计实际
             ,CASE WHEN sum(B.index_income_fact) = 0 THEN 0
                   ELSE SUM(A.lc_total_fee) / SUM(B.index_income_fact) END AS fee_rate -- 费率实际值

        FROM
            (        -- 费控人力行政成本填报
                SELECT
                    organ_code  -- 片区id
                     ,organ_name -- 片区名称
                     ,comm_id   -- 项目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
                     ,comm_name
                     ,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
                       ,comm_name
                       ,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
                     ,''    AS comm_name
                     ,'153'    AS hr_organ_id  -- hr片区编码
                     ,'领悦集团总部'    AS hr_organ_name  -- hr片区名称
                     ,'153'    AS hr_comm_id    -- hr项目id
                     ,'领悦集团总部'    AS hr_comm_name  -- hr项目名称
            )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 ods_caiwu_feecollection
                WHERE category = '物业'
                 AND LEFT(yr_month,4) =  left('${p_ym}',4)
                AND yr_month <= '${p_ym}'
                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 A.organ_code = C.organ_code
            AND A.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 ym = '${p_ym}'
            )D
            ON A.organ_code = D.organ_code
                AND A.comm_id = D.comm_id
        WHERE 1 = 1
            ${IF(LEN(p_area)>0," AND A.organ_code IN ('"+JOINARRAY(p_area,"','")+"')", "")}
        GROUP BY A.organ_name
                ,A.comm_name

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


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


SELECT
    A.organ_name            AS organ_name -- 片区
     ,A.comm_name             AS comm_name -- 项目
     ,SUM(D.ac_fee_rate_target)    AS lc_fee_rate_target -- 人力费率年度目标值
     ,SUM(B.index_market_target) AS index_market_target-- 收入累计目标值
     ,SUM(A.ac_target_fee) AS lc_target_fee -- 人力成本累计目标值
     ,SUM(C.ac_fee_rate_target) AS lc_fee_rate_target -- 人力成本费率目标值
     ,SUM(B.index_income_fact)  AS index_income_fact -- 实际收入累计值
     ,SUM(A.ac_total_fee) AS lc_total_fee -- 人力成本累计实际
     ,CASE WHEN sum(B.index_income_fact) = 0 THEN 0
           ELSE SUM(A.ac_total_fee) / SUM(B.index_income_fact) END AS fee_rate -- 费率实际值

FROM
    (        -- 费控人力行政成本填报
        SELECT
            organ_code  -- 片区id
             ,organ_name -- 片区名称
             ,comm_id   -- 项目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
             ,comm_name
             ,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
               ,comm_name
               ,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
             ,''    AS comm_name
             ,'153'    AS hr_organ_id  -- hr片区编码
             ,'领悦集团总部'    AS hr_organ_name  -- hr片区名称
             ,'153'    AS hr_comm_id    -- hr项目id
             ,'领悦集团总部'    AS hr_comm_name  -- hr项目名称
    )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 ods_caiwu_feecollection
        WHERE category = '物业'
        AND LEFT(yr_month,4) =  left('${p_ym}',4)
        AND yr_month <= '${p_ym}'
        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 A.organ_code = C.organ_code
        AND A.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 ym = '${p_ym}'

    )D
    ON A.organ_code = D.organ_code
        AND A.comm_id = D.comm_id
WHERE 1 = 1
    ${IF(LEN(p_area)>0," AND A.organ_code IN ('"+JOINARRAY(p_area,"','")+"')", "")}
GROUP BY A.organ_name
        ,A.comm_name
    ${if(p_type='行政',"","*/")}
