drop table if exists dw.ods_innoveco_metrics_finish_d;
CREATE TABLE IF NOT EXISTS dw.ods_innoveco_metrics_finish_d (
    id INT AUTO_INCREMENT PRIMARY KEY   COMMENT '自增id'
    ,ym                 VARCHAR(50)    COMMENT '年月'
    ,organ_code         VARCHAR(50)  COMMENT '片区ID'
    ,organ_name         VARCHAR(50)    COMMENT '片区名称'
    ,business_type      VARCHAR(50)    COMMENT '业务类型'
    ,revenue_type       VARCHAR(50)    COMMENT '收入类型'
    ,revenue_value      DECIMAL(16,2)    COMMENT '收入值'
    ,update_time        datetime        COMMENT '更新时间'
    ,update_user         VARCHAR(100)   COMMENT '更新人'

)COMMENT = '多经四大业务指标完成情况填报表'
;



truncate table  dw.ods_innoveco_metrics_finish_d;

select
    id   -- 自增id'
    ,ym                 -- 年月'
     ,organ_code
    ,organ_name         -- 片区'
    ,business_type      -- 业务类型'
    ,revenue_type       -- 收入'
    ,revenue_value      -- 收入值'
,update_user
,update_time
from dw.ods_innoveco_metrics_finish_d
;


-- 【tianbao_innoveco_input】

select
    id   -- 自增id'
     ,ym                 -- 年月'
     ,organ_name         -- 片区'
     ,business_type      -- 业务类型'
     ,revenue_type       -- 收入'
     ,revenue_value      -- 收入值'
from dw.ods_innoveco_metrics_finish_d
where ym = '${p_ym}'
;



-- 【erp_片区】
SELECT
    organ_code
,organ_name
FROM
(
    SELECT DISTINCT
    organ_code AS organ_code
    ,organ_name AS organ_name
    , CASE
          WHEN organ_code = '0102' THEN 2 -- 成都片区
          WHEN organ_code = '0101' THEN 3 -- 乐山片区
          WHEN organ_code = '0115' THEN 4 -- 眉山片区
          WHEN organ_code = '0120' THEN 5 -- 攀西片区
          WHEN organ_code = '0106' THEN 6 -- 长春片区
          WHEN organ_code = '0109' THEN 7 -- 大湾片区
          WHEN organ_code = '0108' THEN 8 -- 商写片区
          WHEN organ_code = '0116' THEN 9 -- 川东北片区
          WHEN organ_code = '0103' THEN 10 -- 库尔勒片区
          WHEN organ_code = '0117' THEN 11 -- 乌鲁木齐片区
          WHEN organ_code = '0118' THEN 12 -- 喀什片区
          WHEN organ_code = '0114' THEN 13 -- 雅华片区
          else 999
                       END AS RK
FROM dim_organ_mapping
where organ_code is not null
and organ_code <> '0121'
UNION
SELECT
    DISTINCT
     cx_oragan_code AS organ_code
    , cx_oragan_name AS organ_name
    ,CASE WHEN cx_oragan_code = '6002' THEN 1
          WHEN cx_oragan_code = '6001' THEN 14
          END AS RK
FROM dim_organ_mapping
where cx_oragan_code in('6001','6002')
     )T
ORDER BY T.RK;
