-- Databricks notebook source
-- CREATE OR REPLACE TABLE DM.DM_TD_EXTERNAL_ORG (
--   REGION_CODE STRING,
--   region_name_en STRING,
--   RSM_Name STRING,
--   rc_code STRING,
--   rc_name_en STRING,
--   RC_Name STRING,
--   RegionCenter STRING,
--   AUDIT_COD STRING,
--   MARKET STRING,
--   bu_category STRING,
--   bu_name_en STRING,
--   sub_bu_name STRING,
--   sub_bu_code_auth STRING,
--   bu_code_auth STRING,
--   DATA_SOURCE STRING,
--   REGION_RATIO FLOAT,
--   TEAM STRING,
--   MR_TRTY_CODE STRING,
--   NSD_KCODE STRING,
--   NSD_NAME STRING,
--   RSD_KCODE STRING,
--   RSD_NAME STRING,
--   RSM_KCODE STRING,
--   DSM_KCODE STRING,
--   DSM_NAME STRING,
--   MR_KCODE STRING,
--   MR_NAME STRING)
-- USING delta
-- LOCATION 'abfss://master@retaildlstoragetest.dfs.core.chinacloudapi.cn/DM/dm_td_external_org';

-- COMMAND ----------

-- DBTITLE 1,UNIONALL_ORG
CREATE OR REPLACE TEMPORARY VIEW UNIONALL_ORG
AS
SELECT 
  REGION_CODE,
  REGION_NAME_EN,
  RSM_NAME,
  RC_CODE,
  RC_NAME_EN,
  RC_NAME,
  REGIONCENTER,
  AUDIT_COD,
  MARKET,
  BU_CATEGORY,
  BU_NAME_EN,
  SUB_BU_NAME,
  SUB_BU_CODE_AUTH,
  BU_CODE_AUTH,
  DATA_SOURCE,
  REGION_RATIO,
  TEAM,
  MR_TRTY_CODE,
  NSD_KCODE,
  NSD_NAME,
  RSD_KCODE,
  RSD_NAME,
  RSM_KCODE,
  DSM_KCODE,
  DSM_NAME,
  MR_KCODE,
  MR_NAME
FROM 
  DM.DM_TD_EXT_AIA_ORG
UNION ALL 
SELECT 
  REGION_CODE,
  REGION_NAME_EN,
  RSM_NAME,
  RC_CODE,
  RC_NAME_EN,
  RC_NAME,
  REGIONCENTER,
  AUDIT_COD,
  MARKET,
  BU_CATEGORY,
  BU_NAME_EN,
  SUB_BU_NAME,
  SUB_BU_CODE_AUTH,
  BU_CODE_AUTH,
  DATA_SOURCE,
  REGION_RATIO,
  TEAM,
  MR_TRTY_CODE,
  NSD_KCODE,
  NSD_NAME,
  RSD_KCODE,
  RSD_NAME,
  RSM_KCODE,
  DSM_KCODE,
  DSM_NAME,
  MR_KCODE,
  MR_NAME
FROM 
  DM.DM_TD_EXT_CHC_ORG
UNION ALL 
SELECT 
  REGION_CODE,
  REGION_NAME_EN,
  RSM_NAME,
  RC_CODE,
  RC_NAME_EN,
  RC_NAME,
  REGIONCENTER,
  AUDIT_COD,
  MARKET,
  BU_CATEGORY,
  BU_NAME_EN,
  SUB_BU_NAME,
  SUB_BU_CODE_AUTH,
  BU_CODE_AUTH,
  DATA_SOURCE,
  REGION_RATIO,
  TEAM,
  MR_TRTY_CODE,
  NSD_KCODE,
  NSD_NAME,
  RSD_KCODE,
  RSD_NAME,
  RSM_KCODE,
  DSM_KCODE,
  DSM_NAME,
  MR_KCODE,
  MR_NAME
FROM 
  DM.DM_TD_EXT_CHPA_ORG
UNION ALL 
SELECT 
  REGION_CODE,
  REGION_NAME_EN,
  RSM_NAME,
  RC_CODE,
  RC_NAME_EN,
  RC_NAME,
  REGIONCENTER,
  AUDIT_COD,
  MARKET,
  BU_CATEGORY,
  BU_NAME_EN,
  SUB_BU_NAME,
  SUB_BU_CODE_AUTH,
  BU_CODE_AUTH,
  DATA_SOURCE,
  REGION_RATIO,
  TEAM,
  MR_TRTY_CODE,
  NSD_KCODE,
  NSD_NAME,
  RSD_KCODE,
  RSD_NAME,
  RSM_KCODE,
  DSM_KCODE,
  DSM_NAME,
  MR_KCODE,
  MR_NAME
FROM 
  DM.DM_TD_EXT_COUNTY_ORG
