Files
MarketAnalysis-ETL/13 external auth.sql

1230 lines
39 KiB
SQL
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- Databricks notebook source
-- log 20250226 auth:庄伟
-- MA和AIA权限表改造如下
--1.MA销售权限表dm.dm_auth_sales_ta通过辖区中销售负责的产品Family找到对应的TA,然后按照权限矩阵的限制对用户所能看到的BU进行筛选
-- Market Analysis_CHPA 全BU可看
-- Market Analysis_County 除BUEC以外可以看
-- Market Analysis_CHC 除BU:EC&RDU 以外可以看
-- Market Analysis_THC 除BU:EC&RDU 以外可以看
-- Market Analysis_Retail/DTP 除BU:EC&RDU 以外可以看
-- Market Analysis_EC 仅EC可看 --修改 全BU可看
-- Market Analysis_AIA 除BUEC以外可以看
-- Market Analysis_科室市场数据 全BU可看
--2.MA和AIA非销售权限表,按照workmate和mds用户申请的权限看
-- dm.dm_auth_nonsales_ta 直接按照用户申请的TA权限查看
-- dm.dm_auth_nonsales_org 根据用户申请的Sub_bu/BU转换为TA权限查看
-- log 20251105 auth:chenwu
-- MA和AIA权限范围修改 dm.dm_auth_sales_ta
-- Market Analysis_AIA 除BUECRetail以外可以看
-- Market Analysis_科室市场数据 除BUEC以外可以看
-- Market Analysis_Retail/DTP 除BU: EC以外可以看
-- COMMAND ----------
--for ec into retail
create or replace temporary view dm_td_org_temp
as
SELECT org_key,mr_trty_tag,district_code,rsm_name,rc_name,p2.region_name,p2.rsd_kcode,p2.rsd_name
,CASE WHEN p2.yyyymm >= '202601' and p2.bu_code = 'PC12' then 'PC09' else p2.bu_code end bu_code
,CASE WHEN p2.yyyymm >= '202601' and p2.bu_report = 'EC' THEN 'Retail' ELSE p2.bu_report end bu_report
,CASE WHEN p2.yyyymm >= '202601' and p2.sub_bu_code = 'NA27' then 'NA06' else p2.sub_bu_code end sub_bu_code
,CASE WHEN p2.yyyymm >= '202601' and p2.sub_bu_name = 'EC' then 'Retail' else p2.sub_bu_name end sub_bu_name
,CASE WHEN p2.yyyymm >= '202601' and p2.sub_bu_name_en = 'EC' then 'Retail' else p2.sub_bu_name_en end sub_bu_name_en
,CASE WHEN p2.yyyymm >= '202601' and p2.bu_name = 'EC' then 'Retail' else p2.bu_name end bu_name
,CASE WHEN p2.yyyymm >= '202601' and p2.nsd_kcode = 'ND_EC0001_V' then 'KSGX559' else p2.nsd_kcode end nsd_kcode
,CASE WHEN p2.yyyymm >= '202601' and p2.nsd_trty_code = 'ND_EC0001' then 'ND_RT0001' else p2.nsd_trty_code end nsd_trty_code
,CASE WHEN p2.yyyymm >= '202601' and p2.bsd_kcode = 'VP_EC0001_V' then 'VP_RT0001_V' else p2.bsd_kcode end bsd_kcode
,CASE WHEN p2.yyyymm >= '202601' and p2.bsd_trty_code = 'VP_EC0001' then 'VP_RT0001' else p2.bsd_trty_code end bsd_trty_code
,p2.rsm_kcode,p2.rc_code,p2.rc_name_en,p2.region_code,p2.region_name_en,BU_CATEGORY,yyyymm,mr_trty_code
,mr_kcode,dsm_trty_code,rsm_trty_code,dsm_kcode,rsd_trty_code
FROM DM.dm_td_org P2
-- COMMAND ----------
------销售权限
---从C月辖区中提取USER与TA映射
drop view if exists tmp_auth_sales_user_ta_mapping;
create temporary view tmp_auth_sales_user_ta_mapping
as
(
select
bu_active_list.bm_name,
org.bu_code,
org.bu_name,
org.sub_bu_code,
org.sub_bu_name,
org.dsm_trty_code,
org.dsm_kcode,
org.rsm_trty_code,
org.rsm_kcode,
org.rsd_trty_code,
org.rsd_kcode,
org.nsd_trty_code,
org.nsd_kcode,
org.rc_code,
prod.ta_code,
case when prod.ta_name ='ONC' then 'ONCO' else prod.ta_name end ta_name
from
(
select distinct
yyyymm,
trty_code,
family_code
from dm.dm_td_sd_territory_indication
where yyyymm in(
select key_value as yyyymm
from dm.dm_td_date_config
where key_code = 'pfmc_cvh'
) and bu not in ('CUBE','KA') and family_code not in ('RTRT','ECEC')
and key_hp<>3 --chenwu 20250904 排除双考影响
)trty_ind_c
left join dm.dm_td_product prod
on trty_ind_c.family_code = prod.prd_key
left join dm_td_org_temp org
on trty_ind_c.yyyymm = org.yyyymm and trty_ind_c.trty_code = org.mr_trty_code
left join dws.dws_td_bu_active bu_active_list
on org.bu_name =bu_active_list.bu_name
);
-- COMMAND ----------
insert overwrite table dm.dm_auth_sales_ta
select
bu_name,
user_trty_code,
user_kcode,
ta,
role,
data_source,
concat(ta,data_source) ta_source,
from_utc_timestamp(current_timestamp,'UTC+8') as etl_insert_dt,
from_utc_timestamp(current_timestamp,'UTC+8') as etl_update_dt
from
(
----chpa 1
select
bu_name,
user_trty_code,
user_kcode,
ta,
role,
'IQVIA-CHPA(Monthly)' data_source
from
(
--dsm
select
bu_name,
dsm_trty_code user_trty_code,
dsm_kcode user_kcode,
ta_name ta,
'dsm' role
from tmp_auth_sales_user_ta_mapping
union
--rsm
select
bu_name,
rsm_trty_code user_trty_code,
rsm_kcode user_kcode,
ta_name ta,
'rsm' role
from tmp_auth_sales_user_ta_mapping
union
--rsd
select
bu_name,
rsd_trty_code user_trty_code,
rsd_kcode user_kcode,
ta_name ta,
'rsd' role
from tmp_auth_sales_user_ta_mapping
union
--nsd
select
bu_name,
nsd_trty_code user_trty_code,
nsd_kcode user_kcode,
ta_name ta,
'nsd' role
from tmp_auth_sales_user_ta_mapping
)auth_chpa
----county 2
union
select
bu_name,
user_trty_code,
user_kcode,
ta,
role,
'IQVIA-COUNTY(Quarterly)' data_source
from
(
--dsm
select
bu_name,
dsm_trty_code user_trty_code,
dsm_kcode user_kcode,
ta_name ta,
'dsm' role
from tmp_auth_sales_user_ta_mapping
where
-- bu_name not in ('EC')
not (bu_name in ('Retail') and rc_code in ('RC198')) --架构中 EC 合并到 Retail 调整 20260127
union
--rsm
select
bu_name,
rsm_trty_code user_trty_code,
rsm_kcode user_kcode,
ta_name ta,
'rsm' role
from tmp_auth_sales_user_ta_mapping
where
-- bu_name not in ('EC')
not (bu_name in ('Retail') and rc_code in ('RC198')) --架构中 EC 合并到 Retail 调整 20260127
union
--rsd
select
bu_name,
rsd_trty_code user_trty_code,
rsd_kcode user_kcode,
ta_name ta,
'rsd' role
from tmp_auth_sales_user_ta_mapping
where
-- bu_name not in ('EC')
not (bu_name in ('Retail') and rc_code in ('RC198')) --架构中 EC 合并到 Retail 调整 20260127
union
--nsd
select
bu_name,
nsd_trty_code user_trty_code,
nsd_kcode user_kcode,
ta_name ta,
'nsd' role
from tmp_auth_sales_user_ta_mapping
where
-- bu_name not in ('EC')
not (bu_name in ('Retail') and rc_code in ('RC198')) --架构中 EC 合并到 Retail 调整 20260127
)auth_county
----chc 3
union
select
bu_name,
user_trty_code,
user_kcode,
ta,
role,
'CHC(Quarterly)' data_source
from
(
--dsm
select
bu_name,
dsm_trty_code user_trty_code,
dsm_kcode user_kcode,
ta_name ta,
'dsm' role
from tmp_auth_sales_user_ta_mapping
where
bu_name not in ('RDU')
and not (bu_name in ('Retail') and rc_code in ('RC198')) --架构中 EC 合并到 Retail 调整 20260127
union
--rsm
select
bu_name,
rsm_trty_code user_trty_code,
rsm_kcode user_kcode,
ta_name ta,
'rsm' role
from tmp_auth_sales_user_ta_mapping
where
bu_name not in ('RDU')
and not (bu_name in ('Retail') and rc_code in ('RC198')) --架构中 EC 合并到 Retail 调整 20260127
union
--rsd
select
bu_name,
rsd_trty_code user_trty_code,
rsd_kcode user_kcode,
ta_name ta,
'rsd' role
from tmp_auth_sales_user_ta_mapping
where --bu_name not in ('RDU','EC')
bu_name not in ('RDU')
and not (bu_name in ('Retail') and rc_code in ('RC198')) --架构中 EC 合并到 Retail 调整 20260127
union
--nsd
select
bu_name,
nsd_trty_code user_trty_code,
nsd_kcode user_kcode,
ta_name ta,
'nsd' role
from tmp_auth_sales_user_ta_mapping
where --bu_name not in ('RDU','EC')
bu_name not in ('RDU')
and not (bu_name in ('Retail') and rc_code in ('RC198')) --架构中 EC 合并到 Retail 调整 20260127
)auth_chc
----thc 4
union
select
bu_name,
user_trty_code,
user_kcode,
ta,
role,
'THC(Quarterly)' data_source
from
(
--dsm
select
bu_name,
dsm_trty_code user_trty_code,
dsm_kcode user_kcode,
ta_name ta,
'dsm' role
from tmp_auth_sales_user_ta_mapping
where --bu_name not in ('RDU','EC')
bu_name not in ('RDU') and not (bu_name in ('Retail') and rc_code in ('RC198')) --架构中 EC 合并到 Retail 调整 20260127
union
--rsm
select
bu_name,
rsm_trty_code user_trty_code,
rsm_kcode user_kcode,
ta_name ta,
'rsm' role
from tmp_auth_sales_user_ta_mapping
where --bu_name not in ('RDU','EC')
bu_name not in ('RDU')
and not (bu_name in ('Retail') and rc_code in ('RC198')) --架构中 EC 合并到 Retail 调整 20260127
union
--rsd
select
bu_name,
rsd_trty_code user_trty_code,
rsd_kcode user_kcode,
ta_name ta,
'rsd' role
from tmp_auth_sales_user_ta_mapping
where --bu_name not in ('RDU','EC')
bu_name not in ('RDU')
and not (bu_name in ('Retail') and rc_code in ('RC198')) --架构中 EC 合并到 Retail 调整 20260127
union
--nsd
select
bu_name,
nsd_trty_code user_trty_code,
nsd_kcode user_kcode,
ta_name ta,
'nsd' role
from tmp_auth_sales_user_ta_mapping
where --bu_name not in ('RDU','EC')
bu_name not in ('RDU')
and not (bu_name in ('Retail') and rc_code in ('RC198')) --架构中 EC 合并到 Retail 调整 20260127
)auth_thc
union
----retail 5
select
bu_name,
user_trty_code,
user_kcode,
ta,
role,
'Retail(Quarterly)' data_source
from
(
--dsm
select
bu_name,
dsm_trty_code user_trty_code,
dsm_kcode user_kcode,
ta_name ta,
'dsm' role
from tmp_auth_sales_user_ta_mapping
where --bu_name not in ('EC')
not (bu_name in ('Retail') and rc_code in ('RC198')) --架构中 EC 合并到 Retail 调整 20260127
union
--rsm
select
bu_name,
rsm_trty_code user_trty_code,
rsm_kcode user_kcode,
ta_name ta,
'rsm' role
from tmp_auth_sales_user_ta_mapping
where --bu_name not in ('EC')
not (bu_name in ('Retail') and rc_code in ('RC198')) --架构中 EC 合并到 Retail 调整 20260127
union
--rsd
select
bu_name,
rsd_trty_code user_trty_code,
rsd_kcode user_kcode,
ta_name ta,
'rsd' role
from tmp_auth_sales_user_ta_mapping
where --bu_name not in ('EC')
not (bu_name in ('Retail') and rc_code in ('RC198')) --架构中 EC 合并到 Retail 调整 20260127
union
--nsd
select
bu_name,
nsd_trty_code user_trty_code,
nsd_kcode user_kcode,
ta_name ta,
'nsd' role
from tmp_auth_sales_user_ta_mapping
where --bu_name not in ('EC')
not (bu_name in ('Retail') and rc_code in ('RC198')) --架构中 EC 合并到 Retail 调整 20260127
)auth_retail
----ec 6
union
select
bu_name,
user_trty_code,
user_kcode,
ta,
role,
'EC(Monthly)' data_source
from
(
--dsm
select
bu_name,
dsm_trty_code user_trty_code,
dsm_kcode user_kcode,
ta_name ta,
'dsm' role
from tmp_auth_sales_user_ta_mapping
-- where --bu_name ='EC'
-- -- (bu_name in ('Retail') and rc_code in ('RC198')) --架构中 EC 合并到 Retail 调整 20260127
-- not (bu_name in ('Retail') and rc_code in ('RC198')) -- EC 改成和CHPA一样 全BU可看 20260305 yusuo
union
--rsm
select
bu_name,
rsm_trty_code user_trty_code,
rsm_kcode user_kcode,
ta_name ta,
'rsm' role
from tmp_auth_sales_user_ta_mapping
-- where --bu_name ='EC'
-- -- (bu_name in ('Retail') and rc_code in ('RC198')) --架构中 EC 合并到 Retail 调整 20260127
-- not (bu_name in ('Retail') and rc_code in ('RC198')) -- EC 改成和CHPA一样 全BU可看 20260305 yusuo
union
--rsd
select
bu_name,
rsd_trty_code user_trty_code,
rsd_kcode user_kcode,
ta_name ta,
'rsd' role
from tmp_auth_sales_user_ta_mapping
-- where --bu_name ='EC'
-- -- (bu_name in ('Retail') and rc_code in ('RC198')) --架构中 EC 合并到 Retail 调整 20260127
-- not (bu_name in ('Retail') and rc_code in ('RC198')) -- EC 改成和CHPA一样 全BU可看 20260305 yusuo
union
--nsd
select
bu_name,
nsd_trty_code user_trty_code,
nsd_kcode user_kcode,
ta_name ta,
'nsd' role
from tmp_auth_sales_user_ta_mapping
-- where --bu_name ='EC'
-- -- (bu_name in ('Retail') and rc_code in ('RC198')) --架构中 EC 合并到 Retail 调整 20260127
-- not (bu_name in ('Retail') and rc_code in ('RC198')) -- EC 改成和CHPA一样 全BU可看 20260305 yusuo
)auth_ec
----aia 7
union
select
bu_name,
user_trty_code,
user_kcode,
ta,
role,
'AIA(Monthly)' data_source
from
(
--dsm
select
bu_name,
dsm_trty_code user_trty_code,
dsm_kcode user_kcode,
ta_name ta,
'dsm' role
from tmp_auth_sales_user_ta_mapping
where bu_name not in ('Retail','EC')
union
--rsm
select
bu_name,
rsm_trty_code user_trty_code,
rsm_kcode user_kcode,
ta_name ta,
'rsm' role
from tmp_auth_sales_user_ta_mapping
where bu_name not in ('Retail','EC')
union
--rsd
select
bu_name,
rsd_trty_code user_trty_code,
rsd_kcode user_kcode,
ta_name ta,
'rsd' role
from tmp_auth_sales_user_ta_mapping
where bu_name not in ('Retail','EC')
union
--nsd
select
bu_name,
nsd_trty_code user_trty_code,
nsd_kcode user_kcode,
ta_name ta,
'nsd' role
from tmp_auth_sales_user_ta_mapping
where bu_name not in ('Retail','EC')
)auth_aia
----xiehe 8
union
select
bu_name,
user_trty_code,
user_kcode,
ta,
role,
'XH Data(Quarterly)' data_source
from
(
--dsm
select
bu_name,
dsm_trty_code user_trty_code,
dsm_kcode user_kcode,
ta_name ta,
'dsm' role
from tmp_auth_sales_user_ta_mapping
where --bu_name not in ('EC')
not (bu_name in ('Retail') and rc_code in ('RC198')) --架构中 EC 合并到 Retail 调整 20260127
union
--rsm
select
bu_name,
rsm_trty_code user_trty_code,
rsm_kcode user_kcode,
ta_name ta,
'rsm' role
from tmp_auth_sales_user_ta_mapping
where --bu_name not in ('EC')
not (bu_name in ('Retail') and rc_code in ('RC198')) --架构中 EC 合并到 Retail 调整 20260127
union
--rsd
select
bu_name,
rsd_trty_code user_trty_code,
rsd_kcode user_kcode,
ta_name ta,
'rsd' role
from tmp_auth_sales_user_ta_mapping
where --bu_name not in ('EC')
not (bu_name in ('Retail') and rc_code in ('RC198')) --架构中 EC 合并到 Retail 调整 20260127
union
--nsd
select
bu_name,
nsd_trty_code user_trty_code,
nsd_kcode user_kcode,
ta_name ta,
'nsd' role
from tmp_auth_sales_user_ta_mapping
where --bu_name not in ('EC')
not (bu_name in ('Retail') and rc_code in ('RC198')) --架构中 EC 合并到 Retail 调整 20260127
)auth_xiehe
)auth_sales_ta
----DTP 9
union all
select
bu_name,
user_trty_code,
user_kcode,
ta,
role,
'DTP(Quarterly)' data_source,
concat(ta,data_source) ta_source,
from_utc_timestamp(current_timestamp,'UTC+8') as etl_insert_dt,
from_utc_timestamp(current_timestamp,'UTC+8') as etl_update_dt
from
(
--dsm
select
bu_name,
dsm_trty_code user_trty_code,
dsm_kcode user_kcode,
ta_name ta,
'dsm' role
from tmp_auth_sales_user_ta_mapping
where --bu_name not in ('EC')
not (bu_name in ('Retail') and rc_code in ('RC198')) --架构中 EC 合并到 Retail 调整 20260127
union
--rsm
select
bu_name,
rsm_trty_code user_trty_code,
rsm_kcode user_kcode,
ta_name ta,
'rsm' role
from tmp_auth_sales_user_ta_mapping
where --bu_name not in ('EC')
not (bu_name in ('Retail') and rc_code in ('RC198')) --架构中 EC 合并到 Retail 调整 20260127
union
--rsd
select
bu_name,
rsd_trty_code user_trty_code,
rsd_kcode user_kcode,
ta_name ta,
'rsd' role
from tmp_auth_sales_user_ta_mapping
where --bu_name not in ('EC')
not (bu_name in ('Retail') and rc_code in ('RC198')) --架构中 EC 合并到 Retail 调整 20260127
union
--nsd
select
bu_name,
nsd_trty_code user_trty_code,
nsd_kcode user_kcode,
ta_name ta,
'nsd' role
from tmp_auth_sales_user_ta_mapping
where --bu_name not in ('EC')
not (bu_name in ('Retail') and rc_code in ('RC198')) --架构中 EC 合并到 Retail 调整 20260127
)auth_retail
;
-- COMMAND ----------
-----非销售权限 用户申请的TA维度权限
insert overwrite table dm.dm_auth_nonsales_ta
select
user_kcode,
user_role,
ta_code,
case when ta_name ='ONC' then 'ONCO' else ta_name end ta_name,
from_utc_timestamp(current_timestamp,'UTC+8') as etl_insert_dt,
from_utc_timestamp(current_timestamp,'UTC+8') as etl_update_dt
from
(
select distinct user_kcode,role user_role,ta_code,ta_name
from dm.dm_td_user_permission_derived
where nvl(ta_name,'')<>''
)nonsales_ta;
-- COMMAND ----------
drop view if exists tmp_auth_sales_bu_ta_mapping;
create temporary view tmp_auth_sales_bu_ta_mapping
as
(
select distinct bu_code,bu_name,sub_bu_code,sub_bu_name,ta_code,ta_name
from tmp_auth_sales_user_ta_mapping
);
-- COMMAND ----------
-----用户申请的BU维度权限转换为架构中负责的TA
insert overwrite table dm.dm_auth_nonsales_org
select
coalesce(subbu_ta_map.bu_code,nonsales_subbu.bu_code) bu_code,
coalesce(subbu_ta_map.bu_name,nonsales_subbu.bu_name) bu_name,
nonsales_subbu.user_kcode,
nonsales_subbu.user_role,
coalesce(subbu_ta_map.sub_bu_code,nonsales_subbu.sub_bu_code) sub_bu_code,
coalesce(subbu_ta_map.sub_bu_name,nonsales_subbu.sub_bu_name) sub_bu_name,
subbu_ta_map.ta_code,
subbu_ta_map.ta_name,
'SUB_BU' row_tag,
from_utc_timestamp(current_timestamp,'UTC+8') as etl_insert_dt,
from_utc_timestamp(current_timestamp,'UTC+8') as etl_update_dt
from
(
select
distinct
user_kcode,
role user_role,
bu_code,
bu_name,
sub_bu_code,
sub_bu_name
from dm.dm_td_user_permission_detail
where nvl(sub_bu_code,'')<>''
)nonsales_subbu
left join
(
select bu_code,bu_name,sub_bu_code,sub_bu_name,ta_code,ta_name
from tmp_auth_sales_bu_ta_mapping
)subbu_ta_map
on nonsales_subbu.sub_bu_code = subbu_ta_map.sub_bu_code
union
select
nonsales_bu.user_kcode,
nonsales_bu.user_role,
coalesce(bu_ta_map.bu_code,nonsales_bu.bu_code) bu_code,
coalesce(bu_ta_map.bu_name,nonsales_bu.bu_name) bu_name,
bu_ta_map.sub_bu_code,
bu_ta_map.sub_bu_name,
bu_ta_map.ta_code,
bu_ta_map.ta_name,
'BU' row_tag,
from_utc_timestamp(current_timestamp,'UTC+8') as etl_insert_dt,
from_utc_timestamp(current_timestamp,'UTC+8') as etl_update_dt
from
(
select
distinct
user_kcode,
role user_role,
bu_code,
bu_name
from dm.dm_td_user_permission_detail
where nvl(sub_bu_code,'')='' and nvl(bu_code,'')<>''
)nonsales_bu
left join
(
select distinct bu_code,bu_name,sub_bu_code,sub_bu_name,ta_code,ta_name
from tmp_auth_sales_bu_ta_mapping
)bu_ta_map
on nonsales_bu.bu_code = bu_ta_map.bu_code;
-- COMMAND ----------
-- DBTITLE 1,old version
-- insert overwrite dm.dm_auth_geo(KCODE,CITY_CODE)
-- select distinct
-- t1.k_account as KCODE,
-- t2.AUDIT_COD as CITY_CODE
-- from (
-- select K_ACCOUNT,
-- USER_NAME,
-- Province_GEO
-- from dm.DM_TD_SPECIAL_GROUP
-- where ROLE = 'PI_GEO_IMS'
-- and length(Province_GEO) > 0
-- ) t1
-- join
-- (
-- select distinct
-- PROVINCE_C,
-- AUDIT_COD
-- from dm.DM_IMS_TD_GEO
-- where length(AUDIT_COD) > 0
-- ) t2
-- on t1.Province_GEO = t2.PROVINCE_C
-- COMMAND ----------
-- insert overwrite dm.dm_auth_org(KCODE,SUB_BU_CODE)
-- select
-- t1.user_kcode as KCODE,
-- t2.SUB_BU_CODE as SUB_BU_CODE
-- from (
-- select K_ACCOUNT as user_kcode,
-- bu_org as BU_NAME
-- from dm.DM_TD_SPECIAL_GROUP
-- where ROLE = 'PI_ORG_IMS'
-- and length(bu_org) > 0
-- ) t1
-- join (
-- select distinct
-- BU_NAME,
-- SUB_BU_CODE
-- from DM.DM_TD_ORG
-- ) t2
-- on t1.BU_NAME = t2.BU_NAME
-- COMMAND ----------
-- ---------------------------------------------------------------------------
-- --修改时间20241112
-- --修改人Fanxujia
-- --修改内容:
-- --for AIA Dashboard:
-- --1MA报告没有权限旧AIA报告有权限增加旧AIA报告的权限人员以旧AIA报告的范围开权限by TA
-- --2MA报告有权限旧AIA报告也有权限以旧AIA报告权限范围为准
-- --3MA报告有权限旧AIA报告没有权限保持不变
-- ---------------------------------------------------------------------------
-- with AIA_USERS as (
-- select
-- distinct gu.k_account as KCODE,
-- upper(rg.ta_prd) as TA_PRD
-- from
-- dm.DM_TD_GROUP_USERS gu
-- join dm.DM_TD_REPORT_GROUP rg on gu.user_team = rg.user_group
-- where
-- rg.role = 'PI_PRD_CPA'
-- and length(rg.ta_prd) > 0
-- union
-- SELECT distinct
-- K_ACCOUNT as KCODE,
-- upper(TA_PRD) as TA_PRD
-- FROM
-- dm.DM_TD_SPECIAL_GROUP
-- WHERE
-- ROLE = 'PI_PRD_CPA'
-- AND length(TA_PRD) > 0
-- )
-- ,CHPA_USERS as (
-- select
-- distinct gu.k_account as KCODE,
-- upper(rg.ta_prd) as TA_PRD
-- from
-- dm.DM_TD_GROUP_USERS gu
-- join dm.DM_TD_REPORT_GROUP rg on gu.user_team = rg.user_group
-- where
-- rg.role = 'PI_PRD_IMS'
-- and length(rg.ta_prd) > 0
-- union
-- SELECT distinct
-- K_ACCOUNT as KCODE,
-- upper(TA_PRD) as TA_PRD
-- FROM
-- dm.DM_TD_SPECIAL_GROUP
-- WHERE
-- ROLE = 'PI_PRD_IMS'
-- AND length(TA_PRD) > 0
-- )
-- ,ALL_USERS as (
-- --单独CHPA用户
-- select distinct
-- KCODE,
-- TA_PRD
-- from CHPA_USERS
-- where KCODE not in (
-- select KCODE from AIA_USERS
-- )
-- union
-- --1单独AIA用户
-- --2同时存在于CHPA以及AIA的用户以AIA的权限为准
-- select distinct
-- KCODE,
-- TA_PRD
-- from AIA_USERS
-- )
-- ,TA_MARKET as (
-- SELECT
-- DISTINCT TA,
-- MARKET
-- FROM
-- dm.dm_td_external_market_pack_mapping_temp
-- WHERE
-- length(TA) > 0
-- )
-- insert overwrite dm.dm_auth_prd(
-- KCODE,
-- MARKET
-- )
-- select distinct
-- t1.KCODE,
-- t2.MARKET
-- from ALL_USERS t1
-- inner join TA_MARKET t2
-- on t1.TA_PRD = t2.TA
-- COMMAND ----------
-- ------------------------------------------------------------------------
-- --修改时间20241108
-- --修改人Fanxujia
-- --修改内容:
-- --增加产品粒度的权限表
-- ------------------------------------------------------------------------
-- insert overwrite table dm.dm_auth_prd_pack
-- (
-- KCODE,
-- PACK_COD
-- )
-- with market_pack as (
-- select distinct
-- PACK_COD,
-- MARKET
-- from dm.dm_td_external_market_pack_mapping
-- )
-- select distinct
-- t1.KCODE,
-- t2.PACK_COD
-- from dm.dm_auth_prd t1
-- inner join market_pack t2
-- on t1.MARKET = t2.MARKET
-- COMMAND ----------
-- --------------------------------chpa--------------------------------------------------------------------
-- ------------------所有BU能看CHPA,用户需要排除非重点城市之外的人
-- insert overwrite dm.dm_auth_sales(KCODE,bu_code_auth,bu_name,role,MKT_SOURCE,AUDIT_SOURCE,DATA_SOURCE)
-- WITH EMP AS (
-- select distinct dsm_kcode KCODE,concat(bu_code,'IQVIA-CHPA(Monthly)') as bu_code_auth,bu_name,'dsm' role from DM.DM_IMS_TD_ORG_HVH where length(dsm_kcode) > 0 and yyyymm=(select max(yyyymm) from DM.DM_IMS_TD_ORG_HVH) --and audit_cod is not null
-- union all
-- select distinct rsm_kcode KCODE,concat(bu_code,'IQVIA-CHPA(Monthly)') as bu_code_auth,bu_name,'rsm' role from DM.DM_IMS_TD_ORG where length(rsm_kcode) > 0 --and audit_cod is not null
-- union all
-- select distinct rsd_kcode KCODE,concat(bu_code,'IQVIA-CHPA(Monthly)') as bu_code_auth,bu_name,'rsd' role from DM.DM_IMS_TD_ORG where length(rsd_kcode) > 0 --and audit_cod is not null
-- union all
-- select distinct nsd_kcode KCODE,concat(bu_code,'IQVIA-CHPA(Monthly)') as bu_code_auth,bu_name,'nsd' role from DM.DM_IMS_TD_ORG where length(nsd_kcode) > 0 --and audit_cod is not null
-- )
-- SELECT A.KCODE,A.bu_code_auth,A.bu_name,A.role,concat(B.MARKET,'IQVIA-CHPA(Monthly)') MKT_SOURCE,concat(B.AUDIT_COD,'IQVIA-CHPA(Monthly)') AUDIT_SOURCE
-- ,'IQVIA-CHPA(Monthly)' DATA_SOURCE FROM EMP A
-- INNER JOIN (SELECT DISTINCT bu_code_auth,AUDIT_COD,MARKET FROM DM.dm_td_external_org) B ON A.bu_code_auth = B.bu_code_auth
-- COMMAND ----------
-- ----------------------------------------------------CHC------------------------------------------------------------------------------
-- -----------------------CHC的人能看
-- insert into dm.dm_auth_sales(KCODE,bu_code_auth,bu_name,role,MKT_SOURCE,AUDIT_SOURCE,DATA_SOURCE)
-- with emp as (
-- select distinct dsm_kcode KCODE,concat(bu_code,'CHC(Quarterly)') as bu_code_auth,'' bu_name,'dsm' role from DM.DM_IMS_TD_ORG_HVH where length(dsm_kcode) > 0 and yyyymm=(select max(yyyymm) from DM.DM_IMS_TD_ORG_HVH) AND BU_NAME_EN = 'CHC'
-- union all
-- select distinct rsm_kcode KCODE,concat(bu_code,'CHC(Quarterly)') as bu_code_auth,'' bu_name,'rsm' role from DM.DM_IMS_TD_ORG where length(rsm_kcode) > 0 AND BU_NAME_EN = 'CHC'
-- union all
-- select distinct rsd_kcode KCODE,concat(bu_code,'CHC(Quarterly)') as bu_code_auth,'' bu_name,'rsd' role from DM.DM_IMS_TD_ORG where length(rsd_kcode) > 0 AND BU_NAME_EN = 'CHC'
-- union all
-- select distinct nsd_kcode KCODE,concat(bu_code,'CHC(Quarterly)') as bu_code_auth,'' bu_name,'nsd' role from DM.DM_IMS_TD_ORG where length(nsd_kcode) > 0 AND BU_NAME_EN = 'CHC'
-- )
-- SELECT A.KCODE,A.bu_code_auth,A.bu_name,A.role,concat(B.MARKET,'CHC(Quarterly)') MKT_SOURCE,concat(B.AUDIT_COD,'CHC(Quarterly)') AUDIT_SOURCE
-- ,'CHC(Quarterly)' DATA_SOURCE FROM EMP A
-- INNER JOIN (SELECT DISTINCT bu_code_auth,AUDIT_COD,MARKET FROM DM.dm_td_external_org ) B ON A.bu_code_auth = B.bu_code_auth
-- COMMAND ----------
-- ----------------------------------------------------county------------------------------------------------------------------------------
-- -----------------------除了Eagle、CHC、Retail、EC的人都能看
-- insert into dm.dm_auth_sales(KCODE,bu_code_auth,bu_name,role,MKT_SOURCE,AUDIT_SOURCE,DATA_SOURCE)
-- with emp as (
-- select distinct dsm_kcode KCODE,concat('','IQVIA-COUNTY(Quarterly)') bu_code_auth,'' bu_name,'dsm' role from DM.DM_IMS_TD_ORG_HVH where length(dsm_kcode) > 0 and yyyymm=(select max(yyyymm) from DM.DM_IMS_TD_ORG_HVH) AND BU_NAME_EN NOT IN ('Eagle','CHC','EC','Retail')
-- union all
-- select distinct rsm_kcode KCODE,concat('','IQVIA-COUNTY(Quarterly)') bu_code_auth,'' bu_name,'rsm' role from DM.DM_IMS_TD_ORG where length(rsm_kcode) > 0 AND BU_NAME_EN NOT IN ('Eagle','CHC','EC','Retail')
-- union all
-- select distinct rsd_kcode KCODE,concat('','IQVIA-COUNTY(Quarterly)') bu_code_auth,'' bu_name,'rsd' role from DM.DM_IMS_TD_ORG where length(rsd_kcode) > 0 AND BU_NAME_EN NOT IN ('Eagle','CHC','EC','Retail')
-- union all
-- select distinct nsd_kcode KCODE,concat('','IQVIA-COUNTY(Quarterly)') bu_code_auth,'' bu_name,'nsd' role from DM.DM_IMS_TD_ORG where length(nsd_kcode) > 0 AND BU_NAME_EN NOT IN ('Eagle','CHC','EC','Retail')
-- )
-- SELECT A.KCODE,A.bu_code_auth,A.bu_name,A.role,concat(B.MARKET,'IQVIA-COUNTY(Quarterly)') MKT_SOURCE,concat(B.AUDIT_COD,'IQVIA-COUNTY(Quarterly)') AUDIT_SOURCE
-- ,'IQVIA-COUNTY(Quarterly)' DATA_SOURCE FROM EMP A
-- INNER JOIN (SELECT DISTINCT bu_code_auth,AUDIT_COD,MARKET FROM DM.dm_td_external_org ) B ON A.bu_code_auth = B.bu_code_auth
-- COMMAND ----------
-- ----------------------------------------------------THC------------------------------------------------------------------------------
-- -----------------------Eagle的人能看
-- insert into dm.dm_auth_sales(KCODE,bu_code_auth,bu_name,role,MKT_SOURCE,AUDIT_SOURCE,DATA_SOURCE)
-- with emp as (
-- select distinct dsm_kcode KCODE,concat('','THC(Quarterly)') as bu_code_auth,'' bu_name,'dsm' role from DM.DM_IMS_TD_ORG_HVH where length(dsm_kcode) > 0 and yyyymm=(select max(yyyymm) from DM.DM_IMS_TD_ORG_HVH) AND BU_NAME_EN = 'Eagle'
-- union all
-- select distinct rsm_kcode KCODE,concat('','THC(Quarterly)') as bu_code_auth,'' bu_name,'rsm' role from DM.DM_IMS_TD_ORG where length(rsm_kcode) > 0 AND BU_NAME_EN = 'Eagle'
-- union all
-- select distinct rsd_kcode KCODE,concat('','THC(Quarterly)') as bu_code_auth,'' bu_name,'rsd' role from DM.DM_IMS_TD_ORG where length(rsd_kcode) > 0 AND BU_NAME_EN = 'Eagle'
-- union all
-- select distinct nsd_kcode KCODE,concat('','THC(Quarterly)') as bu_code_auth,'' bu_name,'nsd' role from DM.DM_IMS_TD_ORG where length(nsd_kcode) > 0 AND BU_NAME_EN = 'Eagle'
-- )
-- SELECT A.KCODE,A.bu_code_auth,A.bu_name,A.role,concat(B.MARKET,'THC(Quarterly)') MKT_SOURCE,concat(B.AUDIT_COD,'THC(Quarterly)') AUDIT_SOURCE
-- ,'THC(Quarterly)' DATA_SOURCE FROM EMP A
-- INNER JOIN (SELECT DISTINCT bu_code_auth,AUDIT_COD,MARKET FROM DM.dm_td_external_org ) B ON A.bu_code_auth = B.bu_code_auth
-- COMMAND ----------
-- --------------------------------Retail--------------------------------------------------------------------
-- ------------------------------------------------
-- --修改时间20241016
-- --修改人FanXujia
-- --修改内容:
-- --给Eagle的人查看Metoprolol Tartrate Market的权限
-- -------------------------------------------------
-- ------------------所有BU能看CHPA,用户需要排除非重点城市之外的人
-- insert into dm.dm_auth_sales(KCODE,bu_code_auth,bu_name,role,MKT_SOURCE,AUDIT_SOURCE,DATA_SOURCE)
-- with emp as (
-- select distinct dsm_kcode KCODE,concat('PC09','Retail(Quarterly)') as bu_code_auth,bu_name,'dsm' role from DM.DM_IMS_TD_ORG_HVH where length(dsm_kcode) > 0 and yyyymm=(select max(yyyymm) from DM.DM_IMS_TD_ORG_HVH) and bu_name_en in ('Retail','OBU')
-- union all
-- select distinct rsm_kcode KCODE,concat('PC09','Retail(Quarterly)') as bu_code_auth,bu_name,'rsm' role from DM.DM_IMS_TD_ORG where length(rsm_kcode) > 0 and bu_name_en in ('Retail','OBU')
-- union all
-- select distinct rsd_kcode KCODE,concat('PC09','Retail(Quarterly)') as bu_code_auth,bu_name,'rsd' role from DM.DM_IMS_TD_ORG where length(rsd_kcode) > 0 and bu_name_en in ('Retail','OBU')
-- union all
-- select distinct nsd_kcode KCODE,concat('PC09','Retail(Quarterly)') as bu_code_auth,bu_name,'nsd' role from DM.DM_IMS_TD_ORG where length(nsd_kcode) > 0 and bu_name_en in ('Retail','OBU')
-- )
-- ,Eagle as (
-- select distinct dsm_kcode KCODE,concat('PC09','Retail(Quarterly)') as bu_code_auth,bu_name,'dsm' role from DM.DM_IMS_TD_ORG_HVH where length(dsm_kcode) > 0 and yyyymm=(select max(yyyymm) from DM.DM_IMS_TD_ORG_HVH) and bu_name_en in ('Eagle')
-- union all
-- select distinct rsm_kcode KCODE,concat('PC09','Retail(Quarterly)') as bu_code_auth,bu_name,'rsm' role from DM.DM_IMS_TD_ORG where length(rsm_kcode) > 0 and bu_name_en in ('Eagle')
-- union all
-- select distinct rsd_kcode KCODE,concat('PC09','Retail(Quarterly)') as bu_code_auth,bu_name,'rsd' role from DM.DM_IMS_TD_ORG where length(rsd_kcode) > 0 and bu_name_en in ('Eagle')
-- union all
-- select distinct nsd_kcode KCODE,concat('PC09','Retail(Quarterly)') as bu_code_auth,bu_name,'nsd' role from DM.DM_IMS_TD_ORG where length(nsd_kcode) > 0 and bu_name_en in ('Eagle')
-- )
-- ,audit as (
-- select distinct t1.AUDIT_COD
-- from dm.DM_TF_EXT_UNIONALL_SALES t1
-- left join dm.dm_td_external_market_pack_mapping_temp t2
-- on t1.DATA_SOURCE = t2.DATA_SOURCE
-- and t1.PACK_COD = t2.PACK_COD
-- where t1.DATA_SOURCE = 'Retail(Quarterly)'
-- and t2.MARKET = 'Metoprolol Tartrate Market'
-- )
-- SELECT A.KCODE,A.bu_code_auth,A.bu_name,A.role,concat(B.MARKET,'Retail(Quarterly)') MKT_SOURCE,concat(B.AUDIT_COD,'Retail(Quarterly)') AUDIT_SOURCE
-- ,'Retail(Quarterly)' DATA_SOURCE FROM EMP A
-- INNER JOIN (SELECT DISTINCT bu_code_auth,AUDIT_COD,MARKET FROM DM.dm_td_external_org ) B ON A.bu_code_auth = B.bu_code_auth
-- union all
-- select t1.KCODE,
-- t1.bu_code_auth,
-- t1.bu_name,
-- t1.role,
-- concat('Metoprolol Tartrate Market','Retail(Quarterly)') MKT_SOURCE,
-- concat(t2.AUDIT_COD,'Retail(Quarterly)') AUDIT_SOURCE,
-- 'Retail(Quarterly)' DATA_SOURCE
-- from Eagle t1
-- cross join audit t2
-- COMMAND ----------
-- --------------------------------EC--------------------------------------------------------------------
-- ------------------EC的数据暂时开通给EC
-- insert into dm.dm_auth_sales(KCODE,bu_code_auth,bu_name,role,MKT_SOURCE,AUDIT_SOURCE,DATA_SOURCE)
-- with emp as (
-- select distinct dsm_kcode KCODE,concat('','EC(Monthly)') as bu_code_auth,bu_name,'dsm' role from DM.DM_IMS_TD_ORG_HVH where length(dsm_kcode) > 0 and yyyymm=(select max(yyyymm) from DM.DM_IMS_TD_ORG_HVH) and bu_name_en = 'EC'
-- union all
-- select distinct rsm_kcode KCODE,concat('','EC(Monthly)') as bu_code_auth,bu_name,'rsm' role from DM.DM_IMS_TD_ORG where length(rsm_kcode) > 0 and bu_name_en = 'EC'
-- union all
-- select distinct rsd_kcode KCODE,concat('','EC(Monthly)') as bu_code_auth,bu_name,'rsd' role from DM.DM_IMS_TD_ORG where length(rsd_kcode) > 0 and bu_name_en = 'EC'
-- union all
-- select distinct nsd_kcode KCODE,concat('','EC(Monthly)') as bu_code_auth,bu_name,'nsd' role from DM.DM_IMS_TD_ORG where length(nsd_kcode) > 0 and bu_name_en = 'EC'
-- )
-- SELECT A.KCODE,A.bu_code_auth,A.bu_name,A.role,concat(B.MARKET,'EC(Monthly)') MKT_SOURCE,concat(B.AUDIT_COD,'EC(Monthly)') AUDIT_SOURCE
-- ,'EC(Monthly)' DATA_SOURCE FROM EMP A
-- INNER JOIN (SELECT DISTINCT bu_code_auth,AUDIT_COD,MARKET FROM DM.dm_td_external_org ) B ON A.bu_code_auth = B.bu_code_auth
-- COMMAND ----------
-- --------------------------------AIA--------------------------------------------------------------------
-- ------------------所有BU能看AIA
-- insert into dm.dm_auth_sales(KCODE,bu_code_auth,bu_name,role,MKT_SOURCE,AUDIT_SOURCE,DATA_SOURCE)
-- WITH EMP AS (
-- select distinct dsm_kcode KCODE,concat(bu_code,'AIA(Monthly)') as bu_code_auth,bu_name,'dsm' role from DM.DM_IMS_TD_ORG_HVH where length(dsm_kcode) > 0 and yyyymm=(select max(yyyymm) from DM.DM_IMS_TD_ORG_HVH) --and audit_cod is not null
-- union all
-- select distinct rsm_kcode KCODE,concat(bu_code,'AIA(Monthly)') as bu_code_auth,bu_name,'rsm' role from DM.DM_IMS_TD_ORG where length(rsm_kcode) > 0 --and audit_cod is not null
-- union all
-- select distinct rsd_kcode KCODE,concat(bu_code,'AIA(Monthly)') as bu_code_auth,bu_name,'rsd' role from DM.DM_IMS_TD_ORG where length(rsd_kcode) > 0 --and audit_cod is not null
-- union all
-- select distinct nsd_kcode KCODE,concat(bu_code,'AIA(Monthly)') as bu_code_auth,bu_name,'nsd' role from DM.DM_IMS_TD_ORG where length(nsd_kcode) > 0 --and audit_cod is not null
-- )
-- SELECT A.KCODE,A.bu_code_auth,A.bu_name,A.role,concat(B.MARKET,'AIA(Monthly)') MKT_SOURCE,concat(B.AUDIT_COD,'AIA(Monthly)') AUDIT_SOURCE
-- ,'AIA(Monthly)' DATA_SOURCE FROM EMP A
-- INNER JOIN (SELECT DISTINCT bu_code_auth,AUDIT_COD,MARKET FROM DM.dm_td_external_org) B ON A.bu_code_auth = B.bu_code_auth
-- COMMAND ----------
-- --------------------------------XIEHE--------------------------------------------------------------------
-- ------------------所有BU能看XIEHE
-- insert into dm.dm_auth_sales(KCODE,bu_code_auth,bu_name,role,MKT_SOURCE,AUDIT_SOURCE,DATA_SOURCE)
-- WITH EMP AS (
-- select distinct dsm_kcode KCODE,concat(bu_code,'XH Data(Quarterly)') as bu_code_auth,bu_name,'dsm' role from DM.DM_IMS_TD_ORG_HVH where length(dsm_kcode) > 0 and yyyymm=(select max(yyyymm) from DM.DM_IMS_TD_ORG_HVH) --and audit_cod is not null
-- union all
-- select distinct rsm_kcode KCODE,concat(bu_code,'XH Data(Quarterly)') as bu_code_auth,bu_name,'rsm' role from DM.DM_IMS_TD_ORG where length(rsm_kcode) > 0 --and audit_cod is not null
-- union all
-- select distinct rsd_kcode KCODE,concat(bu_code,'XH Data(Quarterly)') as bu_code_auth,bu_name,'rsd' role from DM.DM_IMS_TD_ORG where length(rsd_kcode) > 0 --and audit_cod is not null
-- union all
-- select distinct nsd_kcode KCODE,concat(bu_code,'XH Data(Quarterly)') as bu_code_auth,bu_name,'nsd' role from DM.DM_IMS_TD_ORG where length(nsd_kcode) > 0 --and audit_cod is not null
-- )
-- SELECT A.KCODE,A.bu_code_auth,A.bu_name,A.role,concat(B.MARKET,'XH Data(Quarterly)') MKT_SOURCE,concat(B.AUDIT_COD,'XH Data(Quarterly)') AUDIT_SOURCE
-- ,'XH Data(Quarterly)' DATA_SOURCE FROM EMP A
-- INNER JOIN (SELECT DISTINCT bu_code_auth,AUDIT_COD,MARKET FROM DM.dm_td_external_org) B ON A.bu_code_auth = B.bu_code_auth
-- COMMAND ----------
-- -------------------------------------------------------------------------
-- --修改时间20241106
-- --修改人Fanxujia
-- --修改内容:
-- --将PBI的逻辑移到DBR上dm_auth_sales_geo
-- -------------------------------------------------------------------------
-- insert overwrite table dm.dm_auth_sales_geo(
-- KCODE,
-- AUDIT_SOURCE
-- )
-- select
-- distinct KCODE,
-- AUDIT_SOURCE
-- from
-- dm.dm_auth_sales
-- COMMAND ----------
-- -------------------------------------------------------------------------
-- --修改时间20241106
-- --修改人Fanxujia
-- --修改内容:
-- --将PBI的逻辑移到DBR上dm_auth_sales_mkt
-- -------------------------------------------------------------------------
-- insert overwrite table dm.dm_auth_sales_mkt(
-- KCODE,
-- MKT_SOURCE
-- )
-- select
-- distinct KCODE,
-- MKT_SOURCE
-- from
-- dm.dm_auth_sales
-- COMMAND ----------
-- -------------------------------------------------------------------------
-- --修改时间20241106
-- --修改人Fanxujia
-- --修改内容:
-- --将PBI的逻辑移到DBR上dm_auth_sales_org
-- -------------------------------------------------------------------------
-- insert overwrite table dm.dm_auth_sales_org(
-- KCODE,
-- BU_CODE_AUTH
-- )
-- select
-- distinct KCODE,
-- BU_CODE_AUTH
-- from
-- dm.dm_auth_sales
-- COMMAND ----------
-- DBTITLE 1,old version
-- -------------------------------------------------------------------------
-- --修改时间20241106
-- --修改人Fanxujia
-- --修改内容:
-- --将PBI的逻辑移到DBR上dm_auth_sales_pack
-- -------------------------------------------------------------------------
-- insert overwrite table dm.dm_auth_sales_pack(
-- KCODE,
-- PACK_SOURCE
-- )
-- with market_pack as (
-- select distinct
-- concat(MARKET,DATA_SOURCE) as MKT_SOURCE,
-- concat(PACK_COD,DATA_SOURCE) as PACK_SOURCE
-- from dm.dm_td_external_market_pack_mapping
-- )
-- ,user_market as (
-- select distinct KCODE,MKT_SOURCE
-- from dm.dm_auth_sales
-- )
-- select distinct
-- t1.KCODE,
-- t2.PACK_SOURCE
-- from user_market t1
-- inner join market_pack t2
-- on t1.MKT_SOURCE = t2.MKT_SOURCE
-- COMMAND ----------
-- MAGIC %md
-- MAGIC ### -------------------------
-- COMMAND ----------
------------------------------------------------------------------------
--修改时间20241112
--修改人Fanxujia
--修改内容:
--CHPA渠道不显示以下几个BU的架构 ('BBU_County','CHC','EC','Eagle','Retail')
------------------------------------------------------------------------
delete from DM.DM_TD_EXTERNAL_ORG
where bu_name_en in ('BBU_County','CHC','EC','Retail')
and data_source = 'IQVIA-CHPA(Monthly)';
/*
修改时间20251124
修改人Chenwu
修改内容CHPA数据源,消化口服产品Nexium Oral, Losec Oral)架构需要增加Eagle团队
*/
WITH market_list AS (
SELECT DISTINCT m.MARKET
FROM dws.dws_ims_td_market m
INNER JOIN dwd.dwd_td_imscode_azcode a ON m.PACK_COD = a.ims_pack_cod
WHERE a.brandcode IN ('B018','B019')
AND a.ims_pack_cod IS NOT NULL
)
DELETE FROM DM.DM_TD_EXTERNAL_ORG
WHERE data_source = 'IQVIA-CHPA(Monthly)'
AND bu_name_en = 'Eagle'
AND MARKET NOT IN (SELECT MARKET FROM market_list);
-- COMMAND ----------
---------------------------------------------------------------------------
--修改时间20240828
--修改人FanXujia
--修改内容:
--同一个城市的同一个市场同时对应了正常的大区以及ROC这是不对的需要把ROC的数据删除
----------------------------------------------------------------------------
with org as (
select distinct DATA_SOURCE,AUDIT_COD,MARKET,'ROC' as region_name_en
from DM.DM_TD_EXTERNAL_ORG
where region_name_en <> 'ROC'
)
merge into DM.DM_TD_EXTERNAL_ORG as t1
using org t2
on t1.DATA_SOURCE = t2.DATA_SOURCE
and t1.AUDIT_COD = t2.AUDIT_COD
and t1.MARKET = t2.MARKET
and t1.region_name_en = t2.region_name_en
when matched then
delete ;
-- COMMAND ----------
---------------------------------------------------------------------------
--修改时间20240828
--修改人FanXujia
--修改内容:
--在上一步删除完之后
--同一个城市的同一个市场只对应ROC但是有多条数据对应ROC保留1条数据其余的删除
----------------------------------------------------------------------------
with org_1 as (
select DATA_SOURCE,AUDIT_COD,MARKET
from DM.DM_TD_EXTERNAL_ORG
where region_name_en = 'ROC'
group by DATA_SOURCE,AUDIT_COD,MARKET
having count(1) >1
)
,org_2 as (
select t1.DATA_SOURCE,t1.AUDIT_COD,t1.MARKET,
max(concat(t1.RSM_Name,t1.rc_name,t1.regioncenter,t1.bu_category,t1.bu_name_en,t1.sub_bu_name,t1.sub_bu_code_auth,t1.bu_code_auth,t1.MR_TRTY_CODE)) link_key
from DM.DM_TD_EXTERNAL_ORG t1
inner join org_1 t2
on t1.DATA_SOURCE = t2.DATA_SOURCE
and t1.AUDIT_COD = t2.AUDIT_COD
and t1.MARKET = t2.MARKET
group by t1.DATA_SOURCE,t1.AUDIT_COD,t1.MARKET
)
merge into DM.DM_TD_EXTERNAL_ORG as t1
using org_2 t2
on t1.DATA_SOURCE = t2.DATA_SOURCE
and t1.AUDIT_COD = t2.AUDIT_COD
and t1.MARKET = t2.MARKET
and concat(t1.RSM_Name,t1.rc_name,t1.regioncenter,t1.bu_category,t1.bu_name_en,t1.sub_bu_name,t1.sub_bu_code_auth,t1.bu_code_auth,t1.MR_TRTY_CODE) <> t2.link_key
when matched then
delete ;