DROP TABLE IF EXISTS dw.dws_cost_lc_summary_d;
CREATE TABLE IF NOT EXISTS dw.dws_cost_lc_summary_d (
 yearname          VARCHAR(50) COMMENT '年'
,monthname         VARCHAR(50) COMMENT '月'
,organ_code        VARCHAR(255) COMMENT '片区ID'
,organ_name        VARCHAR(255) COMMENT '片区名称'
,dimsubjectcode    VARCHAR(255) COMMENT '组织机构编码'
,dimsubjectname    VARCHAR(255) COMMENT '组织机构名称'
,office_rent       VARCHAR(255) COMMENT '办公室租金'
,office_repair_fee VARCHAR(255) COMMENT '办公维修费'
,office_fee        VARCHAR(255) COMMENT '办公费'
,travel_fee        VARCHAR(255) COMMENT '差旅费'
,phone_fee         VARCHAR(255) COMMENT '电话费'
,ad_fee            VARCHAR(255) COMMENT '广告宣传费'
,business_fee      VARCHAR(255) COMMENT '业务接待费'
,water_power_fee   VARCHAR(255) COMMENT '水电费'
,management_fee    VARCHAR(255) COMMENT '物管费'
,book_fee          VARCHAR(255) COMMENT '书报资料费'
,other_fee         VARCHAR(255) COMMENT '其他费用'
,total_fee         VARCHAR(255) COMMENT '当期小计'
,fee_type          VARCHAR(50) COMMENT '费用类型'
)COMMENT = '费控行政成本科目汇总表'




INSERT into dw.dws_cost_lc_summary_d
SELECT
 A.yearname                AS yearname          -- 年
,A.monthname               AS monthname         -- 月
,A.organ_code              AS organ_code        -- 片区ID
,A.organ_name              AS organ_name        -- 片区名称
,A.dimsubjectcode          AS dimsubjectcode    -- 组织机构编码
,A.dimsubjectname          AS dimsubjectname    -- 组织机构名称
,SUM(A.office_rent       ) AS office_rent       -- 办公室租金
,SUM(A.office_repair_fee ) AS office_repair_fee -- 办公维修费
,SUM(A.office_fee        ) AS office_fee        -- 办公费
,SUM(A.travel_fee        ) AS travel_fee        -- 差旅费
,SUM(A.phone_fee         ) AS phone_fee         -- 电话费
,SUM(A.ad_fee            ) AS ad_fee            -- 广告宣传费
,SUM(A.business_fee      ) AS business_fee      -- 业务接待费
,SUM(A.water_power_fee   ) AS water_power_fee   -- 水电费
,SUM(A.management_fee    ) AS management_fee    -- 物管费
,SUM(A.book_fee          ) AS book_fee          -- 书报资料费


-- ,SUM(A.insure_fee        ) AS insure_fee        -- 财产保险费
-- ,SUM(A.meeting_fee       ) AS meeting_fee       -- 会务费
-- ,SUM(A.brand_fee         ) AS brand_fee         -- 品牌费用
-- ,SUM(A.lawsuit_fee       ) AS lawsuit_fee       -- 审计、评估、咨询及讼诉费
-- ,SUM(A.amortization_fee  ) AS amortization_fee  -- 无形资产摊销
-- ,SUM(A.depreciation      ) AS depreciation      -- 折旧费
 ,SUM(A.other_fee         ) AS other_fee         -- 其他费用
 ,SUM(A.total_fee         ) AS total_fee         -- 当期小计