UNION ALL 
SELECT 
  REGION_CODE,
  REGION_NAME_EN,
  RSM_NAME,
  RC_CODE,
  RC_NAME_EN,
  RC_NAME,
  REGIONCENTER,
  AUDIT_COD,
  MARKET,
  BU_CATEGORY,
  BU_NAME_EN,
  SUB_BU_NAME,
  SUB_BU_CODE_AUTH,
  BU_CODE_AUTH,
  DATA_SOURCE,
  REGION_RATIO,
  TEAM,
  MR_TRTY_CODE,
  NSD_KCODE,
  NSD_NAME,
  RSD_KCODE,
  RSD_NAME,
  RSM_KCODE,
  DSM_KCODE,
  DSM_NAME,
  MR_KCODE,
  MR_NAME
FROM 
  DM.DM_TD_EXT_EC_ORG
UNION ALL 
SELECT 
  REGION_CODE,
  REGION_NAME_EN,
  RSM_NAME,
  RC_CODE,
  RC_NAME_EN,
  RC_NAME,
  REGIONCENTER,
  AUDIT_COD,
  MARKET,
  BU_CATEGORY,
  BU_NAME_EN,
  SUB_BU_NAME,
  SUB_BU_CODE_AUTH,
  BU_CODE_AUTH,
  DATA_SOURCE,
  REGION_RATIO,
  TEAM,
  MR_TRTY_CODE,
  NSD_KCODE,
  NSD_NAME,
  RSD_KCODE,
  RSD_NAME,
  RSM_KCODE,
  DSM_KCODE,
  DSM_NAME,
  MR_KCODE,
  MR_NAME
FROM 
  DM.DM_TD_EXT_RETAIL_ORG
UNION ALL 
SELECT 
  REGION_CODE,
  REGION_NAME_EN,
  RSM_NAME,
  RC_CODE,
  RC_NAME_EN,
  RC_NAME,
  REGIONCENTER,
  AUDIT_COD,
  MARKET,
  BU_CATEGORY,
  BU_NAME_EN,
  SUB_BU_NAME,
  SUB_BU_CODE_AUTH,
  BU_CODE_AUTH,
  DATA_SOURCE,
  REGION_RATIO,
  TEAM,
  MR_TRTY_CODE,
  NSD_KCODE,
  NSD_NAME,
  RSD_KCODE,
  RSD_NAME,
  RSM_KCODE,
  DSM_KCODE,
  DSM_NAME,
  MR_KCODE,
  MR_NAME
FROM 
  DM.DM_TD_EXT_THC_ORG
UNION ALL 
SELECT 
  REGION_CODE,
  REGION_NAME_EN,
  RSM_NAME,
  RC_CODE,
  RC_NAME_EN,
  RC_NAME,
  REGIONCENTER,
  AUDIT_COD,
  MARKET,
  BU_CATEGORY,
  BU_NAME_EN,
  SUB_BU_NAME,
  SUB_BU_CODE_AUTH,
  BU_CODE_AUTH,
  DATA_SOURCE,
  REGION_RATIO,
  TEAM,
  MR_TRTY_CODE,
  NSD_KCODE,
  NSD_NAME,
  RSD_KCODE,
  RSD_NAME,
  RSM_KCODE,
  DSM_KCODE,
  DSM_NAME,
  MR_KCODE,
  MR_NAME
FROM 
  DM.DM_TD_EXT_XIEHE_ORG

-- COMMAND ----------

-- DBTITLE 1,overwrite dm.dm_td_external_org
insert overwrite dm.dm_td_external_org
select
REGION_CODE,
region_name_en,
RSM_Name,
rc_code,
rc_name_en,
RC_Name,
RegionCenter,
AUDIT_COD,
MARKET,
bu_category,
bu_name_en,
sub_bu_name,
sub_bu_code_auth,
bu_code_auth,
DATA_SOURCE,
REGION_RATIO,
TEAM,
MR_TRTY_CODE,
NSD_KCODE,
NSD_NAME,
RSD_KCODE,
RSD_NAME,
RSM_KCODE,
DSM_KCODE,
DSM_NAME,
MR_KCODE,
MR_NAME
from
  UNIONALL_ORG

-- COMMAND ----------

-- /*
-- 修改时间：2025.09.18
-- 修改人：chenwu
-- 修改内容：新增一个org表给merged data使用，原org表在13 external auth 中会被删除掉'BBU_County','CHC','EC','Eagle','Retail'
-- 故要在被删除前将这部分数据保留，供pbi端打目标机构标签
-- */
INSERT OVERWRITE DM.DM_TD_EXTERNAL_ORG_MERGED_DATA 
SELECT 
  REGION_CODE,
  REGION_NAME_EN,
  RSM_NAME,
  RC_CODE,
  RC_NAME_EN,
  RC_NAME,
  REGIONCENTER,
  AUDIT_COD,
  MARKET,
  BU_CATEGORY,
  BU_NAME_EN,
  SUB_BU_NAME,
  SUB_BU_CODE_AUTH,
  BU_CODE_AUTH,
  DATA_SOURCE,
  REGION_RATIO,
  TEAM,
  MR_TRTY_CODE,
  NSD_KCODE,
  NSD_NAME,
  RSD_KCODE,
  RSD_NAME,
  RSM_KCODE,
  DSM_KCODE,
  DSM_NAME,
  MR_KCODE,
  MR_NAME
FROM DM.DM_TD_EXTERNAL_ORG