,A.fee_type                AS fee_type          -- 费用类型
FROM
(
    select
     yearname               -- 年
    ,monthname              -- 月
    ,organ_code             -- 片区ID
    ,organ_name             -- 片区名称
    ,dimsubjectcode         -- 组织机构编码
    ,dimsubjectname         -- 组织机构名称

    ,CASE WHEN dimaccountcode = 'WY040201'  
          THEN target_fee   
          ELSE 0 END        AS office_rent      -- 办公室租金

    ,CASE WHEN dimaccountcode = 'WY040205'  
          THEN target_fee   
          ELSE 0 END        AS office_repair_fee -- 办公维修费

    ,CASE WHEN dimaccountcode = 'WY040206'  
          THEN target_fee   
          ELSE 0 END        AS office_fee -- 办公费

    ,CASE WHEN dimaccountcode = 'WY040208'  
          THEN target_fee   
          ELSE 0 END        AS travel_fee -- 差旅费

    ,CASE WHEN dimaccountcode = 'WY040209'  
          THEN target_fee   
          ELSE 0 END        AS phone_fee-- 电话费


    ,CASE WHEN dimaccountcode = 'WY040210'  
          THEN target_fee   
          ELSE 0 END        AS ad_fee -- 广告宣传费

    ,CASE WHEN dimaccountcode = 'WY040211'  
          THEN target_fee   
          ELSE 0 END        AS business_fee  -- 业务接待费

    ,CASE WHEN dimaccountcode = 'WY040214'  
          THEN target_fee   
          ELSE 0 END        AS water_power_fee -- 水电费

    ,CASE WHEN dimaccountcode = 'WY040215'  
          THEN target_fee   
          ELSE 0 END        AS management_fee -- 物管费

    ,CASE WHEN dimaccountcode = 'WY040216'  
          THEN target_fee   
          ELSE 0 END        AS book_fee -- 书报资料费
-- -------------------------------------------------------------------------------------------
    -- 以下科目费用不在设计报告内
 --  ,CASE WHEN dimaccountcode = 'WY040203'
 --        THEN target_fee   
 --        ELSE 0 END        AS insure_fee -- 财产保险费

 --  ,CASE WHEN dimaccountcode = 'WY040213'
 --        THEN target_fee   
 --        ELSE 0 END        AS meeting_fee -- 会务费

 --  ,CASE WHEN dimaccountcode = 'WY040212'
 --        THEN target_fee   
 --        ELSE 0 END        AS brand_fee -- 品牌费用

 --  ,CASE WHEN dimaccountcode = 'WY040202'
 --        THEN target_fee   
 --        ELSE 0 END        AS lawsuit_fee -- 审计、评估、咨询及讼诉费

 --  ,CASE WHEN dimaccountcode = 'WY040204'
 --        THEN target_fee   
 --        ELSE 0 END        AS amortization_fee -- 无形资产摊销

 --  ,CASE WHEN dimaccountcode = 'WY040207'
 --        THEN target_fee   
 --        ELSE 0 END        AS depreciation -- 折旧费
-- -------------------------------------------------------------------------------------------

    ,CASE WHEN dimaccountcode in('WY040203','WY040213','WY040212','WY040202','WY040204','WY040207')
          THEN target_fee   
          ELSE 0 END        AS other_fee -- 其他费用

    ,target_fee   AS total_fee -- 当期小计
    ,'目标值'       AS fee_type -- 费用类型
    from dw.dwd_cost_detail_data_d
    where cost_code = 'LC'
    UNION ALL
    select
     yearname               -- 年
    ,monthname              -- 月
    ,organ_code             -- 片区ID
    ,organ_name             -- 片区名称
    ,dimsubjectcode         -- 组织机构编码
    ,dimsubjectname         -- 组织机构名称

    ,CASE WHEN dimaccountcode = 'WY040201'  
          THEN actual_fee   
          ELSE 0 END        AS office_rent      -- 办公室租金

    ,CASE WHEN dimaccountcode = 'WY040205'  
          THEN actual_fee   
          ELSE 0 END        AS office_repair_fee -- 办公维修费

    ,CASE WHEN dimaccountcode = 'WY040206'  
          THEN actual_fee   
          ELSE 0 END        AS office_fee -- 办公费

    ,CASE WHEN dimaccountcode = 'WY040208'  
          THEN actual_fee   
          ELSE 0 END        AS travel_fee -- 差旅费

    ,CASE WHEN dimaccountcode = 'WY040209'  
          THEN actual_fee   
          ELSE 0 END        AS phone_fee-- 电话费


    ,CASE WHEN dimaccountcode = 'WY040210'  
          THEN actual_fee   
          ELSE 0 END        AS ad_fee -- 广告宣传费

    ,CASE WHEN dimaccountcode = 'WY040211'  
          THEN actual_fee   
          ELSE 0 END        AS business_fee  -- 业务接待费

    ,CASE WHEN dimaccountcode = 'WY040214'  
          THEN actual_fee   
          ELSE 0 END        AS water_power_fee -- 水电费

    ,CASE WHEN dimaccountcode = 'WY040215'  
          THEN actual_fee   
          ELSE 0 END        AS management_fee -- 物管费

    ,CASE WHEN dimaccountcode = 'WY040216'  
          THEN actual_fee   
          ELSE 0 END        AS book_fee -- 书报资料费
-- -------------------------------------------------------------------------------------------
--   -- 以下科目费用不在设计报告内
--   ,CASE WHEN dimaccountcode = 'WY040203'
--         THEN actual_fee   
--         ELSE 0 END        AS insure_fee -- 财产保险费
--
--   ,CASE WHEN dimaccountcode = 'WY040213'
--         THEN actual_fee   
--         ELSE 0 END        AS meeting_fee -- 会务费
--
--   ,CASE WHEN dimaccountcode = 'WY040212'
--         THEN actual_fee   
--         ELSE 0 END        AS brand_fee -- 品牌费用
--
--   ,CASE WHEN dimaccountcode = 'WY040202'
--         THEN actual_fee   
--         ELSE 0 END        AS lawsuit_fee -- 审计、评估、咨询及讼诉费
--
--   ,CASE WHEN dimaccountcode = 'WY040204'
--         THEN actual_fee   
--         ELSE 0 END        AS amortization_fee -- 无形资产摊销
--
--   ,CASE WHEN dimaccountcode = 'WY040207'
--         THEN actual_fee   
--         ELSE 0 END        AS depreciation -- 折旧费
-- -------------------------------------------------------------------------------------------

    ,CASE WHEN dimaccountcode in('WY040203','WY040213','WY040212','WY040202','WY040204','WY040207')
          THEN actual_fee   
          ELSE 0 END        AS other_fee -- 其他费用

    ,actual_fee   AS total_fee -- 当期小计
    ,'实际值'       AS fee_type -- 费用类型
    from dw.dwd_cost_detail_data_d
    where cost_code = 'LC'
    UNION ALL
    select
     yearname               -- 年
    ,monthname              -- 月
    ,organ_code             -- 片区ID
    ,organ_name             -- 片区名称
    ,dimsubjectcode         -- 组织机构编码
    ,dimsubjectname         -- 组织机构名称
    ,CASE WHEN dimaccountcode = 'WY040201'  
          THEN diff_amount   
          ELSE 0 END        AS office_rent      -- 办公室租金

    ,CASE WHEN dimaccountcode = 'WY040205'  
          THEN diff_amount   
          ELSE 0 END        AS office_repair_fee -- 办公维修费

    ,CASE WHEN dimaccountcode = 'WY040206'  
          THEN diff_amount   
          ELSE 0 END        AS office_fee -- 办公费

    ,CASE WHEN dimaccountcode = 'WY040208'  
          THEN diff_amount   
          ELSE 0 END        AS travel_fee -- 差旅费

    ,CASE WHEN dimaccountcode = 'WY040209'  
          THEN diff_amount   
          ELSE 0 END        AS phone_fee-- 电话费


    ,CASE WHEN dimaccountcode = 'WY040210'  
          THEN diff_amount   
          ELSE 0 END        AS ad_fee -- 广告宣传费

    ,CASE WHEN dimaccountcode = 'WY040211'  
          THEN diff_amount   
          ELSE 0 END        AS business_fee  -- 业务接待费

    ,CASE WHEN dimaccountcode = 'WY040214'  
          THEN diff_amount   
          ELSE 0 END        AS water_power_fee -- 水电费

    ,CASE WHEN dimaccountcode = 'WY040215'  
          THEN diff_amount   
          ELSE 0 END        AS management_fee -- 物管费

    ,CASE WHEN dimaccountcode = 'WY040216'  
          THEN diff_amount   
          ELSE 0 END        AS book_fee -- 书报资料费
 -- -------------------------------------------------------------------------------------------
 --  -- 以下科目费用不在设计报告内
 --  ,CASE WHEN dimaccountcode = 'WY040203'
 --        THEN diff_amount   
 --        ELSE 0 END        AS insure_fee -- 财产保险费

 --  ,CASE WHEN dimaccountcode = 'WY040213'
 --        THEN diff_amount   
 --        ELSE 0 END        AS meeting_fee -- 会务费

 --  ,CASE WHEN dimaccountcode = 'WY040212'
 --        THEN diff_amount   
 --        ELSE 0 END        AS brand_fee -- 品牌费用

 --  ,CASE WHEN dimaccountcode = 'WY040202'
 --        THEN diff_amount   
 --        ELSE 0 END        AS lawsuit_fee -- 审计、评估、咨询及讼诉费

 --  ,CASE WHEN dimaccountcode = 'WY040204'
 --        THEN diff_amount   
 --        ELSE 0 END        AS amortization_fee -- 无形资产摊销

 --  ,CASE WHEN dimaccountcode = 'WY040207'
 --        THEN diff_amount   
 --        ELSE 0 END        AS depreciation -- 折旧费
-- -------------------------------------------------------------------------------------------

    ,CASE WHEN dimaccountcode in('WY040203','WY040213','WY040212','WY040202','WY040204','WY040207')
          THEN diff_amount   
          ELSE 0 END        AS other_fee -- 其他费用

    ,diff_amount   AS total_fee -- 当期小计
    ,'差额'       AS fee_type -- 费用类型
    from dw.dwd_cost_detail_data_d
    where cost_code = 'LC'
)A
GROUP BY
 A.yearname
,A.monthname
,A.organ_code
,A.organ_name
,A.dimsubjectcode
,A.dimsubjectname
,A.fee_type
UNION ALL
select
     yearname               -- 年
    ,monthname              -- 月
    ,organ_code             -- 片区ID
    ,organ_name             -- 片区名称
    ,dimsubjectcode         -- 组织机构编码
    ,dimsubjectname         -- 组织机构名称
    ,CASE WHEN SUM(CASE WHEN dimaccountcode = 'WY040201'  
          THEN target_fee   
          ELSE 0 END) = 0 
          THEN '0%'
          ELSE CONCAT(ROUND((SUM(CASE WHEN dimaccountcode = 'WY040201'  
          THEN actual_fee   
          ELSE 0 END) / SUM(CASE WHEN dimaccountcode = 'WY040201'  
          THEN target_fee   
          ELSE 0 END))*100,2),'%')   END    AS office_rent      -- 办公室租金

    ,CASE WHEN SUM(CASE WHEN dimaccountcode = 'WY040205'  
          THEN target_fee   
          ELSE 0 END) = 0 
          THEN '0%'
          ELSE CONCAT(ROUND((SUM(CASE WHEN dimaccountcode = 'WY040205'  
          THEN actual_fee   
          ELSE 0 END) / SUM(CASE WHEN dimaccountcode = 'WY040205'  
          THEN target_fee   
          ELSE 0 END))*100,2),'%')   END    AS office_repair_fee -- 办公维修费

    ,CASE WHEN SUM(CASE WHEN dimaccountcode = 'WY040206'  
          THEN target_fee   
          ELSE 0 END) = 0
          THEN '0%'
          ELSE CONCAT(ROUND((SUM(CASE WHEN dimaccountcode = 'WY040206'  
          THEN actual_fee   
          ELSE 0 END) / SUM(CASE WHEN dimaccountcode = 'WY040206'  
          THEN target_fee   
          ELSE 0 END))*100,2),'%')   END   AS office_fee -- 办公费

    ,CASE WHEN SUM(CASE WHEN dimaccountcode = 'WY040208'  
          THEN target_fee   
          ELSE 0 END) = 0
          THEN '0%'
          ELSE CONCAT(ROUND((SUM(CASE WHEN dimaccountcode = 'WY040208'  
          THEN actual_fee   
          ELSE 0 END) / SUM(CASE WHEN dimaccountcode = 'WY040208'  
          THEN target_fee   
          ELSE 0 END))*100,2),'%')  END        AS travel_fee -- 差旅费

    ,CASE WHEN SUM(CASE WHEN dimaccountcode = 'WY040209'  
          THEN target_fee   
          ELSE 0 END) = 0
          THEN '0%'
          ELSE CONCAT(ROUND((SUM(CASE WHEN dimaccountcode = 'WY040209'  
          THEN actual_fee   
          ELSE 0 END)  / SUM(CASE WHEN dimaccountcode = 'WY040209'  
          THEN target_fee   
          ELSE 0 END))*100,2),'%')   END      AS phone_fee-- 电话费


    ,CASE WHEN SUM(CASE WHEN dimaccountcode = 'WY040210'  
          THEN target_fee   
          ELSE 0 END) = 0
          THEN '0%'
          ELSE CONCAT(ROUND((SUM(CASE WHEN dimaccountcode = 'WY040210'  
          THEN actual_fee   
          ELSE 0 END) / SUM(CASE WHEN dimaccountcode = 'WY040210'  
          THEN target_fee   
          ELSE 0 END))*100,2),'%')  END      AS ad_fee -- 广告宣传费

    ,CASE WHEN SUM(CASE WHEN dimaccountcode = 'WY040211'  
          THEN target_fee   
          ELSE 0 END) = 0
          THEN '0%'
          ELSE  CONCAT(ROUND((SUM(CASE WHEN dimaccountcode = 'WY040211'  
          THEN actual_fee   
          ELSE 0 END) / SUM(CASE WHEN dimaccountcode = 'WY040211'  
          THEN target_fee   
          ELSE 0 END))*100,2),'%')  END        AS business_fee  -- 业务接待费

    ,CASE WHEN SUM(CASE WHEN dimaccountcode = 'WY040214'  
          THEN target_fee   
          ELSE 0 END) = 0
          THEN '0%'
          ELSE CONCAT(ROUND((SUM(CASE WHEN dimaccountcode = 'WY040214'  
          THEN actual_fee   
          ELSE 0 END)   / SUM(CASE WHEN dimaccountcode = 'WY040214'  
          THEN target_fee   
          ELSE 0 END))*100,2),'%') END      AS water_power_fee -- 水电费

    ,CASE WHEN SUM(CASE WHEN dimaccountcode = 'WY040215'  
          THEN target_fee   
          ELSE 0 END) = 0
          THEN '0%'
          ELSE CONCAT(ROUND((SUM(CASE WHEN dimaccountcode = 'WY040215'  
          THEN actual_fee   
          ELSE 0 END)   / SUM(CASE WHEN dimaccountcode = 'WY040215'  
          THEN target_fee   
          ELSE 0 END))*100,2),'%') END      AS management_fee -- 物管费

    ,CASE WHEN SUM(CASE WHEN dimaccountcode = 'WY040216'  
          THEN target_fee   
          ELSE 0 END) = 0
          THEN '0%'
          ELSE CONCAT(ROUND((SUM(CASE WHEN dimaccountcode = 'WY040216'  
          THEN actual_fee   
          ELSE 0 END) / SUM(CASE WHEN dimaccountcode = 'WY040216'  
          THEN target_fee   
          ELSE 0 END))*100,2),'%') END   AS book_fee -- 书报资料费

-- -------------------------------------------------------------------------------------------
    -- 以下科目费用不在设计报告内
  --  ,ROUND((SUM(CASE WHEN dimaccountcode = 'WY040203'
  --        THEN actual_fee   
  --        ELSE 0 END) / SUM(CASE WHEN dimaccountcode = 'WY040203'
  --        THEN target_fee   
  --        ELSE 0 END))*100,2)         AS insure_fee -- 财产保险费

  -- ,ROUND((SUM(CASE WHEN dimaccountcode = 'WY040213'
  --       THEN actual_fee   
  --       ELSE 0 END)  / SUM(CASE WHEN dimaccountcode = 'WY040213'
  --       THEN target_fee   
  --       ELSE 0 END))*100,2)        AS meeting_fee -- 会务费

  --  ,ROUND((SUM(CASE WHEN dimaccountcode = 'WY040212'
  --        THEN actual_fee   
  --        ELSE 0 END) / SUM(CASE WHEN dimaccountcode = 'WY040212'
  --        THEN target_fee   
  --        ELSE 0 END))*100,2)        AS brand_fee -- 品牌费用

  --  ,ROUND((SUM(CASE WHEN dimaccountcode = 'WY040202'
  --        THEN actual_fee   
  --        ELSE 0 END)  / SUM(CASE WHEN dimaccountcode = 'WY040202'
  --        THEN target_fee   
  --        ELSE 0 END))*100,2)      AS lawsuit_fee -- 审计、评估、咨询及讼诉费

  -- ,ROUND((SUM(CASE WHEN dimaccountcode = 'WY040204'
  --       THEN actual_fee   
  --       ELSE 0 END) /  SUM(CASE WHEN dimaccountcode = 'WY040204'
  --       THEN target_fee   
  --       ELSE 0 END))*100,2)         AS amortization_fee -- 无形资产摊销

  --  ,ROUND((SUM(CASE WHEN dimaccountcode = 'WY040207'
  --        THEN actual_fee   
  --        ELSE 0 END) / SUM(CASE WHEN dimaccountcode = 'WY040207'
  --        THEN target_fee   
  --        ELSE 0 END))*100,2)          AS depreciation -- 折旧费

-- -------------------------------------------------------------------------------------------
    ,CASE WHEN SUM(CASE WHEN dimaccountcode in('WY040203','WY040213','WY040212','WY040202','WY040204','WY040207')
          THEN target_fee   
          ELSE 0 END) = 0
          THEN '0%'
          ELSE CONCAT(ROUND((SUM(CASE WHEN dimaccountcode in('WY040203','WY040213','WY040212','WY040202','WY040204','WY040207')
          THEN actual_fee   
          ELSE 0 END)   / SUM(CASE WHEN dimaccountcode in('WY040203','WY040213','WY040212','WY040202','WY040204','WY040207')
          THEN target_fee   
          ELSE 0 END))*100,2),'%')     END    AS other_fee -- 其他费用

    ,CASE WHEN SUM(target_fee) = 0
          THEN '0%'
          ELSE CONCAT(ROUND((SUM(actual_fee) / SUM(target_fee))*100,2),'%')  END AS total_fee -- 当期小计
    ,'执行率'       AS fee_type -- 费用类型
    from dw.dwd_cost_detail_data_d
    where cost_code = 'LC'
    GROUP BY
     yearname               -- 年
    ,monthname              -- 月
    ,organ_code             -- 片区ID
    ,organ_name             -- 片区名称
    ,dimsubjectcode         -- 组织机构编码
    ,dimsubjectname         -- 组织机构名称
