Add EXTERNAL SQL scripts
This commit is contained in:
64
EXTERNAL/01 dm_tf_external_sales.sql
Normal file
64
EXTERNAL/01 dm_tf_external_sales.sql
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
-- Databricks notebook source
|
||||||
|
insert overwrite table dm.dm_tf_external_sales
|
||||||
|
(YYYYMM,
|
||||||
|
PACK_COD,
|
||||||
|
CORP_COD,
|
||||||
|
AUDIT_COD,
|
||||||
|
PLATFORM_TYPE,
|
||||||
|
STORE_NAME,
|
||||||
|
STORE_TYPE,
|
||||||
|
REGION_TYPE,
|
||||||
|
PACK_FLAG,
|
||||||
|
PROD_FLAG,
|
||||||
|
DTP_FLAG,
|
||||||
|
SALES_UNIT_CAL,
|
||||||
|
SALES_UNIT_CAL_LY,
|
||||||
|
SALES_VALUE_CAL,
|
||||||
|
SALES_VALUE_CAL_LY,
|
||||||
|
CONUTING_UNIT,
|
||||||
|
CONUTING_UNIT_LY,
|
||||||
|
DATA_SOURCE,
|
||||||
|
inst_code,
|
||||||
|
cmps_flag,
|
||||||
|
dept_name,
|
||||||
|
prescription,
|
||||||
|
prescription_ly,
|
||||||
|
new_code,
|
||||||
|
area,
|
||||||
|
h_level,
|
||||||
|
reimburse,
|
||||||
|
reimburse_type,
|
||||||
|
prescription_source
|
||||||
|
)
|
||||||
|
select
|
||||||
|
YYYYMM,
|
||||||
|
PACK_CODE AS PACK_COD,
|
||||||
|
CORP_CODE AS CORP_COD,
|
||||||
|
AUDIT_CODE AS AUDIT_COD,
|
||||||
|
PLATFORM_TYPE,
|
||||||
|
STORE_NAME,
|
||||||
|
STORE_TYPE,
|
||||||
|
REGION_TYPE,
|
||||||
|
PACK_FLAG,
|
||||||
|
PROD_FLAG,
|
||||||
|
DTP_FLAG,
|
||||||
|
SALES_UNIT_CAL,
|
||||||
|
SALES_UNIT_CAL_LY,
|
||||||
|
SALES_VALUE_CAL,
|
||||||
|
SALES_VALUE_CAL_LY,
|
||||||
|
CONUTING_UNIT,
|
||||||
|
CONUTING_UNIT_LY,
|
||||||
|
DATA_SOURCE,
|
||||||
|
inst_code,
|
||||||
|
cmps_flag,
|
||||||
|
dept_name,
|
||||||
|
prescription,
|
||||||
|
prescription_ly,
|
||||||
|
new_code,
|
||||||
|
area,
|
||||||
|
h_level,
|
||||||
|
reimburse,
|
||||||
|
reimburse_type,
|
||||||
|
prescription_source
|
||||||
|
from
|
||||||
|
dm.DM_TF_EXT_UNIONALL_SALES
|
||||||
BIN
EXTERNAL/01 dm_tf_external_sales.sql:Zone.Identifier
Normal file
BIN
EXTERNAL/01 dm_tf_external_sales.sql:Zone.Identifier
Normal file
Binary file not shown.
42
EXTERNAL/02 dm_td_external_keycompatitor.sql
Normal file
42
EXTERNAL/02 dm_td_external_keycompatitor.sql
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
-- Databricks notebook source
|
||||||
|
insert overwrite dm.dm_td_external_keycompatitor (
|
||||||
|
RNK,
|
||||||
|
BRAND_NAME,
|
||||||
|
BRAND_CODE,
|
||||||
|
IMSBRAND,
|
||||||
|
CompatitorCN,
|
||||||
|
CLASS,
|
||||||
|
CompatitorEN,
|
||||||
|
CORP,
|
||||||
|
Market,
|
||||||
|
VBP,
|
||||||
|
ValueType,
|
||||||
|
MARKETDESC,
|
||||||
|
Strategicbrand,
|
||||||
|
TA,
|
||||||
|
DEFAULTFLAG,
|
||||||
|
CompatitorMarket,
|
||||||
|
CompatitorType,
|
||||||
|
DATA_SOURCE
|
||||||
|
)
|
||||||
|
select
|
||||||
|
RNK,
|
||||||
|
BRAND_NAME,
|
||||||
|
BRAND_CODE,
|
||||||
|
IMSBRAND,
|
||||||
|
competitorcn,
|
||||||
|
CLASS,
|
||||||
|
competitoren,
|
||||||
|
CORP,
|
||||||
|
Market,
|
||||||
|
VBP,
|
||||||
|
ValueType,
|
||||||
|
MARKETDESC,
|
||||||
|
Strategicbrand,
|
||||||
|
TA,
|
||||||
|
int(DEFAULTFLAG),
|
||||||
|
competitormarket,
|
||||||
|
competitortype,
|
||||||
|
DATA_SOURCE
|
||||||
|
from
|
||||||
|
dwd.dwd_gnd_ims_compete_product_config
|
||||||
BIN
EXTERNAL/02 dm_td_external_keycompatitor.sql:Zone.Identifier
Normal file
BIN
EXTERNAL/02 dm_td_external_keycompatitor.sql:Zone.Identifier
Normal file
Binary file not shown.
70
EXTERNAL/03 dm_td_external_brand_market.sql
Normal file
70
EXTERNAL/03 dm_td_external_brand_market.sql
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
-- Databricks notebook source
|
||||||
|
-- drop table if exists dm.DM_TD_EXTERNAL_BRAND_MARKET;
|
||||||
|
-- create table if not exists dm.DM_TD_EXTERNAL_BRAND_MARKET
|
||||||
|
-- using parquet as
|
||||||
|
INSERT OVERWRITE dm.DM_TD_EXTERNAL_BRAND_MARKET (
|
||||||
|
TA_NAME,
|
||||||
|
RNK,
|
||||||
|
BRAND_CODE,
|
||||||
|
BRAND_NAME,
|
||||||
|
KEYCOMPATITORCN,
|
||||||
|
KEYCOMPATITOREN,
|
||||||
|
CLASS,
|
||||||
|
IMSBRAND,
|
||||||
|
CORP,
|
||||||
|
MARKET,
|
||||||
|
MARKETDESC,
|
||||||
|
DEFAULTFLAG,
|
||||||
|
VBP,
|
||||||
|
VBPFLAG,
|
||||||
|
VALUETYPE,
|
||||||
|
Strategicbrand,
|
||||||
|
CompatitorMarket,
|
||||||
|
CompatitorType,
|
||||||
|
TA_RN,
|
||||||
|
DATA_SOURCE
|
||||||
|
)
|
||||||
|
SELECT
|
||||||
|
A.TA TA_NAME,
|
||||||
|
CASE
|
||||||
|
WHEN A.RNK = 'null' then 99
|
||||||
|
ELSE A.RNK
|
||||||
|
END,
|
||||||
|
A.BRAND_CODE,
|
||||||
|
A.BRAND_NAME,
|
||||||
|
REPLACE(NVL(A.CompatitorCN, ''), 'null', '') KEYCOMPATITORCN,
|
||||||
|
REPLACE(NVL(A.CompatitorEN, ''), 'null', '') KEYCOMPATITOREN,
|
||||||
|
REPLACE(NVL(A.CLASS, ''), 'null', '') CLASS,
|
||||||
|
upper(A.IMSBRAND) IMSBRAND,
|
||||||
|
REPLACE(NVL(A.CORP, ''), 'null', '') CORP,
|
||||||
|
REPLACE(NVL(A.MARKET, ''), 'null', '') MARKET,
|
||||||
|
A.MARKETDESC,
|
||||||
|
A.DEFAULTFLAG,
|
||||||
|
CASE
|
||||||
|
WHEN REPLACE(NVL(A.VBP, ''), 'null', '') = '' THEN 'NonVBP'
|
||||||
|
ELSE REPLACE(NVL(A.VBP, ''), 'null', '')
|
||||||
|
END VBP,
|
||||||
|
CASE
|
||||||
|
WHEN REPLACE(NVL(A.VBP, ''), 'null', '') <> '' THEN 'VBP'
|
||||||
|
ELSE 'NonVBP'
|
||||||
|
END VBPFLAG,
|
||||||
|
A.VALUETYPE,
|
||||||
|
A.Strategicbrand,
|
||||||
|
REPLACE(NVL(A.CompatitorMarket, ''), 'null', '') CompatitorMarket,
|
||||||
|
REPLACE(NVL(A.CompatitorType, ''), 'null', '') CompatitorType,
|
||||||
|
CASE
|
||||||
|
WHEN A.TA = 'CV' THEN 2
|
||||||
|
WHEN A.TA = 'DM' THEN 1
|
||||||
|
WHEN A.TA = 'RD' THEN 3
|
||||||
|
WHEN A.TA = 'RE' THEN 4
|
||||||
|
WHEN A.TA = 'NEB' THEN 5
|
||||||
|
WHEN A.TA = 'GI' THEN 6
|
||||||
|
WHEN A.TA = 'ONC' THEN 7
|
||||||
|
WHEN A.TA = 'RARE' THEN 8
|
||||||
|
END TA_RN,
|
||||||
|
CASE
|
||||||
|
when A.DATA_SOURCE = 'IQVIA-County(Quarterly)' THEN 'IQVIA-COUNTY(Quarterly)'
|
||||||
|
ELSE A.DATA_SOURCE
|
||||||
|
END DATA_SOURCE
|
||||||
|
FROM
|
||||||
|
dm.DM_TD_EXTERNAL_KEYCOMPATITOR A
|
||||||
BIN
EXTERNAL/03 dm_td_external_brand_market.sql:Zone.Identifier
Normal file
BIN
EXTERNAL/03 dm_td_external_brand_market.sql:Zone.Identifier
Normal file
Binary file not shown.
382
EXTERNAL/04 dm_td_external_calendar.sql
Normal file
382
EXTERNAL/04 dm_td_external_calendar.sql
Normal file
@@ -0,0 +1,382 @@
|
|||||||
|
-- Databricks notebook source
|
||||||
|
---------------------------------------------------------------------------
|
||||||
|
--修改时间:20241126
|
||||||
|
--修改人:Fanxujia
|
||||||
|
--修改内容:
|
||||||
|
--THC渠道分为14省的数据源以及全国的数据源两种,分在了2个事实表里,下面在维表在引用事实表时,需要将两个事实表union在一起
|
||||||
|
----------------------------------------------------------------------------
|
||||||
|
create or replace temporary view external_sales_union as
|
||||||
|
select YYYYMM,SALES_VALUE_CAL,DATA_SOURCE from dm.dm_tf_ext_unionall_sales
|
||||||
|
union all
|
||||||
|
select YYYYMM,SALES_VALUE_CAL,DATA_SOURCE from dm.dm_tf_external_sales_thc
|
||||||
|
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
---------------------------------------------------------------------------
|
||||||
|
--修改时间:20260309
|
||||||
|
--修改人:chenwu
|
||||||
|
--修改内容:
|
||||||
|
--niad 的数据会比其他数据先入数据库,会产生月份差。
|
||||||
|
----------------------------------------------------------------------------
|
||||||
|
create or replace temporary view retail_niad_sales as
|
||||||
|
select
|
||||||
|
GREATEST(niad_month, OTHERS_MONTH) as YYYYMM,
|
||||||
|
NIAD_MONTH
|
||||||
|
from
|
||||||
|
(
|
||||||
|
select
|
||||||
|
max(niad_month) as niad_month,
|
||||||
|
max(OTHERS_MONTH) as OTHERS_MONTH
|
||||||
|
from
|
||||||
|
(
|
||||||
|
select
|
||||||
|
niad_month,
|
||||||
|
OTHERS_MONTH
|
||||||
|
from
|
||||||
|
tmp.tmp_dm_td_ext_retail_niad_month
|
||||||
|
union all
|
||||||
|
select
|
||||||
|
niad_month,
|
||||||
|
OTHERS_MONTH
|
||||||
|
from
|
||||||
|
tmp.tmp_dm_td_ext_ec_niad_month
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
create or replace temporary view merged_date_calender as
|
||||||
|
WITH ALL_DATA_YYYY (
|
||||||
|
select
|
||||||
|
max(yyyymm) YYYYMM,
|
||||||
|
'AIA' DATATYPE
|
||||||
|
from
|
||||||
|
dm.dm_tf_ext_unionall_sales
|
||||||
|
where
|
||||||
|
DATA_SOURCE = 'AIA(Monthly)'
|
||||||
|
union all
|
||||||
|
select
|
||||||
|
max(yyyymm) YYYYMM,
|
||||||
|
'DTP' DATATYPE
|
||||||
|
from
|
||||||
|
dm.dm_tf_ext_unionall_sales
|
||||||
|
where
|
||||||
|
DATA_SOURCE = 'DTP(Quarterly)'
|
||||||
|
union all
|
||||||
|
select
|
||||||
|
max(yyyymm) YYYYMM,
|
||||||
|
'CHPA' DATATYPE
|
||||||
|
from
|
||||||
|
dm.dm_tf_ext_unionall_sales
|
||||||
|
where
|
||||||
|
DATA_SOURCE = 'IQVIA-CHPA(Monthly)'
|
||||||
|
)
|
||||||
|
SELECT
|
||||||
|
min(YYYYMM) AS MERGED_DATA_MONTH,
|
||||||
|
GREATEST(
|
||||||
|
'202401',
|
||||||
|
CAST((CAST(LEFT(min(yyyymm), 4) AS INT) - 3) * 100 + 1 AS VARCHAR(6)) --三年区间
|
||||||
|
) AS MERGED_DATA_MONTH_START
|
||||||
|
from
|
||||||
|
ALL_DATA_YYYY
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
insert overwrite dm.dm_td_external_calendar (
|
||||||
|
date,
|
||||||
|
yyyymmdd,
|
||||||
|
yyyymm,
|
||||||
|
year,
|
||||||
|
quarter,
|
||||||
|
month,
|
||||||
|
month_cn,
|
||||||
|
day,
|
||||||
|
is_holiday,
|
||||||
|
work_days,
|
||||||
|
mtd_work_days,
|
||||||
|
left_work_days,
|
||||||
|
year_quater,
|
||||||
|
half_year,
|
||||||
|
dd,
|
||||||
|
YYYYMM_EXTERNAL,
|
||||||
|
MONTH_EXTERNAL,
|
||||||
|
YEAR_EXTERNAL,
|
||||||
|
YYYYMM_EXTERNAL_AIA,
|
||||||
|
YYYYMM_EXTERNAL_NIAD,
|
||||||
|
YYYYMM_MERGED_DATA
|
||||||
|
)
|
||||||
|
--公用的日历表最小日期不到2018年,因此需要自己构造一个日历表,日期从2018年起。
|
||||||
|
--31天
|
||||||
|
with day_31 as (select
|
||||||
|
explode(
|
||||||
|
array(
|
||||||
|
1,
|
||||||
|
2,
|
||||||
|
3,
|
||||||
|
4,
|
||||||
|
5,
|
||||||
|
6,
|
||||||
|
7,
|
||||||
|
8,
|
||||||
|
9,
|
||||||
|
10,
|
||||||
|
11,
|
||||||
|
12,
|
||||||
|
13,
|
||||||
|
14,
|
||||||
|
15,
|
||||||
|
16,
|
||||||
|
17,
|
||||||
|
18,
|
||||||
|
19,
|
||||||
|
20,
|
||||||
|
21,
|
||||||
|
22,
|
||||||
|
23,
|
||||||
|
24,
|
||||||
|
25,
|
||||||
|
26,
|
||||||
|
27,
|
||||||
|
28,
|
||||||
|
29,
|
||||||
|
30,
|
||||||
|
31
|
||||||
|
)
|
||||||
|
) as day
|
||||||
|
),
|
||||||
|
--30天
|
||||||
|
day_30 as (select
|
||||||
|
day
|
||||||
|
from
|
||||||
|
day_31
|
||||||
|
where
|
||||||
|
day <= 30
|
||||||
|
),
|
||||||
|
--29天(闰年的2月份)
|
||||||
|
day_29 as (select
|
||||||
|
day
|
||||||
|
from
|
||||||
|
day_31
|
||||||
|
where
|
||||||
|
day <= 29
|
||||||
|
),
|
||||||
|
--28天(非闰年的2月份)
|
||||||
|
day_28 as (select
|
||||||
|
day
|
||||||
|
from
|
||||||
|
day_31
|
||||||
|
where
|
||||||
|
day <= 28
|
||||||
|
),
|
||||||
|
--最后一天是31号的月份
|
||||||
|
month_31 as (select
|
||||||
|
explode(array(1, 3, 5, 7, 8, 10, 12)) as month
|
||||||
|
),
|
||||||
|
--最后一天是30号的月份
|
||||||
|
month_30 as (select
|
||||||
|
explode(array(4, 6, 9, 11)) as month
|
||||||
|
),
|
||||||
|
--2月份单拎出来
|
||||||
|
month_2 as (select
|
||||||
|
2 as month
|
||||||
|
),
|
||||||
|
--非闰年的月、日
|
||||||
|
mmdd_365 as (select
|
||||||
|
month,
|
||||||
|
day
|
||||||
|
from
|
||||||
|
month_31 cross join day_31
|
||||||
|
union all
|
||||||
|
select
|
||||||
|
month,
|
||||||
|
day
|
||||||
|
from
|
||||||
|
month_30 cross join day_30
|
||||||
|
union all
|
||||||
|
select
|
||||||
|
month,
|
||||||
|
day
|
||||||
|
from
|
||||||
|
month_2 cross join day_28
|
||||||
|
),
|
||||||
|
--闰年的月、日
|
||||||
|
mmdd_366 as (select
|
||||||
|
month,
|
||||||
|
day
|
||||||
|
from
|
||||||
|
month_31 cross join day_31
|
||||||
|
union all
|
||||||
|
select
|
||||||
|
month,
|
||||||
|
day
|
||||||
|
from
|
||||||
|
month_30 cross join day_30
|
||||||
|
union all
|
||||||
|
select
|
||||||
|
month,
|
||||||
|
day
|
||||||
|
from
|
||||||
|
month_2 cross join day_29
|
||||||
|
),
|
||||||
|
--注意事项:
|
||||||
|
--目前年份设计只到2040年,如果2041年依旧使用此套代码,这里需要做修改
|
||||||
|
--非闰年的年份
|
||||||
|
year_365 as (select
|
||||||
|
explode(
|
||||||
|
array(
|
||||||
|
2018,
|
||||||
|
2019,
|
||||||
|
2021,
|
||||||
|
2022,
|
||||||
|
2023,
|
||||||
|
2025,
|
||||||
|
2026,
|
||||||
|
2027,
|
||||||
|
2029,
|
||||||
|
2030,
|
||||||
|
2031,
|
||||||
|
2033,
|
||||||
|
2034,
|
||||||
|
2035,
|
||||||
|
2037,
|
||||||
|
2038,
|
||||||
|
2039
|
||||||
|
)
|
||||||
|
) as year
|
||||||
|
),
|
||||||
|
--闰年的年份
|
||||||
|
year_366 as (select
|
||||||
|
explode(array(2020, 2024, 2028, 2032, 2036, 2040)) as year
|
||||||
|
),
|
||||||
|
--从2018年1月1日至2040年12月31日的数据
|
||||||
|
--最终插入dm表时还会在此基础上框定时间范围
|
||||||
|
yyyymmdd as (select
|
||||||
|
year,
|
||||||
|
month,
|
||||||
|
day
|
||||||
|
from
|
||||||
|
year_365 cross join mmdd_365
|
||||||
|
union all
|
||||||
|
select
|
||||||
|
year,
|
||||||
|
month,
|
||||||
|
day
|
||||||
|
from
|
||||||
|
year_366 cross join mmdd_366
|
||||||
|
),
|
||||||
|
--补充其他需要的字段
|
||||||
|
tmp_calendar as (select
|
||||||
|
make_date(year, month, day) as `date`,
|
||||||
|
year * 10000 + month * 100 + day as yyyymmdd,
|
||||||
|
year * 100 + month as yyyymm,
|
||||||
|
concat(month, '月') as month_cn,
|
||||||
|
year,
|
||||||
|
month,
|
||||||
|
day,
|
||||||
|
case
|
||||||
|
when month <= 3 then 1
|
||||||
|
when month <= 6 then 2
|
||||||
|
when month <= 9 then 3
|
||||||
|
else 4
|
||||||
|
end as quarter,
|
||||||
|
case
|
||||||
|
when month <= 3 then concat(year, 'Q1')
|
||||||
|
when month <= 6 then concat(year, 'Q2')
|
||||||
|
when month <= 9 then concat(year, 'Q3')
|
||||||
|
else concat(year, 'Q4')
|
||||||
|
end as year_quater,
|
||||||
|
case
|
||||||
|
when month <= 6 then concat(year, 'H1')
|
||||||
|
else concat(year, 'H2')
|
||||||
|
end as half_year,
|
||||||
|
--下面4个字段实际在PBI上没有用到,因此直接设为0
|
||||||
|
0 as work_days,
|
||||||
|
0 as mtd_work_days,
|
||||||
|
0 as is_holiday,
|
||||||
|
0 as left_work_days
|
||||||
|
from
|
||||||
|
yyyymmdd
|
||||||
|
)
|
||||||
|
select
|
||||||
|
`date`,
|
||||||
|
yyyymmdd,
|
||||||
|
A.yyyymm,
|
||||||
|
`year`,
|
||||||
|
quarter,
|
||||||
|
`month`,
|
||||||
|
month_cn,
|
||||||
|
`day`,
|
||||||
|
is_holiday,
|
||||||
|
work_days,
|
||||||
|
mtd_work_days,
|
||||||
|
left_work_days,
|
||||||
|
year_quater,
|
||||||
|
half_year,
|
||||||
|
substring(yyyymmdd, 7, 2) dd,
|
||||||
|
CASE
|
||||||
|
WHEN A.yyyymm = B.YYYYMM THEN 'R'
|
||||||
|
ELSE A.yyyymm
|
||||||
|
END YYYYMM_EXTERNAL,
|
||||||
|
CASE
|
||||||
|
WHEN A.yyyymm = B.YYYYMM THEN 'R'
|
||||||
|
ELSE `month`
|
||||||
|
END MONTH_EXTERNAL,
|
||||||
|
CASE
|
||||||
|
WHEN A.`year` = LEFT(B.YYYYMM, 4) THEN 'R'
|
||||||
|
ELSE A.`year`
|
||||||
|
END YEAR_EXTERNAL,
|
||||||
|
-- from DM.DM_TD_CALENDAR A
|
||||||
|
----------------------------------------------------
|
||||||
|
--修改时间:20241211
|
||||||
|
--修改人:Fanxujia
|
||||||
|
--修改内容:
|
||||||
|
--给AIA报告增加单独的R月标记
|
||||||
|
----------------------------------------------------
|
||||||
|
case
|
||||||
|
when A.yyyymm = C.YYYYMM THEN 'R'
|
||||||
|
WHEN A.yyyymm >= C.min_yyyymm AND A.yyyymm <=C.yyyymm
|
||||||
|
THEN A.YYYYMM
|
||||||
|
ELSE NULL
|
||||||
|
end as YYYYMM_EXTERNAL_AIA,
|
||||||
|
case
|
||||||
|
when A.yyyymm = D.NIAD_MONTH THEN 'R'
|
||||||
|
else A.yyyymm
|
||||||
|
end as YYYYMM_EXTERNAL_NIAD,
|
||||||
|
case
|
||||||
|
when A.yyyymm = E.MERGED_DATA_MONTH THEN 'R'
|
||||||
|
when A.yyyymm >= E.MERGED_DATA_MONTH_START AND A.YYYYMM<= E.MERGED_DATA_MONTH THEN A.yyyymm
|
||||||
|
else null
|
||||||
|
end as YYYYMM_MERGED_DATA
|
||||||
|
from
|
||||||
|
tmp_calendar A
|
||||||
|
join (
|
||||||
|
select
|
||||||
|
MAX(YYYYMM) YYYYMM
|
||||||
|
from
|
||||||
|
external_sales_union
|
||||||
|
where
|
||||||
|
SALES_VALUE_CAL <> 0
|
||||||
|
) B
|
||||||
|
on 1 = 1
|
||||||
|
--获得AIA数据的最新月
|
||||||
|
cross join (
|
||||||
|
select
|
||||||
|
max(YYYYMM) as YYYYMM,min(yyyymm) AS min_yyyymm
|
||||||
|
from
|
||||||
|
dm.dm_tf_ext_unionall_sales
|
||||||
|
where
|
||||||
|
DATA_SOURCE = 'AIA(Monthly)'
|
||||||
|
and SALES_VALUE_CAL <> 0
|
||||||
|
) C
|
||||||
|
left join retail_niad_sales AS D
|
||||||
|
left join merged_date_calender as E
|
||||||
|
WHERE
|
||||||
|
A.yyyymm >= (
|
||||||
|
SELECT
|
||||||
|
MIN(YYYYMM)
|
||||||
|
FROM
|
||||||
|
external_sales_union
|
||||||
|
)
|
||||||
|
AND A.yyyymm <= (LEFT(D.YYYYMM, 4) * 100 + 12)
|
||||||
|
AND A.day = 1
|
||||||
BIN
EXTERNAL/04 dm_td_external_calendar.sql:Zone.Identifier
Normal file
BIN
EXTERNAL/04 dm_td_external_calendar.sql:Zone.Identifier
Normal file
Binary file not shown.
26
EXTERNAL/05 dm_td_external_exchangerate.sql
Normal file
26
EXTERNAL/05 dm_td_external_exchangerate.sql
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
-- Databricks notebook source
|
||||||
|
/*
|
||||||
|
修改日期:20250311
|
||||||
|
修改人:chenwu
|
||||||
|
修改内容:取sales表中最大日期的年
|
||||||
|
*/
|
||||||
|
insert OVERWRITE dm.dm_td_external_exchangerate (
|
||||||
|
from_currency_id, to_currency_id, az_year, exchange_rate, etl_update_dt, etl_insert_dt
|
||||||
|
)
|
||||||
|
select
|
||||||
|
from_currency_id,
|
||||||
|
to_currency_id,
|
||||||
|
az_year,
|
||||||
|
exchange_rate,
|
||||||
|
etl_update_dt,
|
||||||
|
etl_insert_dt
|
||||||
|
from
|
||||||
|
dm.DM_TD_EXCHANGE_RATE
|
||||||
|
where
|
||||||
|
AZ_YEAR in (
|
||||||
|
select
|
||||||
|
left(max(YYYYMM), 4)
|
||||||
|
from
|
||||||
|
dm.dm_tf_external_sales --取sales表中最大日期的年
|
||||||
|
)
|
||||||
|
AND FROM_CURRENCY_ID = 'USD'
|
||||||
BIN
EXTERNAL/05 dm_td_external_exchangerate.sql:Zone.Identifier
Normal file
BIN
EXTERNAL/05 dm_td_external_exchangerate.sql:Zone.Identifier
Normal file
Binary file not shown.
2833
EXTERNAL/07 dm_td_external_packinfo.sql
Normal file
2833
EXTERNAL/07 dm_td_external_packinfo.sql
Normal file
File diff suppressed because it is too large
Load Diff
BIN
EXTERNAL/07 dm_td_external_packinfo.sql:Zone.Identifier
Normal file
BIN
EXTERNAL/07 dm_td_external_packinfo.sql:Zone.Identifier
Normal file
Binary file not shown.
83
EXTERNAL/08 dm_td_external_corp.sql
Normal file
83
EXTERNAL/08 dm_td_external_corp.sql
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
-- Databricks notebook source
|
||||||
|
----------------------------------------------------------------------------
|
||||||
|
--修改时间:20241126
|
||||||
|
--修改人:Fanxujia
|
||||||
|
--修改内容:
|
||||||
|
--补充THC渠道的全国的事实表中的pack_cod
|
||||||
|
----------------------------------------------------------------------------
|
||||||
|
----------------------------------------------------------------------------
|
||||||
|
--修改时间:20241129
|
||||||
|
--修改人:Fanxujia
|
||||||
|
--修改内容:
|
||||||
|
--补充Retail直取中DTP的手工文件里的pack_cod
|
||||||
|
----------------------------------------------------------------------------
|
||||||
|
with external_sales as (
|
||||||
|
select distinct PACK_COD
|
||||||
|
from dm.DM_TF_EXTERNAL_SALES
|
||||||
|
union
|
||||||
|
select distinct pack_cod
|
||||||
|
from dm.dm_tf_external_sales_thc
|
||||||
|
union
|
||||||
|
select distinct pack_code
|
||||||
|
from dm.dm_zk_retail_sales_kpi
|
||||||
|
where NVL(pack_code,'Others') <> 'Others'
|
||||||
|
and pack_code <> ''
|
||||||
|
)
|
||||||
|
|
||||||
|
--只存放有外部数据的公司信息数据
|
||||||
|
INSERT OVERWRITE dm.dm_td_external_corp(CORP_COD,CORP_DES,CORP_DES_C,CORP_TYPE)
|
||||||
|
SELECT A.CORP_COD,CASE WHEN MAX(CORP_DES) LIKE 'ASTRAZENECA%' THEN 'ASTRAZENECA' ELSE MAX(CORP_DES) END CORP_DES,MAX(CORP_DES_C) CORP_DES_C,
|
||||||
|
CASE WHEN MAX(UPPER(MNFL_DES)) = 'LOCAL' THEN 'LOCAL'
|
||||||
|
WHEN UPPER(A.CORP_COD) LIKE '%CORP_COD%' OR UPPER(A.CORP_COD) LIKE '%OTHERS%' THEN ''
|
||||||
|
ELSE 'MNC' END CORP_TYPE
|
||||||
|
FROM (select distinct PACK_COD,CORP_COD,CORP_DES,CORP_DES_C,MNFL_DES from DM.dm_td_external_packinfo_temp2) A
|
||||||
|
INNER JOIN external_sales B ON A.PACK_COD = B.PACK_COD
|
||||||
|
WHERE A.CORP_COD IS NOT NULL
|
||||||
|
GROUP BY A.CORP_COD
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
|
||||||
|
------------ec补充缺少的Corp信息
|
||||||
|
insert into dm.dm_td_external_corp(CORP_COD,CORP_DES,CORP_DES_C,CORP_TYPE)
|
||||||
|
select
|
||||||
|
case when CORP_COD is null or CORP_COD = '' then 'CORP_COD_EC(Monthly)'
|
||||||
|
else CORP_COD end as CORP_COD,
|
||||||
|
max(CORP_DES) CORP_DES,
|
||||||
|
max(CORP_DES_C) CORP_DES,
|
||||||
|
max(case when upper(MNFL_DES) = 'LOCAL' then 'LOCAL' else 'MNC' end) CORP_TYPE
|
||||||
|
from dwd.dwd_inc_gnd_retail_b2c_label_total
|
||||||
|
where CORP_COD in (
|
||||||
|
select distinct
|
||||||
|
a.CORP_COD
|
||||||
|
from (select distinct corp_cod from dm.dm_zk_ec_sales_kpi ) a
|
||||||
|
where not exists(
|
||||||
|
select 1 from dm.dm_td_external_corp b
|
||||||
|
where a.CORP_COD = b.CORP_COD
|
||||||
|
)
|
||||||
|
)
|
||||||
|
group by 1
|
||||||
|
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
|
||||||
|
------------Retail补充缺少的Corp信息
|
||||||
|
insert into dm.dm_td_external_corp(CORP_COD,CORP_DES,CORP_DES_C,CORP_TYPE)
|
||||||
|
select distinct
|
||||||
|
case when CORP_COD is null or CORP_COD = '' then 'CORP_COD_Retail(Quarterly)'
|
||||||
|
else CORP_COD end as CORP_COD,
|
||||||
|
max(CORP_DES) CORP_DES,
|
||||||
|
max(CORP_DES_C) CORP_DES,
|
||||||
|
max(case when upper(MNFL_DES) = 'LOCAL' then 'LOCAL' else 'MNC' end) CORP_TYPE
|
||||||
|
from dwd.dwd_inc_gnd_retail_b2c_label_total
|
||||||
|
where CORP_COD in (
|
||||||
|
select distinct
|
||||||
|
a.CORP_COD
|
||||||
|
from (select distinct corp_cod from dm.dm_zk_retail_sales_kpi ) a
|
||||||
|
where not exists(
|
||||||
|
select 1 from dm.dm_td_external_corp b
|
||||||
|
where a.CORP_COD = b.CORP_COD
|
||||||
|
)
|
||||||
|
)
|
||||||
|
group by 1
|
||||||
BIN
EXTERNAL/08 dm_td_external_corp.sql:Zone.Identifier
Normal file
BIN
EXTERNAL/08 dm_td_external_corp.sql:Zone.Identifier
Normal file
Binary file not shown.
1968
EXTERNAL/09 dm_td_external_geo_type.sql
Normal file
1968
EXTERNAL/09 dm_td_external_geo_type.sql
Normal file
File diff suppressed because it is too large
Load Diff
BIN
EXTERNAL/09 dm_td_external_geo_type.sql:Zone.Identifier
Normal file
BIN
EXTERNAL/09 dm_td_external_geo_type.sql:Zone.Identifier
Normal file
Binary file not shown.
380
EXTERNAL/11 DM_TD_EXTERNAL_MARKET_NEW.sql
Normal file
380
EXTERNAL/11 DM_TD_EXTERNAL_MARKET_NEW.sql
Normal file
@@ -0,0 +1,380 @@
|
|||||||
|
-- Databricks notebook source
|
||||||
|
-- CREATE OR REPLACE TABLE DM.DM_TD_EXT_UNIONALL_MARKET (
|
||||||
|
-- MARKET STRING,
|
||||||
|
-- DATA_SOURCE STRING,
|
||||||
|
-- TA STRING,
|
||||||
|
-- AZ_RELATED STRING,
|
||||||
|
-- MARKET_DESC STRING,
|
||||||
|
-- TA_RN INT,
|
||||||
|
-- PRD_FLAG STRING,
|
||||||
|
-- ETL_INSERT_DT TIMESTAMP,
|
||||||
|
-- ETL_UPDATE_DT TIMESTAMP)
|
||||||
|
-- USING delta
|
||||||
|
-- -- LOCATION 'abfss://master@azcdatalakeprd.dfs.core.chinacloudapi.cn/DM/dm_td_ext_unionall_market';
|
||||||
|
-- -- 上面是生产环境location,下面是测试环境location
|
||||||
|
-- LOCATION 'abfss://master@retaildlstoragetest.dfs.core.chinacloudapi.cn/DM/dm_td_ext_unionall_market';
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
-- CREATE OR REPLACE TABLE DM.DM_TD_EXT_UNIONALL_MARKET_TA (
|
||||||
|
-- MARKET STRING COMMENT '市场',
|
||||||
|
-- DATA_SOURCE STRING COMMENT '数据源',
|
||||||
|
-- TA STRING COMMENT '治疗领域',
|
||||||
|
-- TA_MAP STRING COMMENT '治疗领域匹配',
|
||||||
|
-- TA_RN INT COMMENT '治疗领域排名',
|
||||||
|
-- MARKET_DESC STRING COMMENT '市场描述',
|
||||||
|
-- ETL_INSERT_DT TIMESTAMP,
|
||||||
|
-- ETL_UPDATE_DT TIMESTAMP
|
||||||
|
-- )
|
||||||
|
-- USING delta
|
||||||
|
-- -- LOCATION 'abfss://master@azcdatalakeprd.dfs.core.chinacloudapi.cn/DM/dm_td_ext_unionall_market_ta';
|
||||||
|
-- -- 上面是生产环境location,下面是测试环境location
|
||||||
|
-- LOCATION 'abfss://master@retaildlstoragetest.dfs.core.chinacloudapi.cn/DM/dm_td_ext_unionall_market_ta';
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
-- DBTITLE 1,pbi页面展示ta标签
|
||||||
|
-- create or replace table tmp.tmp_td_ext_unionall_market_ta_mapping
|
||||||
|
-- (
|
||||||
|
-- market string,
|
||||||
|
-- ta string,
|
||||||
|
-- data_source string);
|
||||||
|
-- INSERT OVERWRITE tmp.tmp_td_ext_unionall_market_ta_mapping (market, ta, data_source) VALUES
|
||||||
|
-- ('CVRM Market', 'CVRM', 'EC(Monthly)'),
|
||||||
|
-- ('NIAD (Non-Insulin Anti-Diabetic) Market', 'DM', 'EC(Monthly)'),
|
||||||
|
-- ('GI Market', 'GI', 'EC(Monthly)'),
|
||||||
|
-- ('RE Market', 'RE', 'EC(Monthly)'),
|
||||||
|
-- ('ATTR_EPL', 'CV', 'DTP(Quarterly)'),
|
||||||
|
-- ('ONC Market', 'ONCO', 'DTP(Quarterly)'),
|
||||||
|
-- ('RARE Market', 'RARE', 'DTP(Quarterly)'),
|
||||||
|
-- ('RE Market', 'RE', 'Retail(Quarterly)'),
|
||||||
|
-- ('CV Market', 'CV', 'Retail(Quarterly)'),
|
||||||
|
-- ('CVRM Market', 'CVRM', 'Retail(Quarterly)'),
|
||||||
|
-- ('RD Market', 'RD', 'Retail(Quarterly)'),
|
||||||
|
-- ('GI Market', 'GI', 'Retail(Quarterly)'),
|
||||||
|
-- ('DM Market', 'DM', 'THC(Quarterly)'),
|
||||||
|
-- ('GI Market', 'GI', 'THC(Quarterly)'),
|
||||||
|
-- ('NIAD (Non-Insulin Anti-Diabetic) Market', 'NIAD', 'THC(Quarterly)'),
|
||||||
|
-- ('RE Market', 'RE', 'THC(Quarterly)'),
|
||||||
|
-- ('ONC Market', 'ONCO', 'THC(Quarterly)'),
|
||||||
|
-- ('CV Market', 'CV', 'THC(Quarterly)'),
|
||||||
|
-- ('RE Market', 'RE', 'XH Data(Quarterly)'),
|
||||||
|
-- ('NIAD (Non-Insulin Anti-Diabetic) Market', 'NIAD', 'XH Data(Quarterly)'),
|
||||||
|
-- ('RD Market', 'RD', 'XH Data(Quarterly)'),
|
||||||
|
-- ('DM Market', 'DM', 'XH Data(Quarterly)'),
|
||||||
|
-- ('ONC Market', 'ONCO', 'XH Data(Quarterly)'),
|
||||||
|
-- ('RARE Market', 'RARE', 'XH Data(Quarterly)'),
|
||||||
|
-- ('CV Market', 'CV', 'XH Data(Quarterly)'),
|
||||||
|
-- ('DM Market', 'DM', 'IQVIA-COUNTY(Quarterly)'),
|
||||||
|
-- ('ONC Market', 'ONCO', 'IQVIA-COUNTY(Quarterly)'),
|
||||||
|
-- ('GI Market', 'GI', 'IQVIA-COUNTY(Quarterly)'),
|
||||||
|
-- ('RE Market', 'RE', 'IQVIA-COUNTY(Quarterly)'),
|
||||||
|
-- ('CV Market', 'CV', 'IQVIA-COUNTY(Quarterly)'),
|
||||||
|
-- ('RARE Market', 'RARE', 'IQVIA-COUNTY(Quarterly)'),
|
||||||
|
-- ('RD Market', 'RD', 'IQVIA-COUNTY(Quarterly)'),
|
||||||
|
-- ('NIAD (Non-Insulin Anti-Diabetic) Market', 'NIAD', 'IQVIA-COUNTY(Quarterly)'),
|
||||||
|
-- ('ONC Market', 'ONCO', 'IQVIA-CHPA(Monthly)'),
|
||||||
|
-- ('RE Market', 'RE', 'IQVIA-CHPA(Monthly)'),
|
||||||
|
-- ('RD Market', 'RD', 'IQVIA-CHPA(Monthly)'),
|
||||||
|
-- ('NIAD (Non-Insulin Anti-Diabetic) Market', 'NIAD', 'IQVIA-CHPA(Monthly)'),
|
||||||
|
-- ('RARE Market', 'RARE', 'IQVIA-CHPA(Monthly)'),
|
||||||
|
-- ('DM Market', 'DM', 'IQVIA-CHPA(Monthly)'),
|
||||||
|
-- ('GI Market', 'GI', 'IQVIA-CHPA(Monthly)'),
|
||||||
|
-- ('CV Market', 'CV', 'IQVIA-CHPA(Monthly)'),
|
||||||
|
-- ('DM Market', 'DM', 'AIA(Monthly)'),
|
||||||
|
-- ('CV Market', 'CV', 'AIA(Monthly)'),
|
||||||
|
-- ('GI Market', 'GI', 'AIA(Monthly)'),
|
||||||
|
-- ('RE Market', 'RE', 'AIA(Monthly)'),
|
||||||
|
-- ('RARE Market', 'RARE', 'AIA(Monthly)'),
|
||||||
|
-- ('ONC Market', 'ONCO', 'AIA(Monthly)'),
|
||||||
|
-- ('NIAD (Non-Insulin Anti-Diabetic) Market', 'NIAD', 'AIA(Monthly)'),
|
||||||
|
-- ('RD Market', 'RD', 'AIA(Monthly)'),
|
||||||
|
-- ('GI Market', 'GI', 'CHC(Quarterly)'),
|
||||||
|
-- ('DM Market', 'DM', 'CHC(Quarterly)'),
|
||||||
|
-- ('RE Market', 'RE', 'CHC(Quarterly)'),
|
||||||
|
-- ('CV Market', 'CV', 'CHC(Quarterly)'),
|
||||||
|
-- ('NIAD (Non-Insulin Anti-Diabetic) Market', 'NIAD', 'CHC(Quarterly)');
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
-- DBTITLE 1,藏数逻辑标签
|
||||||
|
-- -- 建表
|
||||||
|
-- CREATE TABLE IF NOT EXISTS tmp.tmp_td_ext_market_hide_flag (
|
||||||
|
-- data_source VARCHAR(20),
|
||||||
|
-- market VARCHAR(100),
|
||||||
|
-- flag VARCHAR(1)
|
||||||
|
-- );
|
||||||
|
|
||||||
|
-- -- 插入数据(对应 case when 中返回 '1' 的规则)
|
||||||
|
-- INSERT overwrite tmp.tmp_td_ext_market_hide_flag (data_source, market, flag) VALUES
|
||||||
|
-- -- Retail(Quarterly) 部分
|
||||||
|
-- ('Retail(Quarterly)', 'Antacid anti GI swelling agent+PPI Oral', '1'),
|
||||||
|
-- ('Retail(Quarterly)', 'HTN Market', '1'),
|
||||||
|
-- ('Retail(Quarterly)', 'NIAD (Non-Insulin Anti-Diabetic) Market', '1'),
|
||||||
|
-- ('Retail(Quarterly)', 'Respules Market', '1'),
|
||||||
|
-- ('Retail(Quarterly)', 'Statin+XZK Market', '1'),
|
||||||
|
-- ('Retail(Quarterly)', 'BUDESONIDE Market', '1'),
|
||||||
|
-- ('Retail(Quarterly)', 'Rosuvastatin Market', '1'),
|
||||||
|
-- ('Retail(Quarterly)', 'Atorvastatin Market', '1'),
|
||||||
|
-- ('Retail(Quarterly)', 'Metoprolol Succinate Market', '1'),
|
||||||
|
-- ('Retail(Quarterly)', 'PPI Oral ESOMEPRAZOLE Market', '1'),
|
||||||
|
-- ('Retail(Quarterly)', 'PPI Oral OMEPRAZOLE Market', '1'),
|
||||||
|
-- ('Retail(Quarterly)', 'Inhaled Extended Market', '1'),
|
||||||
|
-- ('Retail(Quarterly)', 'Non-Omeprazole', '1'),
|
||||||
|
-- ('Retail(Quarterly)', 'Metoprolol Tartrate Market', '1'),
|
||||||
|
-- ('Retail(Quarterly)', 'Pediatric Cough Market', '1'),
|
||||||
|
-- ('Retail(Quarterly)', 'Anti-HER2 Market', '1'),
|
||||||
|
-- ('Retail(Quarterly)', 'Anti-HER2 mBC Market', '1'),
|
||||||
|
-- ('Retail(Quarterly)', 'Fasenra Market', '1'),
|
||||||
|
-- ('Retail(Quarterly)', 'Severe Asthma', '1'),
|
||||||
|
-- ('Retail(Quarterly)', 'DAPA Market', '1'),
|
||||||
|
|
||||||
|
-- -- DTP(Quarterly) 部分
|
||||||
|
-- ('DTP(Quarterly)', 'EGFR TKI Market', '1'),
|
||||||
|
-- ('DTP(Quarterly)', 'Anti-HER2 Market', '1'),
|
||||||
|
-- ('DTP(Quarterly)', 'Anti-HER2 mBC Market', '1'),
|
||||||
|
-- ('DTP(Quarterly)', 'Fasenra/Tezspire Related Market', '1'),
|
||||||
|
-- ('DTP(Quarterly)', 'Severe Asthma', '1'),
|
||||||
|
-- ('DTP(Quarterly)', 'Tezspire CRSwNP Market', '1'),
|
||||||
|
-- ('DTP(Quarterly)', 'CRSwNP Market', '1'),
|
||||||
|
-- ('DTP(Quarterly)', 'PAM Market', '1');
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
-- ============================================================
|
||||||
|
-- 目标表:dm.dm_td_ext_unionall_market
|
||||||
|
-- 功能:整合各数据源的市场、TA、AZ关联、产品标志等维度信息
|
||||||
|
-- 调度:全量覆盖写入(INSERT OVERWRITE)
|
||||||
|
-- 作者/更新:chenwu
|
||||||
|
-- ============================================================
|
||||||
|
INSERT OVERWRITE dm.dm_td_ext_unionall_market (
|
||||||
|
MARKET, DATA_SOURCE, TA, AZ_RELATED, MARKET_DESC, TA_RN, PRD_FLAG, ETL_INSERT_DT, ETL_UPDATE_DT
|
||||||
|
)
|
||||||
|
|
||||||
|
WITH
|
||||||
|
|
||||||
|
-- ------------------------------------------------------------
|
||||||
|
-- market_info:从包映射表取市场-数据源组合,关联竞品表补充市场描述
|
||||||
|
-- 用 MAX(MARKET_DESC) 避免一对多 JOIN 导致重复行
|
||||||
|
-- ------------------------------------------------------------
|
||||||
|
market_info AS (
|
||||||
|
SELECT
|
||||||
|
t1.MARKET,
|
||||||
|
t1.DATASOURCE AS DATA_SOURCE,
|
||||||
|
MAX(t2.MARKETDESC) AS MARKET_DESC
|
||||||
|
FROM dm.dm_td_ext_unionall_market_pack_mapping t1
|
||||||
|
LEFT JOIN dm.dm_td_external_keycompatitor t2
|
||||||
|
ON t1.MARKET = t2.MARKET
|
||||||
|
GROUP BY 1, 2
|
||||||
|
),
|
||||||
|
|
||||||
|
-- ------------------------------------------------------------
|
||||||
|
-- TA_info:从市场属性表取市场-TA-排序,过滤占位行(MARKET_ORDER=999)
|
||||||
|
-- 末尾 UNION ALL 补充"ALL Market"汇总行
|
||||||
|
-- 改用 SELECT DISTINCT 替代无聚合的 GROUP BY
|
||||||
|
-- ------------------------------------------------------------
|
||||||
|
TA_info AS (
|
||||||
|
SELECT DISTINCT
|
||||||
|
MARKET,
|
||||||
|
TA,
|
||||||
|
TA_ORDER
|
||||||
|
FROM DM.DM_IMS_TD_MARKET_PROPERTY
|
||||||
|
WHERE MARKET_ORDER <> 999
|
||||||
|
|
||||||
|
UNION ALL
|
||||||
|
|
||||||
|
SELECT
|
||||||
|
'ALL Market' AS MARKET,
|
||||||
|
'ALL' AS TA,
|
||||||
|
-1 AS TA_ORDER
|
||||||
|
),
|
||||||
|
|
||||||
|
-- ------------------------------------------------------------
|
||||||
|
-- AZ_RELATED_info:从临时映射表取市场-数据源-AZ关联关系(已去重)
|
||||||
|
-- ------------------------------------------------------------
|
||||||
|
AZ_RELATED_info AS (
|
||||||
|
SELECT DISTINCT
|
||||||
|
MARKET,
|
||||||
|
TA AS AZ_RELATED,
|
||||||
|
DATA_SOURCE
|
||||||
|
FROM tmp.tmp_td_ext_unionall_market_ta_mapping
|
||||||
|
)
|
||||||
|
|
||||||
|
-- ------------------------------------------------------------
|
||||||
|
-- 主查询:以 market_info 为驱动表,依次关联 TA、AZ关联、隐藏标志
|
||||||
|
-- hide_flag 为小维表,添加 BROADCAST hint 避免 shuffle
|
||||||
|
-- PRD_FLAG=1 表示市场在隐藏标志表中存在(需隐藏/特殊处理)
|
||||||
|
-- ------------------------------------------------------------
|
||||||
|
SELECT /*+ BROADCAST(t4) */
|
||||||
|
DISTINCT
|
||||||
|
t1.MARKET,
|
||||||
|
t1.DATA_SOURCE,
|
||||||
|
t2.TA,
|
||||||
|
t3.AZ_RELATED,
|
||||||
|
t1.MARKET_DESC,
|
||||||
|
t2.TA_ORDER AS TA_RN,
|
||||||
|
IF(t4.MARKET IS NULL, 0, 1) AS PRD_FLAG,
|
||||||
|
FROM_UTC_TIMESTAMP(CURRENT_TIMESTAMP(), 'UTC+8') AS ETL_INSERT_DT,
|
||||||
|
FROM_UTC_TIMESTAMP(CURRENT_TIMESTAMP(), 'UTC+8') AS ETL_UPDATE_DT
|
||||||
|
FROM market_info t1
|
||||||
|
LEFT JOIN TA_info t2
|
||||||
|
ON t1.MARKET = t2.MARKET
|
||||||
|
LEFT JOIN AZ_RELATED_info t3
|
||||||
|
ON t1.MARKET = t3.MARKET
|
||||||
|
AND t1.DATA_SOURCE = t3.DATA_SOURCE
|
||||||
|
LEFT JOIN
|
||||||
|
tmp.tmp_td_ext_market_hide_flag t4
|
||||||
|
ON t1.MARKET = t4.MARKET
|
||||||
|
AND t1.DATA_SOURCE = t4.DATA_SOURCE
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
insert overwrite dm.dm_td_ext_unionall_market_ta (
|
||||||
|
MARKET, DATA_SOURCE, TA, TA_RN, MARKET_DESC, ETL_INSERT_DT, ETL_UPDATE_DT
|
||||||
|
)
|
||||||
|
SELECT
|
||||||
|
MARKET,
|
||||||
|
DATA_SOURCE,
|
||||||
|
AZ_RELATED TA,
|
||||||
|
TA_RN,
|
||||||
|
MARKET_DESC,
|
||||||
|
FROM_UTC_TIMESTAMP(CURRENT_TIMESTAMP(), 'UTC+8') AS ETL_INSERT_DT,
|
||||||
|
FROM_UTC_TIMESTAMP(CURRENT_TIMESTAMP(), 'UTC+8') AS ETL_UPDATE_DT
|
||||||
|
FROM
|
||||||
|
DM.dm_td_ext_unionall_market
|
||||||
|
-- WHERE MARKET NOT LIKE '%ALL Market%' and MARKET <>'Retail All Market'
|
||||||
|
-- UNION all
|
||||||
|
-- SELECT
|
||||||
|
-- MARKET,
|
||||||
|
-- DATA_SOURCE,
|
||||||
|
-- 'CVRM' TA,
|
||||||
|
-- 0 TA_RN,
|
||||||
|
-- MARKET_DESC,
|
||||||
|
-- FROM_UTC_TIMESTAMP(CURRENT_TIMESTAMP(), 'UTC+8') AS ETL_INSERT_DT,
|
||||||
|
-- FROM_UTC_TIMESTAMP(CURRENT_TIMESTAMP(), 'UTC+8') AS ETL_UPDATE_DT
|
||||||
|
-- FROM
|
||||||
|
-- DM.dm_td_ext_unionall_market
|
||||||
|
-- WHERE
|
||||||
|
-- MARKET = 'CVRM Market'
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
-----------------------------------------------------------------------------------
|
||||||
|
--修改时间:20240822
|
||||||
|
--修改人:FanXujia
|
||||||
|
--修改内容:
|
||||||
|
--Retail、EC渠道,新增NIAD这个TA。数据与DM相同
|
||||||
|
-----------------------------------------------------------------------------------
|
||||||
|
insert into dm.dm_td_ext_unionall_market_ta (
|
||||||
|
MARKET, DATA_SOURCE, TA, TA_RN, MARKET_DESC, ETL_INSERT_DT, ETL_UPDATE_DT
|
||||||
|
)
|
||||||
|
select
|
||||||
|
market,
|
||||||
|
DATA_SOURCE,
|
||||||
|
'NIAD' as TA,
|
||||||
|
7 as TA_RN,
|
||||||
|
MARKET_DESC,
|
||||||
|
FROM_UTC_TIMESTAMP(CURRENT_TIMESTAMP(), 'UTC+8') AS ETL_INSERT_DT,
|
||||||
|
FROM_UTC_TIMESTAMP(CURRENT_TIMESTAMP(), 'UTC+8') AS ETL_UPDATE_DT
|
||||||
|
from
|
||||||
|
dm.dm_td_ext_unionall_market_ta
|
||||||
|
where
|
||||||
|
DATA_SOURCE = 'Retail(Quarterly)'
|
||||||
|
and ta = 'DM'
|
||||||
|
union all
|
||||||
|
select
|
||||||
|
market,
|
||||||
|
DATA_SOURCE,
|
||||||
|
'NIAD' as TA,
|
||||||
|
7 as TA_RN,
|
||||||
|
MARKET_DESC,
|
||||||
|
FROM_UTC_TIMESTAMP(CURRENT_TIMESTAMP(), 'UTC+8') AS ETL_INSERT_DT,
|
||||||
|
FROM_UTC_TIMESTAMP(CURRENT_TIMESTAMP(), 'UTC+8') AS ETL_UPDATE_DT
|
||||||
|
from
|
||||||
|
dm.dm_td_ext_unionall_market_ta
|
||||||
|
where
|
||||||
|
DATA_SOURCE = 'EC(Monthly)'
|
||||||
|
and ta = 'DM'
|
||||||
|
--20250701 chenwu 新增RARE = ONCO的Soliris Related Market
|
||||||
|
union all
|
||||||
|
select
|
||||||
|
market,
|
||||||
|
DATA_SOURCE,
|
||||||
|
'RARE' as TA,
|
||||||
|
8 as TA_RN,
|
||||||
|
MARKET_DESC,
|
||||||
|
FROM_UTC_TIMESTAMP(CURRENT_TIMESTAMP(), 'UTC+8') AS ETL_INSERT_DT,
|
||||||
|
FROM_UTC_TIMESTAMP(CURRENT_TIMESTAMP(), 'UTC+8') AS ETL_UPDATE_DT
|
||||||
|
from
|
||||||
|
dm.dm_td_ext_unionall_market_ta
|
||||||
|
where
|
||||||
|
DATA_SOURCE = 'DTP(Quarterly)'
|
||||||
|
and ta = 'ONCO'
|
||||||
|
and MARKET = 'Soliris Related Market';
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
-- log 20250226 auth:庄伟
|
||||||
|
-- 对market_ta表中市场进行补全,并依据手工文件维护的 market 与 ta 映射,匹配出完整市场对应的TA_NAME
|
||||||
|
insert overwrite table dm.dm_td_ext_unionall_market_ta (
|
||||||
|
MARKET, DATA_SOURCE, TA, TA_MAP, TA_RN, MARKET_DESC, ETL_INSERT_DT, ETL_UPDATE_DT
|
||||||
|
)
|
||||||
|
-----找出在market维度表中不存在的market列表
|
||||||
|
with temp_ma_ext_market as (select distinct
|
||||||
|
MARKET,
|
||||||
|
DATA_SOURCE
|
||||||
|
from
|
||||||
|
dm.dm_td_ext_unionall_market
|
||||||
|
where
|
||||||
|
MARKET NOT IN (
|
||||||
|
select distinct
|
||||||
|
MARKET
|
||||||
|
from
|
||||||
|
dm.dm_td_ext_unionall_market_ta
|
||||||
|
)
|
||||||
|
)
|
||||||
|
------补充完整的market列表,并通过market匹配手工文件维护的market对应的TA
|
||||||
|
select
|
||||||
|
distinct
|
||||||
|
dim_mkt_ta.MARKET,
|
||||||
|
DATA_SOURCE,
|
||||||
|
dim_mkt_ta.TA,
|
||||||
|
case
|
||||||
|
when dim_mkt_ta.MARKET = 'ALL Market' then 'ALL'
|
||||||
|
else nvl(mkt_property.ta, mkt_property_key.TA)
|
||||||
|
end as TA_MAP,
|
||||||
|
dim_mkt_ta.TA_RN,
|
||||||
|
dim_mkt_ta.MARKET_DESC,
|
||||||
|
FROM_UTC_TIMESTAMP(CURRENT_TIMESTAMP(), 'UTC+8') AS ETL_INSERT_DT,
|
||||||
|
FROM_UTC_TIMESTAMP(CURRENT_TIMESTAMP(), 'UTC+8') AS ETL_UPDATE_DT
|
||||||
|
from
|
||||||
|
(
|
||||||
|
select
|
||||||
|
MARKET,
|
||||||
|
DATA_SOURCE,
|
||||||
|
TA,
|
||||||
|
TA_RN,
|
||||||
|
MARKET_DESC
|
||||||
|
from
|
||||||
|
dm.dm_td_ext_unionall_market_ta
|
||||||
|
) dim_mkt_ta
|
||||||
|
left join dm.dm_ims_td_market_property mkt_property
|
||||||
|
on dim_mkt_ta.market = mkt_property.market
|
||||||
|
left join dm.dm_ims_td_market_property mkt_property_key
|
||||||
|
on dim_mkt_ta.MARKET = mkt_property_key.MARKET_KEY
|
||||||
|
union all
|
||||||
|
select
|
||||||
|
ext_market.MARKET,
|
||||||
|
ext_market.DATA_SOURCE,
|
||||||
|
null TA,
|
||||||
|
mkt_property.TA TA_MAP,
|
||||||
|
'99' as TA_RN,
|
||||||
|
null MARKET_DESC,
|
||||||
|
FROM_UTC_TIMESTAMP(CURRENT_TIMESTAMP(), 'UTC+8') AS ETL_INSERT_DT,
|
||||||
|
FROM_UTC_TIMESTAMP(CURRENT_TIMESTAMP(), 'UTC+8') AS ETL_UPDATE_DT
|
||||||
|
from
|
||||||
|
temp_ma_ext_market ext_market
|
||||||
|
left join dm.dm_ims_td_market_property mkt_property
|
||||||
|
on ext_market.market = mkt_property.MARKET
|
||||||
|
where
|
||||||
|
nvl(mkt_property.TA, '') <> '';
|
||||||
BIN
EXTERNAL/11 DM_TD_EXTERNAL_MARKET_NEW.sql:Zone.Identifier
Normal file
BIN
EXTERNAL/11 DM_TD_EXTERNAL_MARKET_NEW.sql:Zone.Identifier
Normal file
Binary file not shown.
358
EXTERNAL/12 dm_td_external_org.sql
Normal file
358
EXTERNAL/12 dm_td_external_org.sql
Normal file
@@ -0,0 +1,358 @@
|
|||||||
|
-- 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
|
||||||
BIN
EXTERNAL/12 dm_td_external_org.sql:Zone.Identifier
Normal file
BIN
EXTERNAL/12 dm_td_external_org.sql:Zone.Identifier
Normal file
Binary file not shown.
1230
EXTERNAL/13 external auth.sql
Normal file
1230
EXTERNAL/13 external auth.sql
Normal file
File diff suppressed because it is too large
Load Diff
BIN
EXTERNAL/13 external auth.sql:Zone.Identifier
Normal file
BIN
EXTERNAL/13 external auth.sql:Zone.Identifier
Normal file
Binary file not shown.
22
EXTERNAL/14 dm_tf_external_retail_special_bkp.sql
Normal file
22
EXTERNAL/14 dm_tf_external_retail_special_bkp.sql
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
-- Databricks notebook source
|
||||||
|
insert overwrite table dm.dm_tf_external_retail_special
|
||||||
|
--dtp 部分的数据 EGFR TKI 需要直取
|
||||||
|
select
|
||||||
|
case when a.market = 'EGFR TKI' then 'EGFR TKI Market' else a.market end as market
|
||||||
|
,a.province_city
|
||||||
|
,a.audit_key
|
||||||
|
,a.audit_source
|
||||||
|
,a.date_key
|
||||||
|
,a.yyyymm
|
||||||
|
,a.sales_value
|
||||||
|
,a.sales_volume
|
||||||
|
,a.pdot_counting_unit
|
||||||
|
,c.sales_value sales_value_ly
|
||||||
|
,c.sales_volume sales_volume_ly
|
||||||
|
,c.pdot_counting_unit pdot_counting_unit_ly
|
||||||
|
,'dtp' `source`
|
||||||
|
from dws.dws_tf_external_retail_dtp_special a
|
||||||
|
left join dws.dws_tf_external_retail_dtp_special c --取去年同期数据
|
||||||
|
on a.province_city = c.province_city
|
||||||
|
and a.market = c.market
|
||||||
|
and (a.yyyymm-100) = c.yyyymm
|
||||||
Binary file not shown.
333
EXTERNAL/AIA/01 dm_aia_pack_property.sql
Normal file
333
EXTERNAL/AIA/01 dm_aia_pack_property.sql
Normal file
@@ -0,0 +1,333 @@
|
|||||||
|
-- Databricks notebook source
|
||||||
|
-- CREATE OR REPLACE TABLE dm.dm_aia_pack_property (
|
||||||
|
-- PACK_COD STRING,
|
||||||
|
-- pack_des STRING,
|
||||||
|
-- stgh_des STRING,
|
||||||
|
-- pack_lch STRING,
|
||||||
|
-- PROD_COD STRING,
|
||||||
|
-- prod_des STRING,
|
||||||
|
-- prod_des_c STRING,
|
||||||
|
-- Family_Code STRING,
|
||||||
|
-- Family_Name STRING,
|
||||||
|
-- cmps_cod STRING,
|
||||||
|
-- cmps_des STRING,
|
||||||
|
-- cmps_des_c STRING,
|
||||||
|
-- atc1_cod STRING,
|
||||||
|
-- atc1_des STRING,
|
||||||
|
-- atc1_des_c STRING,
|
||||||
|
-- atc2_cod STRING,
|
||||||
|
-- atc2_des STRING,
|
||||||
|
-- atc2_des_c STRING,
|
||||||
|
-- atc3_cod STRING,
|
||||||
|
-- atc3_des STRING,
|
||||||
|
-- atc3_des_c STRING,
|
||||||
|
-- atc4_cod STRING,
|
||||||
|
-- atc4_des STRING,
|
||||||
|
-- atc4_des_c STRING,
|
||||||
|
-- app1_cod STRING,
|
||||||
|
-- app1_des STRING,
|
||||||
|
-- app1_des_c STRING,
|
||||||
|
-- app2_cod STRING,
|
||||||
|
-- app2_des STRING,
|
||||||
|
-- app2_des_c STRING,
|
||||||
|
-- app3_cod STRING,
|
||||||
|
-- app3_des STRING,
|
||||||
|
-- app3_des_c STRING,
|
||||||
|
-- bio_desc STRING,
|
||||||
|
-- gene_orig_desc STRING,
|
||||||
|
-- eth_otc_desc STRING,
|
||||||
|
-- nrdl_desc STRING,
|
||||||
|
-- NRDL_Entry_Date STRING,
|
||||||
|
-- edl_desc STRING,
|
||||||
|
-- tcm_desc STRING,
|
||||||
|
-- paed_desc STRING,
|
||||||
|
-- gqce_desc STRING,
|
||||||
|
-- vbp_desc STRING,
|
||||||
|
-- manu_cod STRING,
|
||||||
|
-- manu_des STRING,
|
||||||
|
-- manu_des_c STRING,
|
||||||
|
-- mnfl_cod STRING,
|
||||||
|
-- mnfl_des STRING,
|
||||||
|
-- corp_cod STRING,
|
||||||
|
-- corp_des STRING,
|
||||||
|
-- corp_des_c STRING,
|
||||||
|
-- BrandType STRING,
|
||||||
|
-- IS_AZ STRING,
|
||||||
|
-- AZ_MAIN STRING,
|
||||||
|
-- AZ_Related STRING,
|
||||||
|
-- COUNTING_UNIT STRING,
|
||||||
|
-- ETL_INSERT_DT TIMESTAMP,
|
||||||
|
-- ETL_UPDATE_DT TIMESTAMP)
|
||||||
|
-- USING delta
|
||||||
|
-- -- LOCATION 'abfss://master@azcdatalakeprd.dfs.core.chinacloudapi.cn/DM/dm_aia_pack_property';
|
||||||
|
-- -- 上面是生产环境location,下面是测试环境location
|
||||||
|
-- LOCATION 'abfss://master@retaildlstoragetest.dfs.core.chinacloudapi.cn/DM/dm_aia_pack_property';
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
--对用户上传的表进行去重处理
|
||||||
|
--对部分编码字段进行补0
|
||||||
|
create or replace temporary view aia_pack_property_distinct as
|
||||||
|
select distinct
|
||||||
|
iqvia_lineno,
|
||||||
|
case when length(trim(iqvia_pack_code)) < 12 and trim(iqvia_pack_code) REGEXP '^[0-9]' then right(concat('000000000000',trim(iqvia_pack_code)),12) else trim(iqvia_pack_code) end as iqvia_pack_code,
|
||||||
|
case when length(iqvia_prod_code) < 9 then right(concat('000000000',iqvia_prod_code),9) else iqvia_prod_code end as iqvia_prod_code,
|
||||||
|
iqvia_notes,
|
||||||
|
IS_NEW_PRD,
|
||||||
|
IS_IN_CHPA,
|
||||||
|
ATC1,
|
||||||
|
ATC2,
|
||||||
|
ATC3,
|
||||||
|
ATC4,
|
||||||
|
org_prd_cd,
|
||||||
|
org_mole_nm_c,
|
||||||
|
org_manu_prd_nm_c,
|
||||||
|
org_prd_nm_c,
|
||||||
|
org_pk,
|
||||||
|
org_prd_str,
|
||||||
|
org_pth,
|
||||||
|
min_pk_unit,
|
||||||
|
org_pk_unit,
|
||||||
|
org_manu_nm,
|
||||||
|
app1_cod,
|
||||||
|
app1_des,
|
||||||
|
app1_des_c,
|
||||||
|
app2_cod,
|
||||||
|
app2_des,
|
||||||
|
app2_des_c,
|
||||||
|
app3_cod,
|
||||||
|
app3_des,
|
||||||
|
app3_des_c,
|
||||||
|
atc1_cod,
|
||||||
|
atc1_des,
|
||||||
|
atc1_des_c,
|
||||||
|
atc2_cod,
|
||||||
|
atc2_des,
|
||||||
|
atc2_des_c,
|
||||||
|
atc3_cod,
|
||||||
|
atc3_des,
|
||||||
|
atc3_des_c,
|
||||||
|
atc4_cod,
|
||||||
|
atc4_des,
|
||||||
|
atc4_des_c,
|
||||||
|
bio_desc,
|
||||||
|
case when length(cmps_cod) < 6 then right(concat('000000',cmps_cod),6) else cmps_cod end as cmps_cod,
|
||||||
|
cmps_des,
|
||||||
|
cmps_des_c,
|
||||||
|
corp_cod,
|
||||||
|
corp_des,
|
||||||
|
corp_des_c,
|
||||||
|
edl_desc,
|
||||||
|
eth_otc_desc,
|
||||||
|
gene_orig_desc,
|
||||||
|
gqce_desc,
|
||||||
|
manu_cod,
|
||||||
|
manu_des,
|
||||||
|
manu_des_c,
|
||||||
|
mnfl_cod,
|
||||||
|
mnfl_des,
|
||||||
|
nrdl_desc,
|
||||||
|
pack_des,
|
||||||
|
stgh_des,
|
||||||
|
pack_lch,
|
||||||
|
paed_desc,
|
||||||
|
prod_des,
|
||||||
|
prod_des_c,
|
||||||
|
tcm_desc,
|
||||||
|
vbp_desc,
|
||||||
|
unit,
|
||||||
|
counting_unit,
|
||||||
|
dosage_unit
|
||||||
|
from dwd.dwd_gnd_ext_aia_cpt_data;
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
-- create or replace table dm.dm_aia_pack_property
|
||||||
|
insert overwrite table dm.dm_aia_pack_property
|
||||||
|
WITH dedup_l AS (
|
||||||
|
SELECT *,
|
||||||
|
ROW_NUMBER() OVER (PARTITION BY iqvia_pack_code ORDER BY iqvia_pack_code) AS rn
|
||||||
|
FROM aia_pack_property_distinct
|
||||||
|
QUALIFY rn = 1
|
||||||
|
),
|
||||||
|
dedup_r AS (
|
||||||
|
SELECT *,
|
||||||
|
ROW_NUMBER() OVER (PARTITION BY PACK_COD ORDER BY PACK_COD) AS rn
|
||||||
|
FROM tmp.tmp_ext_chpa_pack_propetry
|
||||||
|
QUALIFY rn = 1
|
||||||
|
)
|
||||||
|
SELECT
|
||||||
|
l.iqvia_pack_code AS PACK_COD,
|
||||||
|
|
||||||
|
-- Pack / Product 描述
|
||||||
|
COALESCE(r.PACK_DES, l.pack_des) AS pack_des,
|
||||||
|
COALESCE(r.STGH_DES, l.stgh_des) AS stgh_des,
|
||||||
|
COALESCE(r.PACK_LCH, l.pack_lch) AS pack_lch,
|
||||||
|
COALESCE(r.PROD_COD, l.iqvia_prod_code) AS PROD_COD,
|
||||||
|
COALESCE(r.PROD_DES, l.prod_des) AS prod_des,
|
||||||
|
COALESCE(r.PROD_DES_C, l.prod_des_c) AS prod_des_c,
|
||||||
|
|
||||||
|
-- 右表独有:Family
|
||||||
|
r.Family_Code,
|
||||||
|
r.Family_Name,
|
||||||
|
|
||||||
|
-- 成分
|
||||||
|
COALESCE(r.CMPS_COD, l.cmps_cod) AS cmps_cod,
|
||||||
|
COALESCE(r.CMPS_DES, l.cmps_des) AS cmps_des,
|
||||||
|
COALESCE(r.CMPS_DES_C, l.cmps_des_c) AS cmps_des_c,
|
||||||
|
|
||||||
|
-- ATC
|
||||||
|
COALESCE(r.ATC1_COD, l.atc1_cod) AS atc1_cod,
|
||||||
|
COALESCE(r.atc1_des, l.atc1_des) AS atc1_des,
|
||||||
|
COALESCE(r.atc1_des_c, l.atc1_des_c) AS atc1_des_c,
|
||||||
|
COALESCE(r.ATC2_COD, l.atc2_cod) AS atc2_cod,
|
||||||
|
COALESCE(r.atc2_des, l.atc2_des) AS atc2_des,
|
||||||
|
COALESCE(r.atc2_des_c, l.atc2_des_c) AS atc2_des_c,
|
||||||
|
COALESCE(r.ATC3_COD, l.atc3_cod) AS atc3_cod,
|
||||||
|
COALESCE(r.atc3_des, l.atc3_des) AS atc3_des,
|
||||||
|
COALESCE(r.atc3_des_c, l.atc3_des_c) AS atc3_des_c,
|
||||||
|
COALESCE(r.ATC4_COD, l.atc4_cod) AS atc4_cod,
|
||||||
|
COALESCE(r.atc4_des, l.atc4_des) AS atc4_des,
|
||||||
|
COALESCE(r.atc4_des_c, l.atc4_des_c) AS atc4_des_c,
|
||||||
|
|
||||||
|
-- APP
|
||||||
|
COALESCE(r.APP1_COD, l.app1_cod) AS app1_cod,
|
||||||
|
COALESCE(r.app1_des, l.app1_des) AS app1_des,
|
||||||
|
COALESCE(r.app1_des_c, l.app1_des_c) AS app1_des_c,
|
||||||
|
COALESCE(r.APP2_COD, l.app2_cod) AS app2_cod,
|
||||||
|
COALESCE(r.app2_des, l.app2_des) AS app2_des,
|
||||||
|
COALESCE(r.app2_des_c, l.app2_des_c) AS app2_des_c,
|
||||||
|
COALESCE(r.APP3_COD, l.app3_cod) AS app3_cod,
|
||||||
|
COALESCE(r.app3_des, l.app3_des) AS app3_des,
|
||||||
|
COALESCE(r.app3_des_c, l.app3_des_c) AS app3_des_c,
|
||||||
|
|
||||||
|
-- 标志位
|
||||||
|
COALESCE(r.BIO_DESC, l.bio_desc) AS bio_desc,
|
||||||
|
COALESCE(r.GENE_ORIG_DESC, l.gene_orig_desc) AS gene_orig_desc,
|
||||||
|
COALESCE(r.ETH_OTC_DESC, l.eth_otc_desc) AS eth_otc_desc,
|
||||||
|
COALESCE(r.NRDL_DESC, l.nrdl_desc) AS nrdl_desc,
|
||||||
|
r.NRDL_Entry_Date,
|
||||||
|
COALESCE(r.EDL_DESC, l.edl_desc) AS edl_desc,
|
||||||
|
COALESCE(r.TCM_DESC, l.tcm_desc) AS tcm_desc,
|
||||||
|
COALESCE(r.PAED_DESC, l.paed_desc) AS paed_desc,
|
||||||
|
COALESCE(r.GQCE_DESC, l.gqce_desc) AS gqce_desc,
|
||||||
|
COALESCE(r.VBP_DESC, l.vbp_desc) AS vbp_desc,
|
||||||
|
|
||||||
|
-- 厂商
|
||||||
|
COALESCE(r.MANU_COD, l.manu_cod) AS manu_cod,
|
||||||
|
COALESCE(r.MANU_DES, l.manu_des) AS manu_des,
|
||||||
|
COALESCE(r.MANU_DES_C, l.manu_des_c) AS manu_des_c,
|
||||||
|
COALESCE(r.MNFL_COD, l.mnfl_cod) AS mnfl_cod,
|
||||||
|
COALESCE(r.MNFL_DES, l.mnfl_des) AS mnfl_des,
|
||||||
|
|
||||||
|
-- 公司
|
||||||
|
COALESCE(r.CORP_COD, l.corp_cod) AS corp_cod,
|
||||||
|
COALESCE(r.CORP_DES, l.corp_des) AS corp_des,
|
||||||
|
COALESCE(r.CORP_DES_C, l.corp_des_c) AS corp_des_c,
|
||||||
|
|
||||||
|
-- 右表独有:品牌 / AZ 标志
|
||||||
|
r.BrandType,
|
||||||
|
r.IS_AZ,
|
||||||
|
r.AZ_MAIN,
|
||||||
|
r.AZ_Related,
|
||||||
|
-- 左表独有
|
||||||
|
l.COUNTING_UNIT,
|
||||||
|
from_utc_timestamp(current_timestamp(),'UTC+8') as ETL_INSERT_DT,
|
||||||
|
from_utc_timestamp(current_timestamp(),'UTC+8') as ETL_UPDATE_DT
|
||||||
|
FROM dedup_l l
|
||||||
|
LEFT JOIN dedup_r r
|
||||||
|
ON l.iqvia_pack_code = r.PACK_COD
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
-- -----------------------------------------------------------
|
||||||
|
-- --修改时间:20241118
|
||||||
|
-- --修改人:Fanxujia
|
||||||
|
-- --修改内容:
|
||||||
|
-- --使用手工文件排除掉每个渠道的不需要显示的市场
|
||||||
|
-- --由于AIA涉及后面的Flag逻辑、NEW AIA Dashboard,因此不能直接在最后的DM表进行限制,需要在这里限制。
|
||||||
|
-- -----------------------------------------------------------
|
||||||
|
-- with hidden_market as (
|
||||||
|
-- select distinct
|
||||||
|
-- upper(hidden_market) as hidden_market
|
||||||
|
-- from dwd.dwd_gnd_ims_hidden_market
|
||||||
|
-- where upper(data_source) = 'AIA'
|
||||||
|
-- )
|
||||||
|
-- ,tmp as (
|
||||||
|
-- select
|
||||||
|
-- MARKET_PACK_KEY,
|
||||||
|
-- PACK_COD,
|
||||||
|
-- PACK_DES,
|
||||||
|
-- STGH_DES,
|
||||||
|
-- PACK_LCH,
|
||||||
|
-- Family_Code,
|
||||||
|
-- Family_Name,
|
||||||
|
-- PROD_COD,
|
||||||
|
-- PROD_DES,
|
||||||
|
-- PROD_DES_C,
|
||||||
|
-- CMPS_COD,
|
||||||
|
-- CMPS_DES,
|
||||||
|
-- CMPS_DES_C,
|
||||||
|
-- ATC1_COD,
|
||||||
|
-- ATC2_COD,
|
||||||
|
-- ATC3_COD,
|
||||||
|
-- ATC4_COD,
|
||||||
|
-- APP1_COD,
|
||||||
|
-- APP2_COD,
|
||||||
|
-- APP3_COD,
|
||||||
|
-- BIO_DESC,
|
||||||
|
-- GENE_ORIG_DESC,
|
||||||
|
-- ETH_OTC_DESC,
|
||||||
|
-- NRDL_DESC,
|
||||||
|
-- NRDL_Entry_Date,
|
||||||
|
-- EDL_DESC,
|
||||||
|
-- TCM_DESC,
|
||||||
|
-- PAED_DESC,
|
||||||
|
-- GQCE_DESC,
|
||||||
|
-- VBP_DESC,
|
||||||
|
-- MANU_COD,
|
||||||
|
-- MANU_DES,
|
||||||
|
-- MANU_DES_C,
|
||||||
|
-- MNFL_COD,
|
||||||
|
-- MNFL_DES,
|
||||||
|
-- CORP_COD,
|
||||||
|
-- CORP_DES,
|
||||||
|
-- CORP_DES_C,
|
||||||
|
-- BrandType,
|
||||||
|
-- t1.MARKET,
|
||||||
|
-- t1.MARKET_CHPA,
|
||||||
|
-- KEY_COMPETITOR,
|
||||||
|
-- IS_AZ,
|
||||||
|
-- AZ_MAIN,
|
||||||
|
-- AZ_Related,
|
||||||
|
-- atc1_des,
|
||||||
|
-- atc1_des_c,
|
||||||
|
-- atc2_des,
|
||||||
|
-- atc2_des_c,
|
||||||
|
-- atc3_des,
|
||||||
|
-- atc3_des_c,
|
||||||
|
-- atc4_des,
|
||||||
|
-- atc4_des_c,
|
||||||
|
-- app1_des,
|
||||||
|
-- app1_des_c,
|
||||||
|
-- app2_des,
|
||||||
|
-- app2_des_c,
|
||||||
|
-- app3_des,
|
||||||
|
-- app3_des_c,
|
||||||
|
-- Class,
|
||||||
|
-- ETL_INSERT_DT,
|
||||||
|
-- ETL_UPDATE_DT,
|
||||||
|
-- market_ratio,
|
||||||
|
-- counting_unit
|
||||||
|
-- from dm.dm_aia_pack_property t1
|
||||||
|
-- left join hidden_market t3
|
||||||
|
-- on upper(t1.MARKET) = t3.hidden_market
|
||||||
|
-- where t3.hidden_market is null
|
||||||
|
-- union
|
||||||
|
-- select *
|
||||||
|
-- from dm.dm_aia_pack_property
|
||||||
|
-- where MARKET = 'AIA ALL Market'
|
||||||
|
-- )
|
||||||
|
|
||||||
|
-- insert overwrite dm.dm_aia_pack_property
|
||||||
|
-- select * from tmp
|
||||||
BIN
EXTERNAL/AIA/01 dm_aia_pack_property.sql:Zone.Identifier
Normal file
BIN
EXTERNAL/AIA/01 dm_aia_pack_property.sql:Zone.Identifier
Normal file
Binary file not shown.
710
EXTERNAL/AIA/02 dm_ext_aia_sales.sql
Normal file
710
EXTERNAL/AIA/02 dm_ext_aia_sales.sql
Normal file
@@ -0,0 +1,710 @@
|
|||||||
|
-- Databricks notebook source
|
||||||
|
-- CREATE OR REPLACE TABLE DM.DM_TF_EXT_AIA_SALES (
|
||||||
|
-- YYYYMM STRING,
|
||||||
|
-- PACK_CODE STRING,
|
||||||
|
-- CORP_CODE STRING,
|
||||||
|
-- AUDIT_CODE STRING,
|
||||||
|
-- PLATFORM_TYPE STRING,
|
||||||
|
-- STORE_NAME STRING,
|
||||||
|
-- STORE_TYPE STRING,
|
||||||
|
-- REGION_TYPE STRING,
|
||||||
|
-- PACK_FLAG INT,
|
||||||
|
-- PROD_FLAG INT,
|
||||||
|
-- DTP_FLAG INT,
|
||||||
|
-- SALES_UNIT_CAL DECIMAL(38,10),
|
||||||
|
-- SALES_UNIT_CAL_LY DECIMAL(38,10),
|
||||||
|
-- SALES_VALUE_CAL DECIMAL(38,10),
|
||||||
|
-- SALES_VALUE_CAL_LY DECIMAL(38,10),
|
||||||
|
-- CONUTING_UNIT DECIMAL(38,10),
|
||||||
|
-- CONUTING_UNIT_LY DECIMAL(38,10),
|
||||||
|
-- DATA_SOURCE STRING,
|
||||||
|
-- INST_CODE STRING COMMENT '内部机构编码',
|
||||||
|
-- CMPS_FLAG STRING COMMENT '分子式标签',
|
||||||
|
-- DEPT_NAME STRING COMMENT '科室名称',
|
||||||
|
-- PRESCRIPTION DECIMAL(38,10) COMMENT '处方张数',
|
||||||
|
-- PRESCRIPTION_LY DECIMAL(38,10) COMMENT '去年同期处方张数',
|
||||||
|
-- NEW_CODE STRING COMMENT '主数据关联CODE',
|
||||||
|
-- AREA STRING COMMENT '城市',
|
||||||
|
-- H_LEVEL STRING COMMENT '医院类型',
|
||||||
|
-- REIMBURSE STRING COMMENT '报销情况',
|
||||||
|
-- REIMBURSE_TYPE STRING COMMENT '报销类型',
|
||||||
|
-- PRESCRIPTION_SOURCE STRING COMMENT '处方来源',
|
||||||
|
-- ETL_INSERT_DT TIMESTAMP,
|
||||||
|
-- ETL_UPDATE_DT TIMESTAMP
|
||||||
|
-- )
|
||||||
|
-- USING delta
|
||||||
|
-- LOCATION 'abfss://master@azcdatalakeprd.dfs.core.chinacloudapi.cn/DM/dm_tf_ext_aia_sales';
|
||||||
|
-- 上面是生产环境location,下面是测试环境location
|
||||||
|
-- LOCATION 'abfss://master@retaildlstoragetest.dfs.core.chinacloudapi.cn/DM/dm_tf_ext_aia_sales';
|
||||||
|
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
-- MAGIC %run ../../../Common/config
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
-- MAGIC %python
|
||||||
|
-- MAGIC spark.read.table(f'`{CDW_CATALOG}`.`dwd`.`dim_product_wide`').createOrReplaceTempView('cdw_dwd_dim_product_wide')
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
-- 原始数据
|
||||||
|
-- 字段值的初步处理
|
||||||
|
CREATE OR REPLACE TEMPORARY VIEW AIA_RAWDATA
|
||||||
|
AS
|
||||||
|
SELECT
|
||||||
|
YEAR,
|
||||||
|
QTR,
|
||||||
|
CAST(YEAR * 100 + YM AS INT) YM,
|
||||||
|
ORG_PROV,
|
||||||
|
ORG_CITY,
|
||||||
|
INS_LEVEL,
|
||||||
|
CASE
|
||||||
|
WHEN
|
||||||
|
INS_CD IS NOT NULL
|
||||||
|
AND
|
||||||
|
LENGTH (INS_CD) < 7
|
||||||
|
THEN RIGHT (CONCAT ('0000000', INS_CD), 7)
|
||||||
|
ELSE INS_CD
|
||||||
|
END AS ORG_INS_CD,
|
||||||
|
ATC1,
|
||||||
|
ATC2,
|
||||||
|
ATC3,
|
||||||
|
ATC4,
|
||||||
|
ATC,
|
||||||
|
MOLE_NM,
|
||||||
|
PROD_NM,
|
||||||
|
ORG_PRD_NM_C,
|
||||||
|
ORG_PK,
|
||||||
|
ORG_PRD_STR,
|
||||||
|
ORG_PTH,
|
||||||
|
MIN_UNIT,
|
||||||
|
VALUE,
|
||||||
|
UNIT,
|
||||||
|
CAST(PACK_DESC AS INT) AS PACK_DESC,
|
||||||
|
ORG_PK_UN,
|
||||||
|
MANU
|
||||||
|
FROM
|
||||||
|
DWD.DWD_GND_AIA_RAWDATA
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
--关联出AUDIT_COD、INST_CODE
|
||||||
|
CREATE OR REPLACE TEMPORARY VIEW AIA_RAWDATA_WITH_AUDIT_INST
|
||||||
|
AS
|
||||||
|
SELECT
|
||||||
|
UPPER(NVL (T2.INST_CODE, T1.ORG_INS_CD)) AS AUDIT_COD,
|
||||||
|
T1.YEAR,
|
||||||
|
T1.QTR,
|
||||||
|
T1.YM,
|
||||||
|
T1.ORG_PROV,
|
||||||
|
T1.ORG_CITY,
|
||||||
|
T1.INS_LEVEL,
|
||||||
|
T1.ORG_INS_CD,
|
||||||
|
T1.ATC1,
|
||||||
|
T1.ATC2,
|
||||||
|
T1.ATC3,
|
||||||
|
T1.ATC4,
|
||||||
|
T1.ATC,
|
||||||
|
T1.MOLE_NM,
|
||||||
|
T1.PROD_NM,
|
||||||
|
T1.ORG_PRD_NM_C,
|
||||||
|
T1.ORG_PK,
|
||||||
|
T1.ORG_PRD_STR,
|
||||||
|
T1.ORG_PTH,
|
||||||
|
T1.MIN_UNIT,
|
||||||
|
T1.VALUE,
|
||||||
|
T1.UNIT,
|
||||||
|
T1.PACK_DESC,
|
||||||
|
T1.ORG_PK_UN,
|
||||||
|
T1.MANU,
|
||||||
|
NVL (T2.INST_CODE, T1.ORG_INS_CD) AS INST_CODE
|
||||||
|
FROM
|
||||||
|
AIA_RAWDATA AS T1
|
||||||
|
LEFT JOIN
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
DISTINCT
|
||||||
|
CASE
|
||||||
|
WHEN LENGTH (CPA_HOSPITAL_CODE) < 7
|
||||||
|
THEN RIGHT (CONCAT ('0000000', CPA_HOSPITAL_CODE), 7)
|
||||||
|
ELSE CPA_HOSPITAL_CODE
|
||||||
|
END AS CPA_HOSPITAL_CODE,
|
||||||
|
INS_CD_NL AS INST_CODE
|
||||||
|
FROM
|
||||||
|
DWD.DWD_GND_HOSPITAL_NOT_PROVIDED
|
||||||
|
) AS T2
|
||||||
|
ON
|
||||||
|
T1.ORG_INS_CD = T2.CPA_HOSPITAL_CODE
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
/*
|
||||||
|
修改人 CHENWU
|
||||||
|
修改时间 20250513
|
||||||
|
修改内容 打包分子的数据,不论 打通表 和 事实表,都没有系数,需要用 事实表里的 最小制剂单位数量 作为COUNTING_UNIT
|
||||||
|
*/
|
||||||
|
--关联出PACK_COD、CORP_COD、COUNTING_UNIT
|
||||||
|
CREATE OR REPLACE TEMPORARY VIEW AIA_RAWDATA_WIHT_COUNTING_UNIT
|
||||||
|
AS
|
||||||
|
SELECT
|
||||||
|
CASE
|
||||||
|
WHEN
|
||||||
|
LENGTH (TRIM(T2.IQVIA_PACK_CODE)) < 12
|
||||||
|
AND
|
||||||
|
TRIM(T2.IQVIA_PACK_CODE) REGEXP '^[0-9]'
|
||||||
|
THEN RIGHT (CONCAT ('000000000000', TRIM(T2.IQVIA_PACK_CODE)),12)
|
||||||
|
ELSE TRIM(T2.IQVIA_PACK_CODE)
|
||||||
|
END AS PACK_COD,
|
||||||
|
T3.CORP_COD,
|
||||||
|
CASE
|
||||||
|
WHEN LEFT (T2.IQVIA_PACK_CODE, 4) = 'AZP_'
|
||||||
|
THEN CAST(T1.MIN_UNIT / T1.ORG_PK_UN AS DECIMAL(38, 10))
|
||||||
|
ELSE T3.COUNTING_UNIT
|
||||||
|
END COUNTING_UNIT,
|
||||||
|
T1.AUDIT_COD,
|
||||||
|
T1.YEAR,
|
||||||
|
T1.QTR,
|
||||||
|
T1.YM,
|
||||||
|
T1.ORG_PROV,
|
||||||
|
T1.ORG_CITY,
|
||||||
|
T1.INS_LEVEL,
|
||||||
|
T1.ORG_INS_CD,
|
||||||
|
T1.ATC1,
|
||||||
|
T1.ATC2,
|
||||||
|
T1.ATC3,
|
||||||
|
T1.ATC4,
|
||||||
|
T1.ATC,
|
||||||
|
T1.MOLE_NM,
|
||||||
|
T1.PROD_NM,
|
||||||
|
T1.ORG_PRD_NM_C,
|
||||||
|
T1.ORG_PK,
|
||||||
|
T1.ORG_PRD_STR,
|
||||||
|
T1.ORG_PTH,
|
||||||
|
T1.MIN_UNIT,
|
||||||
|
T1.VALUE,
|
||||||
|
T1.UNIT,
|
||||||
|
T1.PACK_DESC,
|
||||||
|
T1.ORG_PK_UN,
|
||||||
|
T1.MANU,
|
||||||
|
T1.INST_CODE
|
||||||
|
FROM
|
||||||
|
AIA_RAWDATA_WITH_AUDIT_INST T1
|
||||||
|
LEFT JOIN
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
DISTINCT
|
||||||
|
TRIM(IQVIA_PACK_CODE) AS IQVIA_PACK_CODE,
|
||||||
|
ATC1,
|
||||||
|
ATC2,
|
||||||
|
ATC3,
|
||||||
|
ATC4,
|
||||||
|
ORG_PRD_CD,
|
||||||
|
ORG_MOLE_NM_C,
|
||||||
|
ORG_MANU_PRD_NM_C,
|
||||||
|
ORG_PRD_NM_C,
|
||||||
|
ORG_PK,
|
||||||
|
ORG_PRD_STR,
|
||||||
|
ORG_PTH,
|
||||||
|
MIN_PK_UNIT,
|
||||||
|
ORG_PK_UNIT,
|
||||||
|
ORG_MANU_NM
|
||||||
|
FROM
|
||||||
|
DWD.DWD_GND_EXT_AIA_CPT_DATA
|
||||||
|
) AS T2
|
||||||
|
--ATC1编码+ATC2编码+ATC3编码+ATC4编码+药品编码(ATC)+药品通用名+药品产品名+药品商品名+规格+剂型+给药途径+最小销售包装单位+包装规格+生产企业
|
||||||
|
ON
|
||||||
|
NVL (T1.ATC1, '') = NVL (T2.ATC1, '')
|
||||||
|
AND NVL (T1.ATC2, '') = NVL (T2.ATC2, '')
|
||||||
|
AND NVL (T1.ATC3, '') = NVL (T2.ATC3, '')
|
||||||
|
AND NVL (T1.ATC4, '') = NVL (T2.ATC4, '')
|
||||||
|
AND NVL (T1.ATC, '') = NVL (T2.ORG_PRD_CD, '')
|
||||||
|
AND NVL (T1.MOLE_NM, '') = NVL (T2.ORG_MOLE_NM_C, '')
|
||||||
|
AND NVL (T1.PROD_NM, '') = NVL (T2.ORG_MANU_PRD_NM_C, '')
|
||||||
|
AND NVL (T1.ORG_PRD_NM_C, '') = NVL (T2.ORG_PRD_NM_C, '')
|
||||||
|
AND NVL (T1.ORG_PK, '') = NVL (T2.ORG_PK, '')
|
||||||
|
AND NVL (T1.ORG_PRD_STR, '') = NVL (T2.ORG_PRD_STR, '')
|
||||||
|
AND NVL (T1.ORG_PTH, '') = NVL (T2.ORG_PTH, '')
|
||||||
|
AND NVL (T1.UNIT, '') = NVL (T2.MIN_PK_UNIT, '')
|
||||||
|
AND NVL (T1.PACK_DESC, '') = NVL (T2.ORG_PK_UNIT, '')
|
||||||
|
AND NVL (T1.MANU, '') = NVL (T2.ORG_MANU_NM, '')
|
||||||
|
LEFT JOIN
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
PACK_COD,
|
||||||
|
CORP_COD,
|
||||||
|
COUNTING_UNIT
|
||||||
|
FROM
|
||||||
|
DM.DM_AIA_PACK_PROPERTY
|
||||||
|
GROUP BY
|
||||||
|
PACK_COD,
|
||||||
|
CORP_COD,
|
||||||
|
COUNTING_UNIT
|
||||||
|
) T3 ON T2.IQVIA_PACK_CODE = T3.PACK_COD
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
CREATE OR REPLACE TEMPORARY VIEW FILTER_YM
|
||||||
|
AS
|
||||||
|
SELECT
|
||||||
|
MAX(YM) AS MAX_YYYYMM,
|
||||||
|
CONCAT(CAST(FLOOR(MAX(YM)/100-2) AS STRING),'01') AS MIN_YYYYMM
|
||||||
|
FROM
|
||||||
|
AIA_RAWDATA_WIHT_COUNTING_UNIT
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
--计算出LY销量
|
||||||
|
CREATE OR REPLACE TEMPORARY VIEW AIA_RAWDATA_FINAL AS
|
||||||
|
SELECT
|
||||||
|
YM,
|
||||||
|
ORG_INS_CD,
|
||||||
|
INST_CODE,
|
||||||
|
AUDIT_COD,
|
||||||
|
PACK_COD,
|
||||||
|
CORP_COD,
|
||||||
|
SUM(VALUE) AS VALUE,
|
||||||
|
SUM(VOLUME) AS VOLUME,
|
||||||
|
SUM(COUNTING_UNIT) AS COUNTING_UNIT,
|
||||||
|
SUM(VALUE_LY) AS VALUE_LY,
|
||||||
|
SUM(VOLUME_LY) AS VOLUME_LY,
|
||||||
|
SUM(COUNTING_UNIT_LY) AS COUNTING_UNIT_LY
|
||||||
|
FROM
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
YM,
|
||||||
|
ORG_INS_CD,
|
||||||
|
INST_CODE,
|
||||||
|
AUDIT_COD,
|
||||||
|
PACK_COD,
|
||||||
|
CORP_COD,
|
||||||
|
CAST(VALUE AS DECIMAL(38, 10)) AS VALUE,
|
||||||
|
CAST(ORG_PK_UN AS DECIMAL(38, 10)) AS VOLUME,
|
||||||
|
CAST(ORG_PK_UN AS DECIMAL(38, 10)) * COALESCE(CAST(COUNTING_UNIT AS DECIMAL(38, 10)), 1) AS COUNTING_UNIT,
|
||||||
|
0 AS VALUE_LY,
|
||||||
|
0 AS VOLUME_LY,
|
||||||
|
0 AS COUNTING_UNIT_LY
|
||||||
|
FROM
|
||||||
|
AIA_RAWDATA_WIHT_COUNTING_UNIT
|
||||||
|
UNION ALL
|
||||||
|
SELECT
|
||||||
|
CAST(CAST(YM AS INT) + 100 AS STRING) AS YM,
|
||||||
|
ORG_INS_CD,
|
||||||
|
INST_CODE,
|
||||||
|
AUDIT_COD,
|
||||||
|
PACK_COD,
|
||||||
|
CORP_COD,
|
||||||
|
0 AS VALUE,
|
||||||
|
0 AS VOLUME,
|
||||||
|
0 AS COUNTING_UNIT,
|
||||||
|
CAST(VALUE AS DECIMAL(38, 10)) AS VALUE_LY,
|
||||||
|
CAST(ORG_PK_UN AS DECIMAL(38, 10)) AS VOLUME_LY,
|
||||||
|
CAST(ORG_PK_UN AS DECIMAL(38, 10)) * COALESCE(CAST(COUNTING_UNIT AS DECIMAL(38, 10)), 1) AS COUNTING_UNIT_LY
|
||||||
|
FROM
|
||||||
|
AIA_RAWDATA_WIHT_COUNTING_UNIT T1
|
||||||
|
JOIN
|
||||||
|
FILTER_YM T2
|
||||||
|
ON
|
||||||
|
1 = 1
|
||||||
|
WHERE
|
||||||
|
CAST(CAST(YM AS INT) + 100 AS STRING) <= T2.MAX_YYYYMM
|
||||||
|
)
|
||||||
|
GROUP BY
|
||||||
|
YM,
|
||||||
|
ORG_INS_CD,
|
||||||
|
INST_CODE,
|
||||||
|
AUDIT_COD,
|
||||||
|
PACK_COD,
|
||||||
|
CORP_COD
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
CREATE OR REPLACE TEMPORARY VIEW FINAL_DM_TF_EXT_AIA_SALES
|
||||||
|
AS
|
||||||
|
SELECT
|
||||||
|
YM,
|
||||||
|
ORG_INS_CD,
|
||||||
|
INST_CODE,
|
||||||
|
AUDIT_COD,
|
||||||
|
PACK_COD,
|
||||||
|
CORP_COD,
|
||||||
|
SUM(VALUE) AS VALUE,
|
||||||
|
SUM(VOLUME) AS VOLUME,
|
||||||
|
SUM(COUNTING_UNIT) AS COUNTING_UNIT,
|
||||||
|
SUM(VALUE_LY) AS VALUE_LY,
|
||||||
|
SUM(VOLUME_LY) AS VOLUME_LY,
|
||||||
|
SUM(COUNTING_UNIT_LY) AS COUNTING_UNIT_LY
|
||||||
|
FROM
|
||||||
|
AIA_RAWDATA_FINAL
|
||||||
|
GROUP BY
|
||||||
|
YM,
|
||||||
|
ORG_INS_CD,
|
||||||
|
INST_CODE,
|
||||||
|
AUDIT_COD,
|
||||||
|
PACK_COD,
|
||||||
|
CORP_COD
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
INSERT OVERWRITE TABLE DM.DM_TF_EXT_AIA_SALES (
|
||||||
|
YYYYMM,
|
||||||
|
PACK_CODE,
|
||||||
|
AUDIT_CODE,
|
||||||
|
DATA_SOURCE,
|
||||||
|
SALES_UNIT_CAL,
|
||||||
|
SALES_UNIT_CAL_LY,
|
||||||
|
SALES_VALUE_CAL,
|
||||||
|
SALES_VALUE_CAL_LY,
|
||||||
|
CONUTING_UNIT,
|
||||||
|
CONUTING_UNIT_LY,
|
||||||
|
PRESCRIPTION,
|
||||||
|
PRESCRIPTION_LY,
|
||||||
|
PACK_FLAG,
|
||||||
|
PROD_FLAG,
|
||||||
|
DTP_FLAG,
|
||||||
|
CMPS_FLAG,
|
||||||
|
PLATFORM_TYPE,
|
||||||
|
STORE_NAME,
|
||||||
|
STORE_TYPE,
|
||||||
|
REGION_TYPE,
|
||||||
|
INST_CODE,
|
||||||
|
DEPT_NAME,
|
||||||
|
NEW_CODE,
|
||||||
|
AREA,
|
||||||
|
H_LEVEL,
|
||||||
|
REIMBURSE,
|
||||||
|
REIMBURSE_TYPE,
|
||||||
|
PRESCRIPTION_SOURCE,
|
||||||
|
CORP_CODE,
|
||||||
|
ETL_INSERT_DT,
|
||||||
|
ETL_UPDATE_DT
|
||||||
|
)
|
||||||
|
SELECT
|
||||||
|
A.ym,
|
||||||
|
A.PACK_COD as PACK_CODE,
|
||||||
|
A.AUDIT_COD AS AUDIT_CODE,
|
||||||
|
'AIA(Monthly)' DATA_SOURCE,
|
||||||
|
-------------------------------------
|
||||||
|
sum(volume) as SALES_UNIT_CAL,
|
||||||
|
sum(volume_ly) as SALES_UNIT_CAL_LY,
|
||||||
|
sum(value) as SALES_VALUE_CAL,
|
||||||
|
sum(value_ly) as SALES_VALUE_CAL_LY,
|
||||||
|
sum(counting_unit) as CONUTING_UNIT,
|
||||||
|
sum(counting_unit_ly) as CONUTING_UNIT_LY,
|
||||||
|
null as prescription,
|
||||||
|
null as prescription_ly,
|
||||||
|
-------------------------------------
|
||||||
|
--Retail藏数逻辑标签------------------
|
||||||
|
1 PACK_FLAG,
|
||||||
|
1 PROD_FLAG,
|
||||||
|
0 DTP_FLAG,
|
||||||
|
null as cmps_flag,
|
||||||
|
-------------------------------------
|
||||||
|
--EC数据标签--------------------------
|
||||||
|
'' PLATFORM_TYPE,
|
||||||
|
'' STORE_NAME,
|
||||||
|
'' STORE_TYPE,
|
||||||
|
-------------------------------------
|
||||||
|
--COUNTY数据标签----------------------
|
||||||
|
'' REGION_TYPE,
|
||||||
|
-------------------------------------
|
||||||
|
--AIA数据标签-------------------------
|
||||||
|
inst_code AS inst_code, --可以移除,报告没有使用
|
||||||
|
-------------------------------------
|
||||||
|
--XIE HE 数据标签---------------------
|
||||||
|
'' as dept_name,
|
||||||
|
'' as new_code,
|
||||||
|
'' as area,
|
||||||
|
'' as h_level,
|
||||||
|
'' as reimburse,
|
||||||
|
'' as reimburse_type,
|
||||||
|
'' as prescription_source,
|
||||||
|
-------------------------------------
|
||||||
|
'' as CORP_CODE, --可以移除,报告没有使用
|
||||||
|
-------------------------------------
|
||||||
|
FROM_UTC_TIMESTAMP(CURRENT_TIMESTAMP(), 'UTC+8') AS ETL_INSERT_DT,
|
||||||
|
FROM_UTC_TIMESTAMP(CURRENT_TIMESTAMP(), 'UTC+8') AS ETL_UPDATE_DT
|
||||||
|
FROM
|
||||||
|
FINAL_DM_TF_EXT_AIA_SALES A
|
||||||
|
WHERE
|
||||||
|
ym >= '202401'
|
||||||
|
GROUP BY
|
||||||
|
YM,
|
||||||
|
PACK_COD,
|
||||||
|
CORP_COD,
|
||||||
|
AUDIT_COD,
|
||||||
|
INST_CODE
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
-- 20260109 dm.dm_ext_aia_sales 是老的表,现在已经用DM.DM_TF_EXT_AIA_SALES 替换,但是为了下游,老表暂时不下线,仍然写入
|
||||||
|
insert overwrite table dm.dm_ext_aia_sales (
|
||||||
|
ym,
|
||||||
|
org_ins_cd,
|
||||||
|
inst_code,
|
||||||
|
AUDIT_COD,
|
||||||
|
pack_cod,
|
||||||
|
corp_cod,
|
||||||
|
value,
|
||||||
|
volume,
|
||||||
|
counting_unit,
|
||||||
|
value_ly,
|
||||||
|
volume_ly,
|
||||||
|
counting_unit_ly
|
||||||
|
)
|
||||||
|
select
|
||||||
|
ym,
|
||||||
|
org_ins_cd,
|
||||||
|
inst_code,
|
||||||
|
AUDIT_COD,
|
||||||
|
pack_cod,
|
||||||
|
corp_cod,
|
||||||
|
sum(value) as value,
|
||||||
|
sum(volume) as volume,
|
||||||
|
sum(counting_unit) as counting_unit,
|
||||||
|
sum(value_ly) as value_ly,
|
||||||
|
sum(volume_ly) as volume_ly,
|
||||||
|
sum(counting_unit_ly) as counting_unit_ly
|
||||||
|
from
|
||||||
|
aia_rawdata_FINAL
|
||||||
|
CROSS JOIN
|
||||||
|
filter_ym
|
||||||
|
WHERE
|
||||||
|
YM BETWEEN filter_ym.min_yyyymm AND filter_ym.max_yyyymm
|
||||||
|
group by
|
||||||
|
ym,
|
||||||
|
org_ins_cd,
|
||||||
|
inst_code,
|
||||||
|
AUDIT_COD,
|
||||||
|
pack_cod,
|
||||||
|
corp_cod
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
-- MAGIC %md
|
||||||
|
-- MAGIC 下面是重构验证,chenwu可删
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
-- %sql
|
||||||
|
-- inst_code
|
||||||
|
-- ym
|
||||||
|
-- MARKET
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
-- SELECT ym,AUDIT_COD,inst_code,SUM(value) value,SUM(volume) volume,SUM(counting_unit) counting_unit,SUM(value_ly) value_ly,SUM(volume_ly) volume_ly,SUM(counting_unit_ly) counting_unit_ly
|
||||||
|
-- FROM dm.dm_ext_aia_sales
|
||||||
|
-- GROUP BY ym,AUDIT_COD,inst_code
|
||||||
|
-- ORDER BY ym,AUDIT_COD,inst_code
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
-- SELECT ym,AUDIT_COD,SUM(value) value,SUM(volume) volume,SUM(counting_unit) counting_unit,SUM(value_ly) value_ly,SUM(volume_ly) volume_ly,SUM(counting_unit_ly) counting_unit_ly
|
||||||
|
-- FROM dm.dm_ext_aia_sales
|
||||||
|
-- GROUP BY ym,AUDIT_COD
|
||||||
|
-- ORDER BY ym,AUDIT_COD
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
|
||||||
|
-- SELECT
|
||||||
|
-- old.ym,
|
||||||
|
-- OLD.AUDIT_COD,
|
||||||
|
-- OLD.inst_code,
|
||||||
|
-- OLD.value,
|
||||||
|
-- NEW.value,
|
||||||
|
-- OLD.value-NEW.value as dif_value,
|
||||||
|
-- OLD.value_ly,
|
||||||
|
-- NEW.value_ly,
|
||||||
|
-- OLD.value_ly-NEW.value_ly as dif_value_ly,
|
||||||
|
-- OLD.volume,
|
||||||
|
-- NEW.volume,
|
||||||
|
-- OLD.volume-NEW.volume as dif_volume,
|
||||||
|
-- OLD.volume_ly,
|
||||||
|
-- NEW.volume_ly,
|
||||||
|
-- OLD.volume_ly-NEW.volume_ly as dif_volume_ly,
|
||||||
|
-- OLD.counting_unit,
|
||||||
|
-- NEW.counting_unit,
|
||||||
|
-- OLD.counting_unit-NEW.counting_unit as dif_counting_unit,
|
||||||
|
-- OLD.counting_unit_ly,
|
||||||
|
-- NEW.counting_unit_ly,
|
||||||
|
-- OLD.counting_unit_ly-NEW.counting_unit_ly as dif_counting_unit_ly
|
||||||
|
-- FROM
|
||||||
|
-- (
|
||||||
|
-- SELECT ym,AUDIT_COD,inst_code,SUM(value) value,SUM(volume) volume,SUM(counting_unit) counting_unit,SUM(value_ly) value_ly,SUM(volume_ly) volume_ly,SUM(counting_unit_ly) counting_unit_ly
|
||||||
|
-- FROM dm.dm_ext_aia_sales
|
||||||
|
-- GROUP BY ym,AUDIT_COD,inst_code
|
||||||
|
-- -- ORDER BY ym
|
||||||
|
-- ) AS OLD
|
||||||
|
-- LEFT JOIN
|
||||||
|
-- (
|
||||||
|
-- SELECT YYYYMM,AUDIT_CODE,inst_code,SUM(SALES_VALUE_CAL) value,SUM(SALES_UNIT_CAL) volume,SUM(CONUTING_UNIT) counting_unit,SUM(SALES_VALUE_CAL_LY) value_ly,SUM(SALES_UNIT_CAL_LY) volume_ly,SUM(CONUTING_UNIT_LY) counting_unit_ly
|
||||||
|
-- FROM DM.DM_TF_EXT_AIA_SALES
|
||||||
|
-- GROUP BY YYYYMM,AUDIT_CODE,inst_code
|
||||||
|
-- -- ORDER BY YYYYMM
|
||||||
|
-- ) AS NEW
|
||||||
|
-- ON OLD.YM =NEW.YYYYMM AND old.AUDIT_COD = NEW.AUDIT_CODE AND old.inst_code = new.inst_code
|
||||||
|
-- where
|
||||||
|
-- ABS(OLD.value - NEW.value) > 0.0000000001 OR
|
||||||
|
-- ABS(OLD.volume - NEW.volume) > 0.0000000001 OR
|
||||||
|
-- ABS(OLD.value_ly - NEW.value_ly) > 0.0000000001 OR
|
||||||
|
-- ABS(OLD.volume_ly - NEW.volume_ly) > 0.0000000001 OR
|
||||||
|
-- ABS(OLD.counting_unit - NEW.counting_unit) > 0.0000000001 OR
|
||||||
|
-- ABS(OLD.counting_unit_ly - NEW.counting_unit_ly) > 0.0000000001;
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
|
||||||
|
-- SELECT
|
||||||
|
-- old.ym,
|
||||||
|
-- OLD.AUDIT_COD,
|
||||||
|
-- OLD.value,
|
||||||
|
-- NEW.value,
|
||||||
|
-- OLD.value-NEW.value as dif_value,
|
||||||
|
-- OLD.value_ly,
|
||||||
|
-- NEW.value_ly,
|
||||||
|
-- OLD.value_ly-NEW.value_ly as dif_value_ly,
|
||||||
|
-- OLD.volume,
|
||||||
|
-- NEW.volume,
|
||||||
|
-- OLD.volume-NEW.volume as dif_volume,
|
||||||
|
-- OLD.volume_ly,
|
||||||
|
-- NEW.volume_ly,
|
||||||
|
-- OLD.volume_ly-NEW.volume_ly as dif_volume_ly,
|
||||||
|
-- OLD.counting_unit,
|
||||||
|
-- NEW.counting_unit,
|
||||||
|
-- OLD.counting_unit-NEW.counting_unit as dif_counting_unit,
|
||||||
|
-- OLD.counting_unit_ly,
|
||||||
|
-- NEW.counting_unit_ly,
|
||||||
|
-- OLD.counting_unit_ly-NEW.counting_unit_ly as dif_counting_unit_ly
|
||||||
|
-- FROM
|
||||||
|
-- (
|
||||||
|
-- SELECT ym,AUDIT_COD,SUM(value) value,SUM(volume) volume,SUM(counting_unit) counting_unit,SUM(value_ly) value_ly,SUM(volume_ly) volume_ly,SUM(counting_unit_ly) counting_unit_ly
|
||||||
|
-- FROM dm.dm_ext_aia_sales
|
||||||
|
-- GROUP BY ym,AUDIT_COD
|
||||||
|
-- -- ORDER BY ym
|
||||||
|
-- ) AS OLD
|
||||||
|
-- LEFT JOIN
|
||||||
|
-- (
|
||||||
|
-- SELECT YYYYMM,AUDIT_CODE,SUM(SALES_VALUE_CAL) value,SUM(SALES_UNIT_CAL) volume,SUM(CONUTING_UNIT) counting_unit,SUM(SALES_VALUE_CAL_LY) value_ly,SUM(SALES_UNIT_CAL_LY) volume_ly,SUM(CONUTING_UNIT_LY) counting_unit_ly
|
||||||
|
-- FROM DM.DM_TF_EXT_AIA_SALES
|
||||||
|
-- GROUP BY YYYYMM,AUDIT_CODE
|
||||||
|
-- -- ORDER BY YYYYMM
|
||||||
|
-- ) AS NEW
|
||||||
|
-- ON OLD.YM =NEW.YYYYMM AND old.AUDIT_COD = NEW.AUDIT_CODE
|
||||||
|
-- where
|
||||||
|
-- ABS(OLD.value - NEW.value) > 0.0000000001 OR
|
||||||
|
-- ABS(OLD.volume - NEW.volume) > 0.0000000001 OR
|
||||||
|
-- ABS(OLD.value_ly - NEW.value_ly) > 0.0000000001 OR
|
||||||
|
-- ABS(OLD.volume_ly - NEW.volume_ly) > 0.0000000001 OR
|
||||||
|
-- ABS(OLD.counting_unit - NEW.counting_unit) > 0.0000000001 OR
|
||||||
|
-- ABS(OLD.counting_unit_ly - NEW.counting_unit_ly) > 0.0000000001;
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
|
||||||
|
-- SELECT
|
||||||
|
-- old.ym,
|
||||||
|
-- OLD.value,
|
||||||
|
-- NEW.value,
|
||||||
|
-- OLD.value-NEW.value as dif_value,
|
||||||
|
-- OLD.value_ly,
|
||||||
|
-- NEW.value_ly,
|
||||||
|
-- OLD.value_ly-NEW.value_ly as dif_value_ly,
|
||||||
|
-- OLD.volume,
|
||||||
|
-- NEW.volume,
|
||||||
|
-- OLD.volume-NEW.volume as dif_volume,
|
||||||
|
-- OLD.volume_ly,
|
||||||
|
-- NEW.volume_ly,
|
||||||
|
-- OLD.volume_ly-NEW.volume_ly as dif_volume_ly,
|
||||||
|
-- OLD.counting_unit,
|
||||||
|
-- NEW.counting_unit,
|
||||||
|
-- OLD.counting_unit-NEW.counting_unit as dif_counting_unit,
|
||||||
|
-- OLD.counting_unit_ly,
|
||||||
|
-- NEW.counting_unit_ly,
|
||||||
|
-- OLD.counting_unit_ly-NEW.counting_unit_ly as dif_counting_unit_ly
|
||||||
|
-- FROM
|
||||||
|
-- (
|
||||||
|
-- SELECT ym,SUM(value) value,SUM(volume) volume,SUM(counting_unit) counting_unit,SUM(value_ly) value_ly,SUM(volume_ly) volume_ly,SUM(counting_unit_ly) counting_unit_ly
|
||||||
|
-- FROM dm.dm_ext_aia_sales
|
||||||
|
-- GROUP BY ym
|
||||||
|
-- ORDER BY ym
|
||||||
|
-- ) AS OLD
|
||||||
|
-- LEFT JOIN
|
||||||
|
-- (
|
||||||
|
-- SELECT YYYYMM,SUM(SALES_VALUE_CAL) value,SUM(SALES_UNIT_CAL) volume,SUM(CONUTING_UNIT) counting_unit,SUM(SALES_VALUE_CAL_LY) value_ly,SUM(SALES_UNIT_CAL_LY) volume_ly,SUM(CONUTING_UNIT_LY) counting_unit_ly
|
||||||
|
-- FROM DM.DM_TF_EXT_AIA_SALES
|
||||||
|
-- GROUP BY YYYYMM
|
||||||
|
-- ORDER BY YYYYMM
|
||||||
|
-- ) AS NEW
|
||||||
|
-- ON OLD.YM =NEW.YYYYMM
|
||||||
|
-- where
|
||||||
|
-- ABS(OLD.value - NEW.value) > 0.0000000001 OR
|
||||||
|
-- ABS(OLD.volume - NEW.volume) > 0.0000000001 OR
|
||||||
|
-- ABS(OLD.value_ly - NEW.value_ly) > 0.0000000001 OR
|
||||||
|
-- ABS(OLD.volume_ly - NEW.volume_ly) > 0.0000000001 OR
|
||||||
|
-- ABS(OLD.counting_unit - NEW.counting_unit) > 0.0000000001 OR
|
||||||
|
-- ABS(OLD.counting_unit_ly - NEW.counting_unit_ly) > 0.0000000001;
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
-- -- 这个sql证明,两张表的inst_code,值也相同
|
||||||
|
-- SELECT
|
||||||
|
-- *
|
||||||
|
-- FROM
|
||||||
|
-- (
|
||||||
|
-- SELECT inst_code,COUNT(*) as c1 FROM DM.DM_TF_EXT_AIA_SALES
|
||||||
|
-- GROUP BY inst_code
|
||||||
|
-- ) AS NEW
|
||||||
|
-- FULL JOIN
|
||||||
|
-- (
|
||||||
|
-- SELECT inst_code,COUNT(*) as c1 FROM DM.dm_ext_aia_sales
|
||||||
|
-- GROUP BY inst_code
|
||||||
|
-- ) AS OLD
|
||||||
|
-- ON NEW.inst_code = old.inst_code
|
||||||
|
-- WHERE new.c1 != old.c1
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
-- -- 这个sql证明,两张表的inst_code相同
|
||||||
|
-- SELECT
|
||||||
|
-- *
|
||||||
|
-- FROM
|
||||||
|
-- (
|
||||||
|
-- SELECT inst_code,COUNT(*) as c1 FROM DM.DM_TF_EXT_AIA_SALES
|
||||||
|
-- GROUP BY inst_code
|
||||||
|
-- ) AS NEW
|
||||||
|
-- FULL JOIN
|
||||||
|
-- (
|
||||||
|
-- SELECT inst_code,COUNT(*) as c1 FROM DM.DM_TF_EXT_AIA_SALES
|
||||||
|
-- GROUP BY inst_code
|
||||||
|
-- ) AS OLD
|
||||||
|
-- ON NEW.inst_code = old.inst_code
|
||||||
|
-- WHERE NEW.inst_code IS NULL OR old.inst_code IS NULL
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
-- -- 这个sql证明,两张表的inst_code,值也相同
|
||||||
|
-- SELECT
|
||||||
|
-- *
|
||||||
|
-- FROM
|
||||||
|
-- (
|
||||||
|
-- SELECT inst_code,COUNT(*) as c1 FROM DM.DM_TF_EXT_AIA_SALES
|
||||||
|
-- GROUP BY inst_code
|
||||||
|
-- ) AS NEW
|
||||||
|
-- FULL JOIN
|
||||||
|
-- (
|
||||||
|
-- SELECT inst_code,COUNT(*) as c1 FROM DM.DM_TF_EXT_AIA_SALES
|
||||||
|
-- GROUP BY inst_code
|
||||||
|
-- ) AS OLD
|
||||||
|
-- ON NEW.inst_code = old.inst_code
|
||||||
|
-- WHERE new.c1 != old.c1
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
-- SELECT COUNT(*) FROM DM.DM_TF_EXT_AIA_SALES
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
-- SELECT COUNT(*) FROM DM.dm_ext_aia_sales
|
||||||
BIN
EXTERNAL/AIA/02 dm_ext_aia_sales.sql:Zone.Identifier
Normal file
BIN
EXTERNAL/AIA/02 dm_ext_aia_sales.sql:Zone.Identifier
Normal file
Binary file not shown.
195
EXTERNAL/AIA/02 dm_ext_aia_sales_bakup_20230327.sql
Normal file
195
EXTERNAL/AIA/02 dm_ext_aia_sales_bakup_20230327.sql
Normal file
@@ -0,0 +1,195 @@
|
|||||||
|
-- Databricks notebook source
|
||||||
|
--字段值的初步处理
|
||||||
|
create or replace temporary view aia_rawdata_1 as
|
||||||
|
select
|
||||||
|
year,
|
||||||
|
qtr,
|
||||||
|
cast(year * 100 + ym as int) ym,
|
||||||
|
org_prov,
|
||||||
|
org_city,
|
||||||
|
ins_level,
|
||||||
|
case when ins_cd is not null and length(ins_cd) < 7 then right(concat('0000000',ins_cd),7) else ins_cd end as org_ins_cd,
|
||||||
|
atc1,
|
||||||
|
atc2,
|
||||||
|
atc3,
|
||||||
|
atc4,
|
||||||
|
atc,
|
||||||
|
mole_nm,
|
||||||
|
prod_nm,
|
||||||
|
org_prd_nm_c,
|
||||||
|
org_pk,
|
||||||
|
org_prd_str,
|
||||||
|
org_pth,
|
||||||
|
min_unit,
|
||||||
|
value,
|
||||||
|
unit,
|
||||||
|
cast(pack_desc as int) as pack_desc,
|
||||||
|
org_pk_un,
|
||||||
|
manu
|
||||||
|
from dwd.dwd_gnd_aia_rawdata
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
|
||||||
|
--关联出AUDIT_COD、inst_code
|
||||||
|
create or replace temporary view aia_rawdata_2 as
|
||||||
|
with hospital_mapping as (
|
||||||
|
select distinct
|
||||||
|
case when length(cpa_hospital_code) < 7 then right(concat('0000000',cpa_hospital_code),7) else cpa_hospital_code end as cpa_hospital_code,
|
||||||
|
ins_cd_nl as inst_code
|
||||||
|
from dwd.dwd_gnd_hospital_not_provided
|
||||||
|
)
|
||||||
|
|
||||||
|
select upper(nvl(t2.inst_code,t1.org_ins_cd)) AUDIT_COD,
|
||||||
|
t1.*,
|
||||||
|
nvl(t2.inst_code,t1.org_ins_cd) as inst_code
|
||||||
|
from aia_rawdata_1 t1
|
||||||
|
left join hospital_mapping t2
|
||||||
|
on t1.org_ins_cd = t2.cpa_hospital_code
|
||||||
|
-- left join dm.dm_td_institution t3
|
||||||
|
-- on t2.ins_code = t3.inst_code
|
||||||
|
-- left join dm.dm_ims_td_geo t4
|
||||||
|
-- on t3.city_name_en = t4.city
|
||||||
|
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
/*
|
||||||
|
修改人 chenwu
|
||||||
|
修改时间 20250513
|
||||||
|
修改内容 打包分子的数据,不论 打通表 和 事实表,都没有系数,需要用 事实表里的 最小制剂单位数量 作为counting_unit
|
||||||
|
*/
|
||||||
|
--关联出pack_cod、corp_cod、counting_unit
|
||||||
|
create or replace temporary view aia_rawdata_3 as
|
||||||
|
select case when length(trim(t2.iqvia_pack_code)) < 12 and trim(t2.iqvia_pack_code) REGEXP '^[0-9]' then right(concat('000000000000',trim(t2.iqvia_pack_code)),12)
|
||||||
|
else trim(t2.iqvia_pack_code)
|
||||||
|
end as pack_cod,
|
||||||
|
t3.corp_cod,
|
||||||
|
case when left(t2.iqvia_pack_code,4) = 'AZP_' then cast(t1.min_unit/t1.org_pk_un as decimal(38,10)) else t3.counting_unit end counting_unit,
|
||||||
|
-- t3.counting_unit,20250513
|
||||||
|
t1.*
|
||||||
|
from aia_rawdata_2 t1
|
||||||
|
left join (select distinct trim(iqvia_pack_code) as iqvia_pack_code,
|
||||||
|
ATC1,ATC2,ATC3,ATC4,org_prd_cd,org_mole_nm_c,
|
||||||
|
org_manu_prd_nm_c,org_prd_nm_c,org_pk,org_prd_str,
|
||||||
|
org_pth,min_pk_unit,org_pk_unit,org_manu_nm from dwd.dwd_gnd_ext_aia_cpt_data) t2
|
||||||
|
--ATC1编码+ATC2编码+ATC3编码+ATC4编码+药品编码(ATC)+药品通用名+药品产品名+药品商品名+规格+剂型+给药途径+最小销售包装单位+包装规格+生产企业
|
||||||
|
on nvl(t1.ATC1,'') = nvl(t2.ATC1,'')
|
||||||
|
and nvl(t1.ATC2,'') = nvl(t2.ATC2,'')
|
||||||
|
and nvl(t1.ATC3,'') = nvl(t2.ATC3,'')
|
||||||
|
and nvl(t1.ATC4,'') = nvl(t2.ATC4,'')
|
||||||
|
and nvl(t1.ATC,'') = nvl(t2.org_prd_cd,'')
|
||||||
|
and nvl(t1.MOLE_NM,'') = nvl(t2.org_mole_nm_c,'')
|
||||||
|
and nvl(t1.PROD_NM,'') = nvl(t2.org_manu_prd_nm_c,'')
|
||||||
|
and nvl(t1.ORG_PRD_NM_C,'') = nvl(t2.org_prd_nm_c,'')
|
||||||
|
and nvl(t1.ORG_PK,'') = nvl(t2.org_pk,'')
|
||||||
|
and nvl(t1.ORG_PRD_STR,'') = nvl(t2.org_prd_str,'')
|
||||||
|
and nvl(t1.ORG_PTH,'') = nvl(t2.org_pth,'')
|
||||||
|
and nvl(t1.UNIT,'') = nvl(t2.min_pk_unit,'')
|
||||||
|
and nvl(t1.PACK_DESC,'') = nvl(t2.org_pk_unit,'')
|
||||||
|
and nvl(t1.MANU,'') = nvl(t2.org_manu_nm,'')
|
||||||
|
left join (select distinct PACK_COD,corp_cod,counting_unit from dm.dm_aia_pack_property) t3
|
||||||
|
on t2.iqvia_pack_code = t3.PACK_COD
|
||||||
|
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
|
||||||
|
--计算出ly销量
|
||||||
|
create or replace temporary view aia_rawdata_4 as
|
||||||
|
with max_ym as (
|
||||||
|
select max(ym) as max_ym from aia_rawdata_3
|
||||||
|
)
|
||||||
|
|
||||||
|
select
|
||||||
|
YM,
|
||||||
|
org_ins_cd,
|
||||||
|
inst_code,
|
||||||
|
AUDIT_COD,
|
||||||
|
pack_cod,
|
||||||
|
corp_cod,
|
||||||
|
sum(value) as value,
|
||||||
|
sum(volume) as volume,
|
||||||
|
sum(counting_unit) as counting_unit,
|
||||||
|
sum(value_ly) as value_ly,
|
||||||
|
sum(volume_ly) as volume_ly,
|
||||||
|
sum(counting_unit_ly) as counting_unit_ly
|
||||||
|
from (
|
||||||
|
select
|
||||||
|
ym,
|
||||||
|
org_ins_cd,
|
||||||
|
inst_code,
|
||||||
|
AUDIT_COD,
|
||||||
|
pack_cod,
|
||||||
|
corp_cod,
|
||||||
|
cast(value as decimal(38,10)) as value,
|
||||||
|
cast(ORG_PK_UN as decimal(38,10)) as volume,
|
||||||
|
cast(ORG_PK_UN as decimal(38,10)) * coalesce(cast(counting_unit as decimal(38,10)),1) as counting_unit,
|
||||||
|
0 as value_ly,
|
||||||
|
0 as volume_ly,
|
||||||
|
0 as counting_unit_ly
|
||||||
|
from aia_rawdata_3
|
||||||
|
union all
|
||||||
|
select
|
||||||
|
cast(cast(ym as int) + 100 as string) as ym,
|
||||||
|
org_ins_cd,
|
||||||
|
inst_code,
|
||||||
|
AUDIT_COD,
|
||||||
|
pack_cod,
|
||||||
|
corp_cod,
|
||||||
|
0 as value,
|
||||||
|
0 as volume,
|
||||||
|
0 as counting_unit,
|
||||||
|
cast(value as decimal(38,10)) as value_ly,
|
||||||
|
cast(ORG_PK_UN as decimal(38,10)) as volume_ly,
|
||||||
|
cast(ORG_PK_UN as decimal(38,10)) * coalesce(cast(counting_unit as decimal(38,10)),1) as counting_unit_ly
|
||||||
|
from aia_rawdata_3 t1
|
||||||
|
join max_ym t2
|
||||||
|
on 1=1
|
||||||
|
where cast(cast(ym as int) + 100 as string) <= t2.max_ym
|
||||||
|
)
|
||||||
|
group by ym,
|
||||||
|
org_ins_cd,
|
||||||
|
inst_code,
|
||||||
|
AUDIT_COD,
|
||||||
|
pack_cod,
|
||||||
|
corp_cod
|
||||||
|
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
insert overwrite table dm.dm_ext_aia_sales(
|
||||||
|
ym,
|
||||||
|
org_ins_cd,
|
||||||
|
inst_code,
|
||||||
|
AUDIT_COD,
|
||||||
|
pack_cod,
|
||||||
|
corp_cod,
|
||||||
|
value,
|
||||||
|
volume,
|
||||||
|
counting_unit,
|
||||||
|
value_ly,
|
||||||
|
volume_ly,
|
||||||
|
counting_unit_ly
|
||||||
|
)
|
||||||
|
select
|
||||||
|
ym,
|
||||||
|
org_ins_cd,
|
||||||
|
inst_code,
|
||||||
|
AUDIT_COD,
|
||||||
|
pack_cod,
|
||||||
|
corp_cod,
|
||||||
|
sum(value) as value,
|
||||||
|
sum(volume) as volume,
|
||||||
|
sum(counting_unit) as counting_unit,
|
||||||
|
sum(value_ly) as value_ly,
|
||||||
|
sum(volume_ly) as volume_ly,
|
||||||
|
sum(counting_unit_ly) as counting_unit_ly
|
||||||
|
from aia_rawdata_4
|
||||||
|
group by
|
||||||
|
ym,
|
||||||
|
org_ins_cd,
|
||||||
|
inst_code,
|
||||||
|
AUDIT_COD,
|
||||||
|
pack_cod,
|
||||||
|
corp_cod
|
||||||
Binary file not shown.
84
EXTERNAL/AIA/03 dm_aia_flag.sql
Normal file
84
EXTERNAL/AIA/03 dm_aia_flag.sql
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
-- Databricks notebook source
|
||||||
|
-----------------------------------------------------------------------------------
|
||||||
|
--修改时间:20241021
|
||||||
|
--修改人:FanXujia
|
||||||
|
--修改内容:
|
||||||
|
--只取最新年-1 ~ 最新年范围内的,称为“2年共有”。增加
|
||||||
|
-----------------------------------------------------------------------------------
|
||||||
|
--共有机构
|
||||||
|
insert overwrite table dm.dm_aia_hp_flag
|
||||||
|
with max_year as (
|
||||||
|
select
|
||||||
|
max(cast(year as int)) as max_year,
|
||||||
|
max(cast(year as int) - 1) as max_year_ly
|
||||||
|
from dwd.dwd_gnd_hospital_not_provided
|
||||||
|
)
|
||||||
|
,hp_flag as (
|
||||||
|
select
|
||||||
|
distinct concat(t1.year, '-Y') as hp_his_flag,
|
||||||
|
case when length(t1.cpa_hospital_code) < 7 then right(concat('0000000',t1.cpa_hospital_code),7) else t1.cpa_hospital_code end as cpa_hospital_code
|
||||||
|
from
|
||||||
|
dwd.dwd_gnd_hospital_not_provided t1
|
||||||
|
cross join max_year t2
|
||||||
|
where cast(t1.year as int) >= t2.max_year_ly
|
||||||
|
)
|
||||||
|
,hp_flag2 as (
|
||||||
|
SELECT
|
||||||
|
cpa_hospital_code,
|
||||||
|
CONCAT_WS(' ∩ ', SORT_ARRAY(COLLECT_LIST(hp_his_flag))) AS FLAG
|
||||||
|
FROM
|
||||||
|
hp_flag
|
||||||
|
GROUP BY
|
||||||
|
cpa_hospital_code
|
||||||
|
)
|
||||||
|
,inst_mapping as (
|
||||||
|
-- select
|
||||||
|
-- distinct right(concat('0000000', org_cd), 7) org_cd,
|
||||||
|
-- ins_cd
|
||||||
|
-- from
|
||||||
|
-- dwd.dwd_gnd_hospitalmapping
|
||||||
|
select distinct
|
||||||
|
case when length(cpa_hospital_code) < 7 then right(concat('0000000',cpa_hospital_code),7) else cpa_hospital_code end as cpa_hospital_code,
|
||||||
|
ins_cd_nl as inst_code
|
||||||
|
from dwd.dwd_gnd_hospital_not_provided
|
||||||
|
)
|
||||||
|
|
||||||
|
,ALL_INS as (
|
||||||
|
select distinct inst_code
|
||||||
|
from dm.dm_ext_aia_sales
|
||||||
|
)
|
||||||
|
|
||||||
|
,flag as (
|
||||||
|
select distinct
|
||||||
|
coalesce(t2.inst_code,t1.cpa_hospital_code) as inst_code,
|
||||||
|
t1.flag as aia_hp_flag,
|
||||||
|
'Y' as FLAG,
|
||||||
|
'AIA(Monthly)' as DATA_SOURCE
|
||||||
|
from hp_flag2 t1
|
||||||
|
left join inst_mapping t2 on t1.cpa_hospital_code = t2.cpa_hospital_code
|
||||||
|
where
|
||||||
|
length(t1.flag) > 6
|
||||||
|
union all
|
||||||
|
select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'IQVIA-CHPA(Monthly)' as DATA_SOURCE
|
||||||
|
union all
|
||||||
|
select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'XH Data(Quarterly)' as DATA_SOURCE
|
||||||
|
union all
|
||||||
|
select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'EC(Monthly)' as DATA_SOURCE
|
||||||
|
union all
|
||||||
|
select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'Retail(Quarterly)' as DATA_SOURCE
|
||||||
|
union all
|
||||||
|
select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'CHC(Quarterly)' as DATA_SOURCE
|
||||||
|
union all
|
||||||
|
select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'THC(Quarterly)' as DATA_SOURCE
|
||||||
|
union all
|
||||||
|
select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'IQVIA-COUNTY(Quarterly)' as DATA_SOURCE
|
||||||
|
)
|
||||||
|
|
||||||
|
select t1.inst_code,
|
||||||
|
NVL(t2.aia_hp_flag,'') as aia_hp_flag,
|
||||||
|
NVL(t2.FLAG,'N') as FLAG,
|
||||||
|
'AIA(Monthly)' as DATA_SOURCE
|
||||||
|
from ALL_INS t1
|
||||||
|
left join flag t2
|
||||||
|
on t1.inst_code = t2.inst_code
|
||||||
|
|
||||||
BIN
EXTERNAL/AIA/03 dm_aia_flag.sql:Zone.Identifier
Normal file
BIN
EXTERNAL/AIA/03 dm_aia_flag.sql:Zone.Identifier
Normal file
Binary file not shown.
112
EXTERNAL/AIA/04 dm_aia_provided_flag.sql
Normal file
112
EXTERNAL/AIA/04 dm_aia_provided_flag.sql
Normal file
@@ -0,0 +1,112 @@
|
|||||||
|
-- Databricks notebook source
|
||||||
|
-- MAGIC %run ../../../Common/config
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
-- MAGIC %python
|
||||||
|
-- MAGIC spark.read.table(f'`{CDW_CATALOG}`.`dwd`.`dim_product_wide`').createOrReplaceTempView('cdw_dwd_dim_product_wide')
|
||||||
|
-- MAGIC spark.read.table(f'`{CDW_CATALOG}`.`dwd`.`dwd_hospital_classification`').createOrReplaceTempView('cdw_dwd_dwd_hospital_classification')
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
--连续取数机构
|
||||||
|
----------------------------------------------------------------------------------------------------------------------
|
||||||
|
--修改时间:20241029
|
||||||
|
--修改人:FanXujia
|
||||||
|
--修改内容:
|
||||||
|
--根据事实表最新月,往前推12个月(含最新月),按机构 + 年月粒度汇总,如果每个月销量都不等于0,这家机构称为:滚动一年有数
|
||||||
|
--根据事实表最新月,往前推24个月(含最新月),按机构 + 年月粒度汇总,如果每个月销量都不等于0,这家机构称为:滚动两年有数
|
||||||
|
--2022年没有销量为正常情况,判断时仅考虑2023年01月起销量是否等于0
|
||||||
|
----------------------------------------------------------------------------------------------------------------------
|
||||||
|
insert overwrite table dm.dm_aia_provided_flag
|
||||||
|
with max_ym as (
|
||||||
|
select max(ym) as max_ym
|
||||||
|
from dm.dm_ext_aia_sales
|
||||||
|
)
|
||||||
|
,ym_range as (
|
||||||
|
select max_ym,
|
||||||
|
case when max_ym <= '202412' then cast(right(max_ym,2) as int)
|
||||||
|
else cast(12 as int)
|
||||||
|
end ym_12m,
|
||||||
|
case when max_ym <= '202312' then '202401'
|
||||||
|
when right(max_ym,2) = '12' then concat(left(max_ym,4),'01')
|
||||||
|
else concat(cast(left(max_ym, 4) - 1 as int),right(concat('0', cast(right(max_ym, 2) + 1 as int)), 2))
|
||||||
|
end as ym_12m_before,
|
||||||
|
case when max_ym <= '202312' then cast(right(max_ym,2) as int)
|
||||||
|
when max_ym <= '202512' then cast(right(max_ym,2) as int) + 12
|
||||||
|
else cast(24 as int)
|
||||||
|
end as ym_24m,
|
||||||
|
case when max_ym <= '202512' then '202401'
|
||||||
|
when right(max_ym,2) = '12' then concat(cast(left(max_ym,4) - 1 as int),'01')
|
||||||
|
else concat(cast(left(max_ym, 4) -2 as int),right(concat('0', cast(right(max_ym, 2) + 1 as int)), 2))
|
||||||
|
end as ym_24m_before
|
||||||
|
from max_ym
|
||||||
|
)
|
||||||
|
,sales_1 as (
|
||||||
|
select
|
||||||
|
inst_code,
|
||||||
|
ym
|
||||||
|
from
|
||||||
|
dm.dm_ext_aia_sales
|
||||||
|
group by
|
||||||
|
ym,
|
||||||
|
inst_code
|
||||||
|
having
|
||||||
|
sum(value) <> 0
|
||||||
|
)
|
||||||
|
,sales_2 as (
|
||||||
|
select
|
||||||
|
t1.inst_code,
|
||||||
|
t1.ym,
|
||||||
|
t2.max_ym,
|
||||||
|
t2.ym_12m,
|
||||||
|
t2.ym_12m_before,
|
||||||
|
t2.ym_24m,
|
||||||
|
t2.ym_24m_before
|
||||||
|
from sales_1 t1
|
||||||
|
cross join ym_range t2
|
||||||
|
)
|
||||||
|
,flag_12m as (
|
||||||
|
select inst_code,
|
||||||
|
'滚动一年有数' as aia_provided_flag,
|
||||||
|
'AIA(Monthly)' as DATA_SOURCE
|
||||||
|
from sales_2
|
||||||
|
where ym <= max_ym
|
||||||
|
and ym >= ym_12m_before
|
||||||
|
group by inst_code
|
||||||
|
having count(1) = (select ym_12m from ym_range)
|
||||||
|
)
|
||||||
|
,flag_24m as (
|
||||||
|
select inst_code,
|
||||||
|
'滚动两年有数' as aia_provided_flag,
|
||||||
|
'AIA(Monthly)' as DATA_SOURCE
|
||||||
|
from sales_2
|
||||||
|
where ym <= max_ym
|
||||||
|
and ym >= ym_24m_before
|
||||||
|
group by inst_code
|
||||||
|
having count(1) = (select ym_24m from ym_range)
|
||||||
|
)
|
||||||
|
|
||||||
|
select inst_code,
|
||||||
|
aia_provided_flag,
|
||||||
|
DATA_SOURCE
|
||||||
|
from flag_12m
|
||||||
|
union all
|
||||||
|
select inst_code,
|
||||||
|
aia_provided_flag,
|
||||||
|
DATA_SOURCE
|
||||||
|
from flag_24m
|
||||||
|
union all
|
||||||
|
select '' as inst_code,'' as aia_provided_flag,'IQVIA-CHPA(Monthly)' as data_source
|
||||||
|
union all
|
||||||
|
select '' as inst_code,'' as aia_provided_flag,'XH Data(Quarterly)' as data_source
|
||||||
|
union all
|
||||||
|
select '' as inst_code,'' as aia_provided_flag,'EC(Monthly)' as data_source
|
||||||
|
union all
|
||||||
|
select '' as inst_code,'' as aia_provided_flag,'Retail(Quarterly)' as data_source
|
||||||
|
union all
|
||||||
|
select '' as inst_code,'' as aia_provided_flag,'CHC(Quarterly)' as data_source
|
||||||
|
union all
|
||||||
|
select '' as inst_code,'' as aia_provided_flag,'THC(Quarterly)' as data_source
|
||||||
|
union all
|
||||||
|
select '' as inst_code,'' as aia_provided_flag,'IQVIA-COUNTY(Quarterly)' as data_source
|
||||||
BIN
EXTERNAL/AIA/04 dm_aia_provided_flag.sql:Zone.Identifier
Normal file
BIN
EXTERNAL/AIA/04 dm_aia_provided_flag.sql:Zone.Identifier
Normal file
Binary file not shown.
146
EXTERNAL/AIA/06 DM_TD_EXT_AIA_PACK_PROPERTY.sql
Normal file
146
EXTERNAL/AIA/06 DM_TD_EXT_AIA_PACK_PROPERTY.sql
Normal file
@@ -0,0 +1,146 @@
|
|||||||
|
-- Databricks notebook source
|
||||||
|
-- CREATE OR REPLACE TABLE DM.DM_TD_EXT_AIA_PACK_PROPERTY
|
||||||
|
-- (
|
||||||
|
-- -- MARKET_PACK_KEY STRING,
|
||||||
|
-- PACK_CODE STRING,
|
||||||
|
-- PACK_DESC STRING,
|
||||||
|
-- STGH_DESC STRING,
|
||||||
|
-- PACK_LCH STRING,
|
||||||
|
-- FAMILY_CODE STRING,
|
||||||
|
-- FAMILY_NAME STRING,
|
||||||
|
-- PROD_CODE STRING,
|
||||||
|
-- PROD_DESC STRING,
|
||||||
|
-- PROD_DESC_C STRING,
|
||||||
|
-- CMPS_CODE STRING,
|
||||||
|
-- CMPS_DESC STRING,
|
||||||
|
-- CMPS_DESC_C STRING,
|
||||||
|
-- ATC1_CODE STRING,
|
||||||
|
-- ATC2_CODE STRING,
|
||||||
|
-- ATC3_CODE STRING,
|
||||||
|
-- ATC4_CODE STRING,
|
||||||
|
-- APP1_CODE STRING,
|
||||||
|
-- APP2_CODE STRING,
|
||||||
|
-- APP3_CODE STRING,
|
||||||
|
-- BIO_DESC STRING,
|
||||||
|
-- GENE_ORIG_DESC STRING,
|
||||||
|
-- ETH_OTC_DESC STRING,
|
||||||
|
-- NRDL_DESC STRING,
|
||||||
|
-- NRDL_ENTRY_DATE STRING,
|
||||||
|
-- EDL_DESC STRING,
|
||||||
|
-- TCM_DESC STRING,
|
||||||
|
-- PAED_DESC STRING,
|
||||||
|
-- GQCE_DESC STRING,
|
||||||
|
-- VBP_DESC_V STRING,
|
||||||
|
-- VBP_DESC STRING,
|
||||||
|
-- MANU_CODE STRING,
|
||||||
|
-- MANU_DESC STRING,
|
||||||
|
-- MANU_DESC_C STRING,
|
||||||
|
-- MNFL_CODE STRING,
|
||||||
|
-- MNFL_DESC STRING,
|
||||||
|
-- CORP_CODE STRING,
|
||||||
|
-- CORP_DESC STRING,
|
||||||
|
-- CORP_DESC_C STRING,
|
||||||
|
-- BRANDTYPE STRING,
|
||||||
|
-- -- MARKET STRING,
|
||||||
|
-- -- KEY_COMPETITOR STRING,
|
||||||
|
-- IS_AZ STRING,
|
||||||
|
-- AZ_MAIN STRING,
|
||||||
|
-- -- AZ_RELATED STRING,
|
||||||
|
-- ATC1_DESC STRING,
|
||||||
|
-- ATC1_DESC_C STRING,
|
||||||
|
-- ATC2_DESC STRING,
|
||||||
|
-- ATC2_DESC_C STRING,
|
||||||
|
-- ATC3_DESC STRING,
|
||||||
|
-- ATC3_DESC_C STRING,
|
||||||
|
-- ATC4_DESC STRING,
|
||||||
|
-- ATC4_DESC_C STRING,
|
||||||
|
-- APP1_DESC STRING,
|
||||||
|
-- APP1_DESC_C STRING,
|
||||||
|
-- APP2_DESC STRING,
|
||||||
|
-- APP2_DESC_C STRING,
|
||||||
|
-- APP3_DESC STRING,
|
||||||
|
-- APP3_DESC_C STRING,
|
||||||
|
-- -- CLASS STRING,
|
||||||
|
-- -- MARKET_RATIO STRING,
|
||||||
|
-- COUNTINGUNIT STRING,
|
||||||
|
-- VBP_BRAND STRING,
|
||||||
|
-- REPLENISH_FALG STRING,
|
||||||
|
-- ETL_INSERT_DT TIMESTAMP,
|
||||||
|
-- ETL_UPDATE_DT TIMESTAMP
|
||||||
|
-- )
|
||||||
|
-- USING delta
|
||||||
|
-- -- LOCATION 'abfss://master@azcdatalakeprd.dfs.core.chinacloudapi.cn/DM/dm_td_ext_aia_pack_property';
|
||||||
|
-- -- 上面是生产环境location,下面是测试环境location
|
||||||
|
-- LOCATION 'abfss://master@retaildlstoragetest.dfs.core.chinacloudapi.cn/DM/dm_td_ext_aia_pack_property';
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
INSERT OVERWRITE TABLE DM.DM_TD_EXT_AIA_PACK_PROPERTY
|
||||||
|
SELECT
|
||||||
|
-- MARKET_PACK_KEY,
|
||||||
|
PACK_COD AS PACK_CODE,
|
||||||
|
PACK_DES AS PACK_DESC,
|
||||||
|
STGH_DES AS STGH_DESC,
|
||||||
|
PACK_LCH,
|
||||||
|
FAMILY_CODE,
|
||||||
|
FAMILY_NAME,
|
||||||
|
PROD_COD AS PROD_CODE,
|
||||||
|
PROD_DES AS PROD_DESC,
|
||||||
|
PROD_DES_C AS PROD_DESC_C,
|
||||||
|
CMPS_COD AS CMPS_CODE,
|
||||||
|
CMPS_DES AS CMPS_DESC,
|
||||||
|
CMPS_DES_C AS CMPS_DESC_C,
|
||||||
|
ATC1_COD AS ATC1_CODE,
|
||||||
|
ATC2_COD AS ATC2_CODE,
|
||||||
|
ATC3_COD AS ATC3_CODE,
|
||||||
|
ATC4_COD AS ATC4_CODE,
|
||||||
|
APP1_COD AS APP1_CODE,
|
||||||
|
APP2_COD AS APP2_CODE,
|
||||||
|
APP3_COD AS APP3_CODE,
|
||||||
|
BIO_DESC,
|
||||||
|
GENE_ORIG_DESC,
|
||||||
|
ETH_OTC_DESC,
|
||||||
|
NRDL_DESC,
|
||||||
|
NRDL_ENTRY_DATE,
|
||||||
|
EDL_DESC,
|
||||||
|
TCM_DESC,
|
||||||
|
PAED_DESC,
|
||||||
|
GQCE_DESC,
|
||||||
|
null as VBP_DESC_V,
|
||||||
|
VBP_DESC,
|
||||||
|
MANU_COD AS MANU_CODE,
|
||||||
|
MANU_DES AS MANU_DESC,
|
||||||
|
MANU_DES_C AS MANU_DESC_C,
|
||||||
|
MNFL_COD AS MNFL_CODE,
|
||||||
|
MNFL_DES AS MNFL_DESC,
|
||||||
|
CORP_COD AS CORP_CODE,
|
||||||
|
CORP_DES AS CORP_DESC,
|
||||||
|
CORP_DES_C AS CORP_DESC_C,
|
||||||
|
BRANDTYPE,
|
||||||
|
-- MARKET,
|
||||||
|
-- KEY_COMPETITOR,
|
||||||
|
IS_AZ,
|
||||||
|
AZ_MAIN,
|
||||||
|
-- AZ_RELATED,
|
||||||
|
ATC1_DES AS ATC1_DESC,
|
||||||
|
ATC1_DES_C AS ATC1_DESC_C,
|
||||||
|
ATC2_DES AS ATC2_DESC,
|
||||||
|
ATC2_DES_C AS ATC2_DESC_C,
|
||||||
|
ATC3_DES AS ATC3_DESC,
|
||||||
|
ATC3_DES_C AS ATC3_DESC_C,
|
||||||
|
ATC4_DES AS ATC4_DESC,
|
||||||
|
ATC4_DES_C AS ATC4_DESC_C,
|
||||||
|
APP1_DES AS APP1_DESC,
|
||||||
|
APP1_DES_C AS APP1_DESC_C,
|
||||||
|
APP2_DES AS APP2_DESC,
|
||||||
|
APP2_DES_C AS APP2_DESC_C,
|
||||||
|
APP3_DES AS APP3_DESC,
|
||||||
|
APP3_DES_C AS APP3_DESC_C,
|
||||||
|
-- CLASS,
|
||||||
|
-- NULL AS MARKET_RATIO,
|
||||||
|
NULL AS COUNTINGUNIT,
|
||||||
|
NULL AS VBP_BRAND,
|
||||||
|
NULL AS REPLENISH_FALG,
|
||||||
|
FROM_UTC_TIMESTAMP(CURRENT_TIMESTAMP(),'UTC+8') AS ETL_INSERT_DT,
|
||||||
|
FROM_UTC_TIMESTAMP(CURRENT_TIMESTAMP(),'UTC+8') AS ETL_UPDATE_DT
|
||||||
|
FROM DM.DM_AIA_PACK_PROPERTY;
|
||||||
BIN
EXTERNAL/AIA/06 DM_TD_EXT_AIA_PACK_PROPERTY.sql:Zone.Identifier
Normal file
BIN
EXTERNAL/AIA/06 DM_TD_EXT_AIA_PACK_PROPERTY.sql:Zone.Identifier
Normal file
Binary file not shown.
339
EXTERNAL/AIA/07 DM_TD_EXT_AIA_PACK2MARKET.sql
Normal file
339
EXTERNAL/AIA/07 DM_TD_EXT_AIA_PACK2MARKET.sql
Normal file
@@ -0,0 +1,339 @@
|
|||||||
|
-- Databricks notebook source
|
||||||
|
-- CREATE OR REPLACE TABLE DM.DM_TD_EXT_AIA_PACK2MARKET (
|
||||||
|
-- MARKET STRING,
|
||||||
|
-- PACK_CODE STRING,
|
||||||
|
-- PACK_DESC STRING,
|
||||||
|
-- STGH_DESC STRING,
|
||||||
|
-- PACK_LCH STRING,
|
||||||
|
-- PROD_CODE STRING,
|
||||||
|
-- CMPS_CODE STRING,
|
||||||
|
-- CMPS_DESC STRING,
|
||||||
|
-- ATC1_CODE STRING,
|
||||||
|
-- ATC2_CODE STRING,
|
||||||
|
-- ATC3_CODE STRING,
|
||||||
|
-- ATC4_CODE STRING,
|
||||||
|
-- APP1_CODE STRING,
|
||||||
|
-- APP2_CODE STRING,
|
||||||
|
-- APP3_CODE STRING,
|
||||||
|
-- BIO_DESC STRING,
|
||||||
|
-- GENE_ORIG_DESC STRING,
|
||||||
|
-- ETH_OTC_DESC STRING,
|
||||||
|
-- NRDL_DESC STRING,
|
||||||
|
-- NRDL_ENTRY_DATE STRING,
|
||||||
|
-- EDL_DESC STRING,
|
||||||
|
-- TCM_DESC STRING,
|
||||||
|
-- PAED_DESC STRING,
|
||||||
|
-- GQCE_DESC STRING,
|
||||||
|
-- VBP_DESC STRING,
|
||||||
|
-- MANU_CODE STRING,
|
||||||
|
-- MANU_DESC STRING,
|
||||||
|
-- MNFL_CODE STRING,
|
||||||
|
-- MNFL_DESC STRING,
|
||||||
|
-- CORP_CODE STRING,
|
||||||
|
-- CORP_DESC STRING,
|
||||||
|
-- BRANDTYPE STRING,
|
||||||
|
-- BU STRING,
|
||||||
|
-- STARTTIME STRING,
|
||||||
|
-- ENDTIME STRING,
|
||||||
|
-- MARKET_RATIO STRING,
|
||||||
|
-- ETL_INSERT_DT TIMESTAMP,
|
||||||
|
-- ETL_UPDATE_DT TIMESTAMP)
|
||||||
|
-- USING delta
|
||||||
|
-- -- LOCATION 'abfss://master@azcdatalakeprd.dfs.core.chinacloudapi.cn/DM/dm_td_ext_aia_pack2market';
|
||||||
|
-- -- 上面是生产环境location,下面是测试环境location
|
||||||
|
-- LOCATION 'abfss://master@retaildlstoragetest.dfs.core.chinacloudapi.cn/DM/dm_td_ext_aia_pack2market';
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
-- CREATE OR REPLACE TABLE DM.DM_TD_EXT_AIA_PACK2MARKET_KEYCOMPETITOR (
|
||||||
|
-- KEY_COMPETITOR STRING,
|
||||||
|
-- MARKET STRING,
|
||||||
|
-- PACK_CODE STRING)
|
||||||
|
-- USING delta
|
||||||
|
-- -- LOCATION 'abfss://master@azcdatalakeprd.dfs.core.chinacloudapi.cn/DM/dm_td_ext_aia_pack2market_keycompetitor';
|
||||||
|
-- -- 上面是生产环境location,下面是测试环境location
|
||||||
|
-- LOCATION 'abfss://master@retaildlstoragetest.dfs.core.chinacloudapi.cn/DM/dm_td_ext_aia_pack2market_keycompetitor';
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
-- CREATE or REPLACE TABLE DM.DM_TD_EXT_AIA_PACK2MARKET_CLASS (
|
||||||
|
-- MARKET STRING,
|
||||||
|
-- PACK_CODE STRING,
|
||||||
|
-- CLASS STRING)
|
||||||
|
-- USING delta
|
||||||
|
-- -- LOCATION 'abfss://master@azcdatalakeprd.dfs.core.chinacloudapi.cn/DM/dm_td_ext_aia_pack2market_class';
|
||||||
|
-- -- 上面是生产环境location,下面是测试环境location
|
||||||
|
-- LOCATION 'abfss://master@retaildlstoragetest.dfs.core.chinacloudapi.cn/DM/dm_td_ext_aia_pack2market_class';
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
select * from DM.DM_TD_EXT_AIA_PACK2MARKET where PACK_CODE = '000097274004'
|
||||||
|
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
INSERT OVERWRITE TABLE DM.DM_TD_EXT_AIA_PACK2MARKET
|
||||||
|
-- 包含规则
|
||||||
|
SELECT DISTINCT
|
||||||
|
T2.MARKET,
|
||||||
|
T1.PACK_CODE,
|
||||||
|
T1.PACK_DESC,
|
||||||
|
T1.STGH_DESC,
|
||||||
|
T1.PACK_LCH,
|
||||||
|
T1.PROD_CODE,
|
||||||
|
T1.CMPS_CODE,
|
||||||
|
T1.CMPS_DESC,
|
||||||
|
T1.ATC1_CODE,
|
||||||
|
T1.ATC2_CODE,
|
||||||
|
T1.ATC3_CODE,
|
||||||
|
T1.ATC4_CODE,
|
||||||
|
T1.APP1_CODE,
|
||||||
|
T1.APP2_CODE,
|
||||||
|
T1.APP3_CODE,
|
||||||
|
T1.BIO_DESC,
|
||||||
|
T1.GENE_ORIG_DESC,
|
||||||
|
T1.ETH_OTC_DESC,
|
||||||
|
T1.NRDL_DESC,
|
||||||
|
T1.NRDL_ENTRY_DATE,
|
||||||
|
T1.EDL_DESC,
|
||||||
|
T1.TCM_DESC,
|
||||||
|
T1.PAED_DESC,
|
||||||
|
T1.GQCE_DESC,
|
||||||
|
T1.VBP_DESC,
|
||||||
|
T1.MANU_CODE,
|
||||||
|
T1.MANU_DESC,
|
||||||
|
T1.MNFL_CODE,
|
||||||
|
T1.MNFL_DESC,
|
||||||
|
T1.CORP_CODE,
|
||||||
|
T1.CORP_DESC,
|
||||||
|
T1.BRANDTYPE,
|
||||||
|
T2.BU,
|
||||||
|
T2.STARTTIME,
|
||||||
|
T2.ENDTIME,
|
||||||
|
COALESCE(T2.EXTEND_MARKET_RATIO, '1') AS MARKET_RATIO,
|
||||||
|
FROM_UTC_TIMESTAMP(CURRENT_TIMESTAMP(),'UTC+8') AS ETL_INSERT_DT,
|
||||||
|
FROM_UTC_TIMESTAMP(CURRENT_TIMESTAMP(),'UTC+8') AS ETL_UPDATE_DT
|
||||||
|
FROM DM.DM_TD_EXT_AIA_PACK_PROPERTY AS T1
|
||||||
|
LEFT JOIN (
|
||||||
|
SELECT
|
||||||
|
MARKET,
|
||||||
|
BU,
|
||||||
|
STARTTIME,
|
||||||
|
ENDTIME,
|
||||||
|
EXTEND_MARKET_RATIO,
|
||||||
|
ATC1_CODE,
|
||||||
|
ATC2_CODE,
|
||||||
|
ATC3_CODE,
|
||||||
|
ATC4_CODE,
|
||||||
|
NFC1_CODE,
|
||||||
|
NFC2_CODE,
|
||||||
|
NFC3_CODE,
|
||||||
|
CORPORATION_CODE,
|
||||||
|
MANUFACTURER_CODE,
|
||||||
|
PRODUCT_CODE,
|
||||||
|
PACK_CODE,
|
||||||
|
STRENGTH,
|
||||||
|
MOLECULE_CODE
|
||||||
|
FROM DWD.DWD_GND_IMS_TBLMARKET_BYMONTH
|
||||||
|
WHERE EXTEND_MARKET IS NULL AND (NOT_IN_FLAG IS NULL OR NOT_IN_FLAG = '1')
|
||||||
|
) AS T2
|
||||||
|
ON (T2.ATC1_CODE IS NULL OR T1.ATC1_CODE = T2.ATC1_CODE)
|
||||||
|
AND (T2.ATC2_CODE IS NULL OR T1.ATC2_CODE = T2.ATC2_CODE)
|
||||||
|
AND (T2.ATC3_CODE IS NULL OR T1.ATC3_CODE = T2.ATC3_CODE)
|
||||||
|
AND (T2.ATC4_CODE IS NULL OR T1.ATC4_CODE = T2.ATC4_CODE)
|
||||||
|
AND (T2.NFC1_CODE IS NULL OR T1.APP1_CODE = T2.NFC1_CODE)
|
||||||
|
AND (T2.NFC2_CODE IS NULL OR T1.APP2_CODE = T2.NFC2_CODE)
|
||||||
|
AND (T2.NFC3_CODE IS NULL OR T1.APP3_CODE = T2.NFC3_CODE)
|
||||||
|
AND (T2.CORPORATION_CODE IS NULL OR T1.CORP_CODE = T2.CORPORATION_CODE)
|
||||||
|
AND (T2.MANUFACTURER_CODE IS NULL OR T1.MANU_CODE = T2.MANUFACTURER_CODE)
|
||||||
|
AND (T2.PRODUCT_CODE IS NULL OR T1.PROD_CODE = T2.PRODUCT_CODE)
|
||||||
|
AND (T2.PACK_CODE IS NULL OR T1.PACK_CODE = T2.PACK_CODE)
|
||||||
|
AND (T2.STRENGTH IS NULL OR T1.STGH_DESC = T2.STRENGTH)
|
||||||
|
AND (T2.MOLECULE_CODE IS NULL OR T1.CMPS_CODE = T2.MOLECULE_CODE)
|
||||||
|
WHERE T2.MARKET IS NOT NULL
|
||||||
|
|
||||||
|
EXCEPT
|
||||||
|
|
||||||
|
-- 排除规则
|
||||||
|
SELECT DISTINCT
|
||||||
|
T2.MARKET,
|
||||||
|
T1.PACK_CODE,
|
||||||
|
T1.PACK_DESC,
|
||||||
|
T1.STGH_DESC,
|
||||||
|
T1.PACK_LCH,
|
||||||
|
T1.PROD_CODE,
|
||||||
|
T1.CMPS_CODE,
|
||||||
|
T1.CMPS_DESC,
|
||||||
|
T1.ATC1_CODE,
|
||||||
|
T1.ATC2_CODE,
|
||||||
|
T1.ATC3_CODE,
|
||||||
|
T1.ATC4_CODE,
|
||||||
|
T1.APP1_CODE,
|
||||||
|
T1.APP2_CODE,
|
||||||
|
T1.APP3_CODE,
|
||||||
|
T1.BIO_DESC,
|
||||||
|
T1.GENE_ORIG_DESC,
|
||||||
|
T1.ETH_OTC_DESC,
|
||||||
|
T1.NRDL_DESC,
|
||||||
|
T1.NRDL_ENTRY_DATE,
|
||||||
|
T1.EDL_DESC,
|
||||||
|
T1.TCM_DESC,
|
||||||
|
T1.PAED_DESC,
|
||||||
|
T1.GQCE_DESC,
|
||||||
|
T1.VBP_DESC,
|
||||||
|
T1.MANU_CODE,
|
||||||
|
T1.MANU_DESC,
|
||||||
|
T1.MNFL_CODE,
|
||||||
|
T1.MNFL_DESC,
|
||||||
|
T1.CORP_CODE,
|
||||||
|
T1.CORP_DESC,
|
||||||
|
T1.BRANDTYPE,
|
||||||
|
T2.BU,
|
||||||
|
T2.STARTTIME,
|
||||||
|
T2.ENDTIME,
|
||||||
|
COALESCE(T2.EXTEND_MARKET_RATIO, '1') AS MARKET_RATIO,
|
||||||
|
FROM_UTC_TIMESTAMP(CURRENT_TIMESTAMP(),'UTC+8') AS ETL_INSERT_DT,
|
||||||
|
FROM_UTC_TIMESTAMP(CURRENT_TIMESTAMP(),'UTC+8') AS ETL_UPDATE_DT
|
||||||
|
FROM DM.DM_TD_EXT_AIA_PACK_PROPERTY AS T1
|
||||||
|
LEFT JOIN (
|
||||||
|
SELECT
|
||||||
|
MARKET,
|
||||||
|
BU,
|
||||||
|
STARTTIME,
|
||||||
|
ENDTIME,
|
||||||
|
EXTEND_MARKET_RATIO,
|
||||||
|
ATC1_CODE,
|
||||||
|
ATC2_CODE,
|
||||||
|
ATC3_CODE,
|
||||||
|
ATC4_CODE,
|
||||||
|
NFC1_CODE,
|
||||||
|
NFC2_CODE,
|
||||||
|
NFC3_CODE,
|
||||||
|
CORPORATION_CODE,
|
||||||
|
MANUFACTURER_CODE,
|
||||||
|
PRODUCT_CODE,
|
||||||
|
PACK_CODE,
|
||||||
|
STRENGTH,
|
||||||
|
MOLECULE_CODE
|
||||||
|
FROM DWD.DWD_GND_IMS_TBLMARKET_BYMONTH
|
||||||
|
WHERE EXTEND_MARKET IS NULL AND NOT_IN_FLAG = '0'
|
||||||
|
) AS T2
|
||||||
|
ON (T2.ATC1_CODE IS NULL OR T1.ATC1_CODE = T2.ATC1_CODE)
|
||||||
|
AND (T2.ATC2_CODE IS NULL OR T1.ATC2_CODE = T2.ATC2_CODE)
|
||||||
|
AND (T2.ATC3_CODE IS NULL OR T1.ATC3_CODE = T2.ATC3_CODE)
|
||||||
|
AND (T2.ATC4_CODE IS NULL OR T1.ATC4_CODE = T2.ATC4_CODE)
|
||||||
|
AND (T2.NFC1_CODE IS NULL OR T1.APP1_CODE = T2.NFC1_CODE)
|
||||||
|
AND (T2.NFC2_CODE IS NULL OR T1.APP2_CODE = T2.NFC2_CODE)
|
||||||
|
AND (T2.NFC3_CODE IS NULL OR T1.APP3_CODE = T2.NFC3_CODE)
|
||||||
|
AND (T2.CORPORATION_CODE IS NULL OR T1.CORP_CODE = T2.CORPORATION_CODE)
|
||||||
|
AND (T2.MANUFACTURER_CODE IS NULL OR T1.MANU_CODE = T2.MANUFACTURER_CODE)
|
||||||
|
AND (T2.PRODUCT_CODE IS NULL OR T1.PROD_CODE = T2.PRODUCT_CODE)
|
||||||
|
AND (T2.PACK_CODE IS NULL OR T1.PACK_CODE = T2.PACK_CODE)
|
||||||
|
AND (T2.STRENGTH IS NULL OR T1.STGH_DESC = T2.STRENGTH)
|
||||||
|
AND (T2.MOLECULE_CODE IS NULL OR T1.CMPS_CODE = T2.MOLECULE_CODE)
|
||||||
|
WHERE T2.MARKET IS NOT NULL;
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
--删除需要隐藏的市场
|
||||||
|
MERGE INTO DM.DM_TD_EXT_AIA_PACK2MARKET AS target
|
||||||
|
USING dwd.dwd_gnd_ims_hidden_market AS source
|
||||||
|
ON UPPER(target.market) = UPPER(source.hidden_market)
|
||||||
|
AND source.data_source = 'AIA'
|
||||||
|
WHEN MATCHED THEN DELETE;
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
INSERT OVERWRITE TABLE DM.DM_TD_EXT_AIA_PACK2MARKET_KEYCOMPETITOR
|
||||||
|
WITH kc AS (
|
||||||
|
SELECT
|
||||||
|
keycompetitor, no,
|
||||||
|
ROW_NUMBER() OVER (ORDER BY
|
||||||
|
CASE
|
||||||
|
WHEN ATC1_Code IS NOT NULL THEN 1
|
||||||
|
WHEN ATC2_Code IS NOT NULL THEN 2
|
||||||
|
WHEN ATC3_Code IS NOT NULL THEN 3
|
||||||
|
WHEN ATC4_Code IS NOT NULL THEN 4
|
||||||
|
WHEN Molecule_Code IS NOT NULL THEN 5
|
||||||
|
WHEN Product_Code IS NOT NULL THEN 6
|
||||||
|
WHEN Pack_Code IS NOT NULL THEN 7
|
||||||
|
ELSE 999
|
||||||
|
END,
|
||||||
|
CASE WHEN NFC1_CODE IS NULL THEN 0 ELSE 1 END,
|
||||||
|
CASE WHEN NFC2_CODE IS NULL THEN 0 ELSE 1 END,
|
||||||
|
CASE WHEN NFC3_CODE IS NULL THEN 0 ELSE 1 END
|
||||||
|
) AS no1,
|
||||||
|
ATC1_Code, ATC2_Code, ATC3_Code, ATC4_Code,
|
||||||
|
NFC1_Code, NFC2_Code, NFC3_Code,
|
||||||
|
corporation_code, Manufacturer_Code,
|
||||||
|
Product_Code, Pack_Code, Strength, Molecule_Code, market
|
||||||
|
FROM dwd.dwd_gnd_ims_tblkeycompetitor
|
||||||
|
),
|
||||||
|
joined AS (
|
||||||
|
SELECT
|
||||||
|
/*+ BROADCAST(kc) */
|
||||||
|
COALESCE(kc.keycompetitor, 'OTHERS') AS Key_Competitor, -- ✅ UPDATE 逻辑内联
|
||||||
|
t1.market, t1.PACK_CODE
|
||||||
|
/*bug 调试区
|
||||||
|
,
|
||||||
|
kc.no,
|
||||||
|
kc.no1,
|
||||||
|
t1.PROD_COD, t1.CMPS_COD, t1.CMPS_DES,
|
||||||
|
t1.ATC1_COD, t1.ATC2_COD, t1.ATC3_COD, t1.ATC4_COD,
|
||||||
|
t1.APP1_COD, t1.APP2_COD, t1.APP3_COD,
|
||||||
|
t1.BIO_DESC, t1.GENE_ORIG_DESC, t1.ETH_OTC_DESC,
|
||||||
|
t1.NRDL_DESC, t1.NRDL_Entry_Date, t1.EDL_DESC, t1.TCM_DESC,
|
||||||
|
t1.PAED_DESC, t1.GQCE_DESC, t1.VBP_DESC,
|
||||||
|
t1.MANU_COD, t1.MANU_DES, t1.MNFL_COD, t1.MNFL_DES,
|
||||||
|
t1.CORP_COD, t1.CORP_DES, t1.BrandType, t1.bu, t1.Market_Ratio
|
||||||
|
*/
|
||||||
|
,ROW_NUMBER() OVER (
|
||||||
|
PARTITION BY t1.market, t1.PACK_CODE, t1.PROD_CODE
|
||||||
|
ORDER BY kc.no1 DESC, kc.no DESC
|
||||||
|
) AS _rn
|
||||||
|
FROM DM.DM_TD_EXT_AIA_PACK2MARKET t1
|
||||||
|
LEFT JOIN kc
|
||||||
|
ON (kc.ATC1_Code IS NULL OR t1.ATC1_CODE = kc.ATC1_Code)
|
||||||
|
AND (kc.ATC2_Code IS NULL OR t1.ATC2_CODE = kc.ATC2_Code)
|
||||||
|
AND (kc.ATC3_Code IS NULL OR t1.ATC3_CODE = kc.ATC3_Code)
|
||||||
|
AND (kc.ATC4_Code IS NULL OR t1.ATC4_CODE = kc.ATC4_Code)
|
||||||
|
AND (kc.NFC1_Code IS NULL OR t1.APP1_CODE = kc.NFC1_Code)
|
||||||
|
AND (kc.NFC2_Code IS NULL OR t1.APP2_CODE = kc.NFC2_Code)
|
||||||
|
AND (kc.NFC3_Code IS NULL OR t1.APP3_CODE = kc.NFC3_Code)
|
||||||
|
AND (kc.corporation_code IS NULL OR t1.CORP_CODE = kc.corporation_code)
|
||||||
|
AND (kc.Manufacturer_Code IS NULL OR t1.MANU_CODE = kc.Manufacturer_Code)
|
||||||
|
AND (kc.Product_Code IS NULL OR t1.PROD_CODE = kc.Product_Code)
|
||||||
|
AND (kc.Pack_Code IS NULL OR t1.PACK_CODE = kc.Pack_Code)
|
||||||
|
AND (kc.Strength IS NULL OR t1.STGH_DESC = kc.Strength)
|
||||||
|
AND (kc.Molecule_Code IS NULL OR t1.CMPS_CODE = kc.Molecule_Code)
|
||||||
|
AND (kc.market IS NULL OR t1.market = kc.market)
|
||||||
|
)
|
||||||
|
SELECT distinct Key_Competitor,market,pack_code
|
||||||
|
FROM joined
|
||||||
|
WHERE _rn = 1;
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
INSERT OVERWRITE TABLE DM.DM_TD_EXT_AIA_PACK2MARKET_CLASS
|
||||||
|
SELECT
|
||||||
|
/*+ BROADCAST(T2) */
|
||||||
|
DISTINCT
|
||||||
|
T1.MARKET,
|
||||||
|
T1.PACK_CODE,
|
||||||
|
COALESCE( T2.CLASS, 'OTHERS') AS CLASS
|
||||||
|
FROM DM.DM_TD_EXT_AIA_PACK2MARKET T1
|
||||||
|
LEFT JOIN DWD.DWD_GND_TBLCLASS T2
|
||||||
|
ON (T2.MARKET IS NULL OR T1.MARKET = T2.MARKET)
|
||||||
|
AND (T2.PACK_CODE IS NULL OR T1.PACK_CODE = T2.PACK_CODE)
|
||||||
|
AND (T2.PRODUCT_CODE IS NULL OR T1.PROD_CODE = T2.PRODUCT_CODE)
|
||||||
|
AND (T2.MOLECULE_CODE IS NULL OR T1.CMPS_CODE = T2.MOLECULE_CODE)
|
||||||
|
AND (T2.CORPORATION_CODE IS NULL OR T1.CORP_CODE = T2.CORPORATION_CODE)
|
||||||
|
AND (T2.MANUFACTURER_CODE IS NULL OR T1.MANU_CODE = T2.MANUFACTURER_CODE)
|
||||||
|
AND (T2.ATC1_CODE IS NULL OR T1.ATC1_CODE = T2.ATC1_CODE)
|
||||||
|
AND (T2.ATC2_CODE IS NULL OR T1.ATC2_CODE = T2.ATC2_CODE)
|
||||||
|
AND (T2.ATC3_CODE IS NULL OR T1.ATC3_CODE = T2.ATC3_CODE)
|
||||||
|
AND (T2.ATC4_CODE IS NULL OR T1.ATC4_CODE = T2.ATC4_CODE)
|
||||||
|
AND (T2.NFC1_CODE IS NULL OR T1.APP1_CODE = T2.NFC1_CODE)
|
||||||
|
AND (T2.NFC2_CODE IS NULL OR T1.APP2_CODE = T2.NFC2_CODE)
|
||||||
|
AND (T2.NFC3_CODE IS NULL OR T1.APP3_CODE = T2.NFC3_CODE)
|
||||||
|
AND (T2.STRENGTH IS NULL OR T1.STGH_DESC = T2.STRENGTH)
|
||||||
BIN
EXTERNAL/AIA/07 DM_TD_EXT_AIA_PACK2MARKET.sql:Zone.Identifier
Normal file
BIN
EXTERNAL/AIA/07 DM_TD_EXT_AIA_PACK2MARKET.sql:Zone.Identifier
Normal file
Binary file not shown.
30
EXTERNAL/AIA/08 DM_TD_EXT_AIA_MARKET_RATIO.sql
Normal file
30
EXTERNAL/AIA/08 DM_TD_EXT_AIA_MARKET_RATIO.sql
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
-- Databricks notebook source
|
||||||
|
-- CREATE OR REPLACE TABLE DM.DM_TD_EXT_AIA_MARKET_RATIO (
|
||||||
|
-- MARKET STRING,
|
||||||
|
-- PACK_CODE STRING,
|
||||||
|
-- CMPS_CODE STRING,
|
||||||
|
-- STARTTIME STRING,
|
||||||
|
-- ENDTIME STRING,
|
||||||
|
-- MARKET_RATIO STRING,
|
||||||
|
-- ETL_INSERT_DT TIMESTAMP,
|
||||||
|
-- ETL_UPDATE_DT TIMESTAMP)
|
||||||
|
-- USING delta
|
||||||
|
-- -- LOCATION 'abfss://master@azcdatalakeprd.dfs.core.chinacloudapi.cn/DM/dm_td_ext_aia_market_ratio';
|
||||||
|
-- -- 上面是生产环境location,下面是测试环境location
|
||||||
|
-- LOCATION 'abfss://master@retaildlstoragetest.dfs.core.chinacloudapi.cn/DM/dm_td_ext_aia_market_ratio';
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
INSERT OVERWRITE TABLE DM.DM_TD_EXT_AIA_MARKET_RATIO
|
||||||
|
SELECT
|
||||||
|
DISTINCT
|
||||||
|
MARKET,
|
||||||
|
PACK_CODE,
|
||||||
|
CMPS_CODE,
|
||||||
|
STARTTIME,
|
||||||
|
ENDTIME,
|
||||||
|
MARKET_RATIO,
|
||||||
|
FROM_UTC_TIMESTAMP(CURRENT_TIMESTAMP(),'UTC+8') AS ETL_INSERT_DT,
|
||||||
|
FROM_UTC_TIMESTAMP(CURRENT_TIMESTAMP(),'UTC+8') AS ETL_UPDATE_DT
|
||||||
|
FROM DM.DM_TD_EXT_AIA_PACK2MARKET
|
||||||
|
WHERE UPPER(MARKET) NOT LIKE '%ALL%'
|
||||||
BIN
EXTERNAL/AIA/08 DM_TD_EXT_AIA_MARKET_RATIO.sql:Zone.Identifier
Normal file
BIN
EXTERNAL/AIA/08 DM_TD_EXT_AIA_MARKET_RATIO.sql:Zone.Identifier
Normal file
Binary file not shown.
163
EXTERNAL/AIA/09 DM_TD_EXT_AIA_MARKET_BRAND_RATIO.sql
Normal file
163
EXTERNAL/AIA/09 DM_TD_EXT_AIA_MARKET_BRAND_RATIO.sql
Normal file
@@ -0,0 +1,163 @@
|
|||||||
|
-- Databricks notebook source
|
||||||
|
-- CREATE OR REPLACE TABLE DM.DM_TD_EXT_AIA_MARKET_BRAND_RATIO (
|
||||||
|
-- MARKET STRING,
|
||||||
|
-- PACK_CODE STRING,
|
||||||
|
-- VALUE_BRAND_RATIO FLOAT,
|
||||||
|
-- VALUE_BRAND_RATIO_START STRING,
|
||||||
|
-- VALUE_BRAND_RATIO_END STRING,
|
||||||
|
-- UNIT_BRAND_RATIO FLOAT,
|
||||||
|
-- UNIT_BRAND_START STRING,
|
||||||
|
-- UNIT_BRAND_END STRING,
|
||||||
|
-- COUNTINGUNIT_BRAND_RATIO FLOAT,
|
||||||
|
-- COUNTINGUNIT_BRAND_START STRING,
|
||||||
|
-- COUNTINGUNIT_BRAND_END STRING,
|
||||||
|
-- PDOT_BRAND_RATIO FLOAT,
|
||||||
|
-- PDOT_BRAND_START STRING,
|
||||||
|
-- PDOT_BRAND_END STRING,
|
||||||
|
-- ETL_INSERT_DT TIMESTAMP,
|
||||||
|
-- ETL_UPDATE_DT TIMESTAMP)
|
||||||
|
-- USING delta
|
||||||
|
-- -- LOCATION 'abfss://master@azcdatalakeprd.dfs.core.chinacloudapi.cn/DM/dm_td_ext_aia_market_brand_ratio';
|
||||||
|
-- -- 上面是生产环境location,下面是测试环境location
|
||||||
|
-- LOCATION 'abfss://master@retaildlstoragetest.dfs.core.chinacloudapi.cn/DM/dm_td_ext_aia_market_brand_ratio';
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
INSERT OVERWRITE TABLE DM.DM_TD_EXT_AIA_MARKET_BRAND_RATIO
|
||||||
|
SELECT
|
||||||
|
DISTINCT
|
||||||
|
T1.MARKET,
|
||||||
|
T1.PACK_CODE,
|
||||||
|
CASE
|
||||||
|
WHEN T2.RATIO IS NOT NULL THEN T2.RATIO
|
||||||
|
ELSE NVL(T5.RATIO, 1) END AS VALUE_BRAND_RATIO,
|
||||||
|
CASE
|
||||||
|
WHEN T2.RATIO IS NOT NULL THEN T2.STARTTIME
|
||||||
|
ELSE NVL(T5.STARTTIME, 200001) END AS VALUE_BRAND_RATIO_START,
|
||||||
|
CASE
|
||||||
|
WHEN T2.RATIO IS NOT NULL THEN T2.ENDTIME
|
||||||
|
ELSE NVL(T5.ENDTIME, 299912) END AS VALUE_BRAND_RATIO_END,
|
||||||
|
CASE
|
||||||
|
WHEN T3.RATIO IS NOT NULL THEN T3.RATIO
|
||||||
|
ELSE NVL(T6.RATIO, 1) END AS UNIT_BRAND_RATIO,
|
||||||
|
CASE
|
||||||
|
WHEN T3.RATIO IS NOT NULL THEN T3.STARTTIME
|
||||||
|
ELSE NVL(T6.STARTTIME, 200001) END AS UNIT_BRAND_START,
|
||||||
|
CASE
|
||||||
|
WHEN T3.RATIO IS NOT NULL THEN T3.ENDTIME
|
||||||
|
ELSE NVL(T6.ENDTIME, 299912) END AS UNIT_BRAND_END,
|
||||||
|
1 AS COUNTINGUNIT_BRAND_RATIO,
|
||||||
|
200001 AS COUNTINGUNIT_BRAND_START,
|
||||||
|
299912 AS COUNTINGUNIT_BRAND_END,
|
||||||
|
CASE
|
||||||
|
WHEN T4.RATIO IS NOT NULL THEN T4.RATIO
|
||||||
|
ELSE NVL(T7.RATIO, 1) END AS PDOT_BRAND_RATIO,
|
||||||
|
CASE
|
||||||
|
WHEN T4.RATIO IS NOT NULL THEN T4.STARTTIME
|
||||||
|
ELSE NVL(T7.STARTTIME, 200001) END AS PDOT_BRAND_START,
|
||||||
|
CASE
|
||||||
|
WHEN T4.RATIO IS NOT NULL THEN T4.ENDTIME
|
||||||
|
ELSE NVL(T7.ENDTIME, 299912) END AS PDOT_BRAND_END,
|
||||||
|
FROM_UTC_TIMESTAMP(CURRENT_TIMESTAMP(),'UTC+8') AS ETL_INSERT_DT,
|
||||||
|
FROM_UTC_TIMESTAMP(CURRENT_TIMESTAMP(),'UTC+8') AS ETL_UPDATE_DT
|
||||||
|
FROM DM.DM_TD_EXT_AIA_MARKET_RATIO T1
|
||||||
|
LEFT JOIN ( --VALUE--PACK
|
||||||
|
SELECT
|
||||||
|
DISTINCT
|
||||||
|
MARKET,
|
||||||
|
CMPS_COD,
|
||||||
|
CMPS_DES,
|
||||||
|
PACK_COD,
|
||||||
|
PACK_DES,
|
||||||
|
LEVEL,
|
||||||
|
TYPE,
|
||||||
|
CAST(RATIO AS FLOAT) AS RATIO,
|
||||||
|
CAST(STARTTIME AS INT) STARTTIME,
|
||||||
|
CAST(ENDTIME AS INT) ENDTIME
|
||||||
|
FROM DWD.DWD_GND_IMS_TBLBRANDRATIO_BYMONTH
|
||||||
|
WHERE UPPER(TYPE) = 'VALUE' AND UPPER(LEVEL) = 'PACK'
|
||||||
|
) T2 ON T1.MARKET = T2.MARKET AND T1.PACK_CODE = T2.PACK_COD
|
||||||
|
LEFT JOIN ( --UNIT--PACK
|
||||||
|
SELECT
|
||||||
|
DISTINCT
|
||||||
|
MARKET,
|
||||||
|
CMPS_COD,
|
||||||
|
CMPS_DES,
|
||||||
|
PACK_COD,
|
||||||
|
PACK_DES,
|
||||||
|
LEVEL,
|
||||||
|
TYPE,
|
||||||
|
CAST(RATIO AS FLOAT) AS RATIO,
|
||||||
|
CAST(STARTTIME AS INT) STARTTIME,
|
||||||
|
CAST(ENDTIME AS INT) ENDTIME
|
||||||
|
FROM DWD.DWD_GND_IMS_TBLBRANDRATIO_BYMONTH
|
||||||
|
WHERE UPPER(TYPE) = 'UNIT' AND UPPER(LEVEL) = 'PACK'
|
||||||
|
) T3 ON T1.MARKET = T3.MARKET AND T1.PACK_CODE = T3.PACK_COD
|
||||||
|
LEFT JOIN ( --COUNTINGUNIT--PACK
|
||||||
|
SELECT
|
||||||
|
DISTINCT
|
||||||
|
MARKET,
|
||||||
|
CMPS_COD,
|
||||||
|
CMPS_DES,
|
||||||
|
PACK_COD,
|
||||||
|
PACK_DES,
|
||||||
|
LEVEL,
|
||||||
|
TYPE,
|
||||||
|
CAST(RATIO AS FLOAT) AS RATIO,
|
||||||
|
CAST(STARTTIME AS INT) STARTTIME,
|
||||||
|
CAST(ENDTIME AS INT) ENDTIME
|
||||||
|
FROM DWD.DWD_GND_IMS_TBLBRANDRATIO_BYMONTH
|
||||||
|
WHERE UPPER(TYPE) = 'COUNTINGUNIT' AND UPPER(LEVEL) = 'PACK'
|
||||||
|
) T4 ON T1.MARKET = T4.MARKET AND T1.PACK_CODE = T4.PACK_COD
|
||||||
|
LEFT JOIN ( --VALUE--MOLECULE
|
||||||
|
SELECT
|
||||||
|
DISTINCT
|
||||||
|
MARKET,
|
||||||
|
CMPS_COD,
|
||||||
|
CMPS_DES,
|
||||||
|
PACK_COD,
|
||||||
|
PACK_DES,
|
||||||
|
LEVEL,
|
||||||
|
TYPE,
|
||||||
|
CAST(RATIO AS FLOAT) AS RATIO,
|
||||||
|
CAST(STARTTIME AS INT) STARTTIME,
|
||||||
|
CAST(ENDTIME AS INT) ENDTIME
|
||||||
|
FROM DWD.DWD_GND_IMS_TBLBRANDRATIO_BYMONTH
|
||||||
|
WHERE UPPER(TYPE) = 'VALUE' AND UPPER(LEVEL) = 'MOLECULE'
|
||||||
|
) T5 ON T1.MARKET = T5.MARKET AND T1.CMPS_CODE = T5.CMPS_COD
|
||||||
|
LEFT JOIN ( --UNIT--MOLECULE
|
||||||
|
SELECT
|
||||||
|
DISTINCT
|
||||||
|
MARKET,
|
||||||
|
CMPS_COD,
|
||||||
|
CMPS_DES,
|
||||||
|
PACK_COD,
|
||||||
|
PACK_DES,
|
||||||
|
LEVEL,
|
||||||
|
TYPE,
|
||||||
|
CAST(RATIO AS FLOAT) AS RATIO,
|
||||||
|
CAST(STARTTIME AS INT) STARTTIME,
|
||||||
|
CAST(ENDTIME AS INT) ENDTIME
|
||||||
|
FROM DWD.DWD_GND_IMS_TBLBRANDRATIO_BYMONTH
|
||||||
|
WHERE UPPER(TYPE) = 'UNIT' AND UPPER(LEVEL) = 'MOLECULE'
|
||||||
|
) T6 ON T1.MARKET = T6.MARKET AND T1.CMPS_CODE = T6.CMPS_COD
|
||||||
|
LEFT JOIN ( --COUNTINGUNIT--MOLECULE
|
||||||
|
SELECT
|
||||||
|
DISTINCT
|
||||||
|
MARKET,
|
||||||
|
CMPS_COD,
|
||||||
|
CMPS_DES,
|
||||||
|
PACK_COD,
|
||||||
|
PACK_DES,
|
||||||
|
LEVEL,
|
||||||
|
TYPE,
|
||||||
|
CAST(RATIO AS FLOAT) AS RATIO,
|
||||||
|
CAST(STARTTIME AS INT) STARTTIME,
|
||||||
|
CAST(ENDTIME AS INT) ENDTIME
|
||||||
|
FROM DWD.DWD_GND_IMS_TBLBRANDRATIO_BYMONTH
|
||||||
|
WHERE UPPER(TYPE) = 'COUNTINGUNIT' AND UPPER(LEVEL) = 'MOLECULE'
|
||||||
|
) T7 ON T1.MARKET = T7.MARKET
|
||||||
|
AND T1.CMPS_CODE = T7.CMPS_COD
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
Binary file not shown.
71
EXTERNAL/AIA/10 DM_TD_EXT_AIA_MARKET_PACK_MAPPING.sql
Normal file
71
EXTERNAL/AIA/10 DM_TD_EXT_AIA_MARKET_PACK_MAPPING.sql
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
-- Databricks notebook source
|
||||||
|
-- CREATE OR REPLACE TABLE DM.DM_TD_EXT_AIA_MARKET_PACK_MAPPING
|
||||||
|
-- (
|
||||||
|
-- PACK_CODE STRING,
|
||||||
|
-- MARKET STRING,
|
||||||
|
-- CLASS STRING,
|
||||||
|
-- KEY_COMPETITOR STRING,
|
||||||
|
-- MARKET_RATIO STRING,
|
||||||
|
-- STARTTIME STRING,
|
||||||
|
-- ENDTIME STRING,
|
||||||
|
-- VALUE_BRAND_RATIO STRING,
|
||||||
|
-- VALUE_BRAND_RATIO_START STRING,
|
||||||
|
-- VALUE_BRAND_RATIO_END STRING,
|
||||||
|
-- UNIT_BRAND_RATIO STRING,
|
||||||
|
-- UNIT_BRAND_START STRING,
|
||||||
|
-- UNIT_BRAND_END STRING,
|
||||||
|
-- COUNTINGUNIT_BRAND_RATIO STRING,
|
||||||
|
-- COUNTINGUNIT_BRAND_START STRING,
|
||||||
|
-- COUNTINGUNIT_BRAND_END STRING,
|
||||||
|
-- PDOT_BRAND_RATIO STRING,
|
||||||
|
-- PDOT_BRAND_START STRING,
|
||||||
|
-- PDOT_BRAND_END STRING,
|
||||||
|
-- ETL_INSERT_DT TIMESTAMP,
|
||||||
|
-- ETL_UPDATE_DT TIMESTAMP
|
||||||
|
-- )
|
||||||
|
-- USING delta
|
||||||
|
-- -- LOCATION 'abfss://master@azcdatalakeprd.dfs.core.chinacloudapi.cn/DM/dm_td_ext_aia_market_pack_mapping';
|
||||||
|
-- -- 上面是生产环境location,下面是测试环境location
|
||||||
|
-- LOCATION 'abfss://master@retaildlstoragetest.dfs.core.chinacloudapi.cn/DM/dm_td_ext_aia_market_pack_mapping';
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
INSERT OVERWRITE TABLE DM.DM_TD_EXT_AIA_MARKET_PACK_MAPPING
|
||||||
|
SELECT DISTINCT
|
||||||
|
T1.PACK_CODE,
|
||||||
|
T1.MARKET,
|
||||||
|
T4.CLASS,
|
||||||
|
T5.KEY_COMPETITOR,
|
||||||
|
T2.MARKET_RATIO,
|
||||||
|
T2.STARTTIME,
|
||||||
|
T2.ENDTIME,
|
||||||
|
T3.VALUE_BRAND_RATIO,
|
||||||
|
T3.VALUE_BRAND_RATIO_START,
|
||||||
|
T3.VALUE_BRAND_RATIO_END,
|
||||||
|
T3.UNIT_BRAND_RATIO,
|
||||||
|
T3.UNIT_BRAND_START,
|
||||||
|
T3.UNIT_BRAND_END,
|
||||||
|
T3.COUNTINGUNIT_BRAND_RATIO,
|
||||||
|
T3.COUNTINGUNIT_BRAND_START,
|
||||||
|
T3.COUNTINGUNIT_BRAND_END,
|
||||||
|
T3.PDOT_BRAND_RATIO,
|
||||||
|
T3.PDOT_BRAND_START,
|
||||||
|
T3.PDOT_BRAND_END,
|
||||||
|
FROM_UTC_TIMESTAMP(CURRENT_TIMESTAMP(), 'UTC+8') AS ETL_INSERT_DT,
|
||||||
|
FROM_UTC_TIMESTAMP(CURRENT_TIMESTAMP(), 'UTC+8') AS ETL_UPDATE_DT
|
||||||
|
FROM
|
||||||
|
DM.DM_TD_EXT_AIA_PACK2MARKET T1
|
||||||
|
LEFT JOIN DM.DM_TD_EXT_AIA_MARKET_RATIO T2
|
||||||
|
ON T1.MARKET = T2.MARKET
|
||||||
|
AND T1.PACK_CODE = T2.PACK_CODE
|
||||||
|
LEFT JOIN DM.DM_TD_EXT_AIA_MARKET_BRAND_RATIO T3
|
||||||
|
ON T1.MARKET = T3.MARKET
|
||||||
|
AND T1.PACK_CODE = T3.PACK_CODE
|
||||||
|
LEFT JOIN DM.DM_TD_EXT_AIA_PACK2MARKET_CLASS T4
|
||||||
|
ON T1.PACK_CODE = T4.PACK_CODE
|
||||||
|
AND T1.MARKET = T4.MARKET
|
||||||
|
LEFT JOIN DM.DM_TD_EXT_AIA_PACK2MARKET_KEYCOMPETITOR T5
|
||||||
|
ON T1.PACK_CODE = T5.PACK_CODE
|
||||||
|
AND T1.MARKET = T5.MARKET
|
||||||
|
WHERE
|
||||||
|
UPPER(T1.MARKET) NOT LIKE "%ALL%"
|
||||||
Binary file not shown.
366
EXTERNAL/AIA/11 dm_aia_targethp_flag.sql
Normal file
366
EXTERNAL/AIA/11 dm_aia_targethp_flag.sql
Normal file
@@ -0,0 +1,366 @@
|
|||||||
|
-- Databricks notebook source
|
||||||
|
-- MAGIC %run ../../../Common/config
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
-- MAGIC %python
|
||||||
|
-- MAGIC spark.read.table(f'`{CDW_CATALOG}`.`dwd`.`dim_product_wide`').createOrReplaceTempView('cdw_dwd_dim_product_wide')
|
||||||
|
-- MAGIC spark.read.table(f'`{CDW_CATALOG}`.`dwd`.`dwd_hospital_classification`').createOrReplaceTempView('cdw_dwd_dwd_hospital_classification')
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
---------------------------------------------------------------------------
|
||||||
|
--修改时间:20241108
|
||||||
|
--修改人:Fanxujia
|
||||||
|
--修改内容:
|
||||||
|
--for AIA Dashboard
|
||||||
|
--增加sub_channel,dragon_flag,ka_flag,target_ins_level字段
|
||||||
|
---------------------------------------------------------------------------
|
||||||
|
---------------------------------------------------------------------------
|
||||||
|
--修改时间:20241209
|
||||||
|
--修改人:Fanxujia
|
||||||
|
--修改内容:
|
||||||
|
--增加sub channel的排序
|
||||||
|
---------------------------------------------------------------------------
|
||||||
|
--目标医院
|
||||||
|
insert overwrite table dm.dm_aia_targethp_flag --获取全部的机构 + 市场
|
||||||
|
with sales as (
|
||||||
|
select distinct t1.inst_code,t2.MARKET
|
||||||
|
from dm.dm_ext_aia_sales t1
|
||||||
|
left join DM.DM_TD_EXT_AIA_MARKET_PACK_MAPPING t2
|
||||||
|
on t1.pack_cod = t2.PACK_CODE
|
||||||
|
)
|
||||||
|
--获取产品与市场的映射关系
|
||||||
|
,pack_market as (
|
||||||
|
select distinct
|
||||||
|
PACK_CODE as PACK_COD,
|
||||||
|
MARKET
|
||||||
|
from DM.DM_TD_EXT_AIA_MARKET_PACK_MAPPING
|
||||||
|
)
|
||||||
|
--辖区表,获取到KA_Flag、target_ins_level以及目标机构Flag(在辖区表里存在的就是目标机构)
|
||||||
|
,td_territory_inst as (
|
||||||
|
select distinct
|
||||||
|
inst_code,
|
||||||
|
trty_code,
|
||||||
|
family_code,
|
||||||
|
ka_flag,
|
||||||
|
TARGET_LEVEL as target_ins_level
|
||||||
|
from dm.dm_td_sd_territory_indication --eagle的非重点医院同样加为目标机构
|
||||||
|
where yyyymm in (
|
||||||
|
select
|
||||||
|
key_value
|
||||||
|
from
|
||||||
|
dm.dm_td_date_config
|
||||||
|
where
|
||||||
|
key_code = 'pfmc_cvh'
|
||||||
|
)
|
||||||
|
and bu not in ('CUBE','KA')
|
||||||
|
and key_hp<>3 --chenwu 20250904 排除双考影响
|
||||||
|
)
|
||||||
|
--汇报线表,获取到Channel,后续用于判断出sub_channel
|
||||||
|
,td_org as (
|
||||||
|
select distinct
|
||||||
|
t1.mr_trty_code,
|
||||||
|
case when t1.bu_code in ('PC14','PC16','PC18','PC19') then 'BU'
|
||||||
|
when t1.bu_code = 'SC01' and t1.sub_bu_code = 'NA29' then sub_bu_name
|
||||||
|
when t1.bu_code = 'SC01' then 'BU'
|
||||||
|
when t1.bu_code <> 'SC01' and t2.bm_name = 'BCBH' then 'BU'
|
||||||
|
when t1.bu_code <> 'SC01' then t1.bu_name
|
||||||
|
end as channel
|
||||||
|
from dm.dm_td_org t1
|
||||||
|
left join dws.dws_td_bu_active t2
|
||||||
|
on t1.bu_name = t2.bu_name
|
||||||
|
where t1.yyyymm in (
|
||||||
|
select
|
||||||
|
key_value
|
||||||
|
from
|
||||||
|
dm.dm_td_date_config
|
||||||
|
where
|
||||||
|
key_code = 'pfmc_cvh'
|
||||||
|
)
|
||||||
|
)
|
||||||
|
--Family粒度和Brand粒度的映射表
|
||||||
|
,product_wide as (
|
||||||
|
select distinct
|
||||||
|
prd_family_cd,
|
||||||
|
prd_brand_cd
|
||||||
|
from cdw_dwd_dim_product_wide
|
||||||
|
)
|
||||||
|
--内部Brand粒度与外部pack_cod的映射表
|
||||||
|
,imscode_azcode as (
|
||||||
|
select distinct
|
||||||
|
brandcode,
|
||||||
|
case when length(ims_pack_cod) < 12 and ims_pack_cod REGEXP '^[0-9]' then right(concat('00000000000',ims_pack_cod),12) else ims_pack_cod end as ims_pack_cod
|
||||||
|
from dwd.dwd_td_imscode_azcode
|
||||||
|
)
|
||||||
|
--综合上面的几个表,得到KA_flag、target_ins_level、channel、目标机构Flag
|
||||||
|
,target_hp_all as (
|
||||||
|
select
|
||||||
|
distinct
|
||||||
|
t1.inst_code,
|
||||||
|
t1.ka_flag,
|
||||||
|
t1.target_ins_level,
|
||||||
|
t2.channel,
|
||||||
|
t5.MARKET
|
||||||
|
from
|
||||||
|
td_territory_inst t1
|
||||||
|
inner join td_org t2 on t1.trty_code = t2.mr_trty_code
|
||||||
|
left join product_wide t3 on t3.prd_family_cd = t1.family_code
|
||||||
|
left join imscode_azcode t4 on t3.prd_brand_cd = t4.brandcode
|
||||||
|
left join pack_market t5 on t4.ims_pack_cod = t5.pack_cod
|
||||||
|
where t5.MARKET is not null
|
||||||
|
)
|
||||||
|
--target_hp_all里一个机构一个市场对应了多个属性,这里按优先级依次取唯一值
|
||||||
|
--ka_flag
|
||||||
|
,ka_flag1 as (
|
||||||
|
select distinct
|
||||||
|
inst_code,
|
||||||
|
MARKET,
|
||||||
|
ka_flag
|
||||||
|
from target_hp_all
|
||||||
|
)
|
||||||
|
,ka_flag2 as (
|
||||||
|
select
|
||||||
|
inst_code,
|
||||||
|
MARKET,
|
||||||
|
ka_flag,
|
||||||
|
row_number() over(partition by inst_code,market
|
||||||
|
order by case when ka_flag = 'KA' then 1
|
||||||
|
ELSE 2 end asc) as RN
|
||||||
|
from ka_flag1
|
||||||
|
)
|
||||||
|
,ka_flag_final as (
|
||||||
|
select
|
||||||
|
inst_code,
|
||||||
|
MARKET,
|
||||||
|
ka_flag
|
||||||
|
from ka_flag2
|
||||||
|
where RN = 1
|
||||||
|
)
|
||||||
|
--target_ins_level不取唯一值
|
||||||
|
--加个hardcoding,ONC Market和ALL Market设置成NA
|
||||||
|
,target_ins_level1 as (
|
||||||
|
select distinct
|
||||||
|
inst_code,
|
||||||
|
MARKET,
|
||||||
|
case when MARKET in ('ALL Market','ONC Market') then 'NA'
|
||||||
|
else target_ins_level
|
||||||
|
end as target_ins_level
|
||||||
|
from target_hp_all
|
||||||
|
)
|
||||||
|
,target_ins_level_final as (
|
||||||
|
select distinct
|
||||||
|
inst_code,
|
||||||
|
MARKET,
|
||||||
|
target_ins_level
|
||||||
|
from target_ins_level1
|
||||||
|
)
|
||||||
|
--target_hp_all里一个机构一个市场对应了多个属性,这里按优先级依次取唯一值
|
||||||
|
--Channel
|
||||||
|
,Channel1 as (
|
||||||
|
select distinct
|
||||||
|
inst_code,
|
||||||
|
MARKET,
|
||||||
|
channel
|
||||||
|
from target_hp_all
|
||||||
|
)
|
||||||
|
,channel2 as (
|
||||||
|
select
|
||||||
|
inst_code,
|
||||||
|
MARKET,
|
||||||
|
channel,
|
||||||
|
row_number() over(partition by inst_code,market
|
||||||
|
order by case when channel = 'BU' then 1
|
||||||
|
when channel = 'BBU_County' then 2
|
||||||
|
when channel = 'CHC' then 3
|
||||||
|
when channel = 'Eagle' then 4
|
||||||
|
ELSE 5 end asc) as RN
|
||||||
|
from channel1
|
||||||
|
)
|
||||||
|
,channel_final as (
|
||||||
|
select
|
||||||
|
inst_code,
|
||||||
|
MARKET,
|
||||||
|
channel
|
||||||
|
from channel2
|
||||||
|
where RN = 1
|
||||||
|
)
|
||||||
|
--确定唯一值后重新放一起
|
||||||
|
,target_hp1 as (
|
||||||
|
select distinct
|
||||||
|
inst_code,
|
||||||
|
MARKET
|
||||||
|
from target_hp_all
|
||||||
|
)
|
||||||
|
,target_hp as (
|
||||||
|
select t1.inst_code,
|
||||||
|
t1.market,
|
||||||
|
t2.ka_flag,
|
||||||
|
t3.target_ins_level,
|
||||||
|
t4.channel
|
||||||
|
from target_hp1 t1
|
||||||
|
left join ka_flag_final t2
|
||||||
|
on t1.inst_code = t2.inst_code
|
||||||
|
and t1.market = t2.market
|
||||||
|
left join target_ins_level_final t3
|
||||||
|
on t1.inst_code = t3.inst_code
|
||||||
|
and t1.market = t3.market
|
||||||
|
left join channel_final t4
|
||||||
|
on t1.inst_code = t4.inst_code
|
||||||
|
and t1.market = t4.market
|
||||||
|
)
|
||||||
|
|
||||||
|
--获取Dragon_type
|
||||||
|
--判断Dragon_type是否为County
|
||||||
|
,county as (
|
||||||
|
select distinct inst_code,county_tier_desc
|
||||||
|
from dm.dm_td_institution
|
||||||
|
)
|
||||||
|
--判断另外2种
|
||||||
|
-- 20250825修改
|
||||||
|
,dragon_type as (
|
||||||
|
select src_hcc_code as inst_code,
|
||||||
|
src_prod_code as family_code,
|
||||||
|
max(is_core) is_core
|
||||||
|
from cdw_dwd_dwd_hospital_classification
|
||||||
|
where active_status = 1
|
||||||
|
and src_sales_cycle = (select key_value from dm.dm_td_date_config where key_code = 'pfmc_cvh')
|
||||||
|
group by src_hcc_code,
|
||||||
|
src_prod_code
|
||||||
|
)
|
||||||
|
--将Family_code转为pack_cod
|
||||||
|
,dragon_type_market1 as (
|
||||||
|
select distinct
|
||||||
|
t1.inst_code,
|
||||||
|
t1.is_core,
|
||||||
|
t4.market
|
||||||
|
from dragon_type t1
|
||||||
|
left join cdw_dwd_dim_product_wide t2
|
||||||
|
on t1.family_code = t2.prd_family_cd
|
||||||
|
left join dwd.dwd_td_imscode_azcode t3
|
||||||
|
on t2.prd_brand_cd = t3.brandcode
|
||||||
|
left join pack_market t4
|
||||||
|
on t3.ims_pack_cod = t4.pack_cod
|
||||||
|
where t4.market is not null
|
||||||
|
)
|
||||||
|
--按优先级取唯一值
|
||||||
|
,dragon_type_market2 as (
|
||||||
|
select distinct
|
||||||
|
inst_code,
|
||||||
|
is_core,
|
||||||
|
market,
|
||||||
|
row_number() over(partition by inst_code,market
|
||||||
|
order by case when is_core = 'CORE' then 1
|
||||||
|
when is_core = 'EMERGING' then 2
|
||||||
|
else 3 end asc) as RN
|
||||||
|
from dragon_type_market1
|
||||||
|
)
|
||||||
|
,dragon_type_market as (
|
||||||
|
select distinct
|
||||||
|
inst_code,
|
||||||
|
is_core,
|
||||||
|
market
|
||||||
|
from dragon_type_market2
|
||||||
|
where RN = 1
|
||||||
|
)
|
||||||
|
--综合前面几个表,得出dragon_flag
|
||||||
|
,dragon_flag1 as (
|
||||||
|
select distinct
|
||||||
|
t0.inst_code,
|
||||||
|
t0.market,
|
||||||
|
case when t1.county_tier_desc in ('County','County level city') then 'County'
|
||||||
|
when t2.is_core is null or length(t2.is_core) = 0 then 'Others'
|
||||||
|
else t2.is_core
|
||||||
|
end as dragon_flag
|
||||||
|
from target_hp t0
|
||||||
|
left join county t1
|
||||||
|
on t0.inst_code = t1.inst_code
|
||||||
|
left join dragon_type_market t2
|
||||||
|
on t0.inst_code = t2.inst_code
|
||||||
|
and t0.MARKET = t2.MARKET
|
||||||
|
)
|
||||||
|
--按优先级取唯一值
|
||||||
|
,dragon_flag2 as (
|
||||||
|
select inst_code,
|
||||||
|
market,
|
||||||
|
dragon_flag,
|
||||||
|
row_number() over(partition by inst_code,market
|
||||||
|
order by case when dragon_flag = 'CORE' then 1
|
||||||
|
when dragon_flag = 'EMERGING' then 2
|
||||||
|
when dragon_flag = 'County' then 3
|
||||||
|
else 4 end asc) as RN
|
||||||
|
from dragon_flag1
|
||||||
|
)
|
||||||
|
,dragon_flag as (
|
||||||
|
select inst_code,
|
||||||
|
market,
|
||||||
|
dragon_flag
|
||||||
|
from dragon_flag2
|
||||||
|
where RN = 1
|
||||||
|
)
|
||||||
|
|
||||||
|
--下面是为了把其他渠道并进来
|
||||||
|
,other_data_source as (
|
||||||
|
select explode(array('IQVIA-CHPA(Monthly)','XH Data(Quarterly)','EC(Monthly)','Retail(Quarterly)','CHC(Quarterly)','THC(Quarterly)','IQVIA-COUNTY(Quarterly)'))
|
||||||
|
)
|
||||||
|
,col as (
|
||||||
|
select '' as inst_code,
|
||||||
|
'' as MARKET,
|
||||||
|
'' as is_target_hp,
|
||||||
|
'' as sub_channel,
|
||||||
|
'' as dragon_flag,
|
||||||
|
'' as ka_flag,
|
||||||
|
'' as target_ins_level
|
||||||
|
)
|
||||||
|
|
||||||
|
,temp_1 as (
|
||||||
|
select distinct
|
||||||
|
case when t1.inst_code is null or t1.inst_code = '' then 'ROC'
|
||||||
|
else t1.inst_code
|
||||||
|
end as inst_code,
|
||||||
|
t1.MARKET,
|
||||||
|
case when t2.inst_code is null then 'N' else 'Y' end as is_target_hp,
|
||||||
|
case when t2.channel = 'BU' and geo.county_tier_desc in ('County','County level city') then 'BU_County'
|
||||||
|
when t2.channel = 'BU' and geo.county_tier_desc in ('City-3b','City-4a','City-4b') then 'BU_Emerging'
|
||||||
|
when t2.channel = 'BU' and t3.dragon_flag = 'CORE' then 'BU_Core'
|
||||||
|
when t2.channel = 'BU' then 'BU_Emerging'
|
||||||
|
when t2.channel in ('Eagle','CHC','BBU_County') then t2.channel
|
||||||
|
when t2.inst_code is null then 'NON_TARGET'
|
||||||
|
else 'NA'
|
||||||
|
end as sub_channel,
|
||||||
|
case when t3.dragon_flag is null then 'NON_TARGET'
|
||||||
|
else t3.dragon_flag
|
||||||
|
end as dragon_flag,
|
||||||
|
case when t2.ka_flag is null then 'NON_TARGET'
|
||||||
|
else t2.ka_flag
|
||||||
|
end as ka_flag,
|
||||||
|
case when t2.target_ins_level is null then 'NON_TARGET'
|
||||||
|
else t2.target_ins_level
|
||||||
|
end as target_ins_level,
|
||||||
|
'AIA(Monthly)' as DATA_SOURCE
|
||||||
|
from sales t1
|
||||||
|
left join target_hp t2
|
||||||
|
on t1.inst_code = t2.inst_code
|
||||||
|
and t1.MARKET = t2.MARKET
|
||||||
|
left join dragon_flag t3
|
||||||
|
on t1.inst_code = t3.inst_code
|
||||||
|
and t1.MARKET = t3.MARKET
|
||||||
|
left join dm.dm_td_institution inst
|
||||||
|
on t1.inst_code =inst.inst_code
|
||||||
|
left join dm.dm_td_geography geo
|
||||||
|
on inst.county_code = geo.geo_key
|
||||||
|
union all
|
||||||
|
select *
|
||||||
|
from col
|
||||||
|
cross join other_data_source
|
||||||
|
)
|
||||||
|
,sub_channel_rank as (
|
||||||
|
select distinct SUB_CHANNELSTD,SUB_CHANNEL_ORDER
|
||||||
|
from dm.dm_td_sd_channel
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
select t1.*,NVL(t2.SUB_CHANNEL_ORDER,100) as SUB_CHANNEL_ORDER
|
||||||
|
from temp_1 t1
|
||||||
|
left join sub_channel_rank t2
|
||||||
|
on t1.sub_channel = t2.SUB_CHANNELSTD
|
||||||
BIN
EXTERNAL/AIA/11 dm_aia_targethp_flag.sql:Zone.Identifier
Normal file
BIN
EXTERNAL/AIA/11 dm_aia_targethp_flag.sql:Zone.Identifier
Normal file
Binary file not shown.
643
EXTERNAL/AIA/z_03 dm_aia_flag_备份用.sql
Normal file
643
EXTERNAL/AIA/z_03 dm_aia_flag_备份用.sql
Normal file
@@ -0,0 +1,643 @@
|
|||||||
|
-- Databricks notebook source
|
||||||
|
-- MAGIC %run ../../../Common/config
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
-- MAGIC %python
|
||||||
|
-- MAGIC spark.read.table(f'`{CDW_CATALOG}`.`dwd`.`dim_product_wide`').createOrReplaceTempView('cdw_dwd_dim_product_wide')
|
||||||
|
-- MAGIC spark.read.table(f'`{CDW_CATALOG}`.`dwd`.`dwd_hospital_classification`').createOrReplaceTempView('cdw_dwd_dwd_hospital_classification')
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
-----------------------------------------------------------------------------------
|
||||||
|
--修改时间:20241021
|
||||||
|
--修改人:FanXujia
|
||||||
|
--修改内容:
|
||||||
|
--只取最新年-1 ~ 最新年范围内的,称为“2年共有”。增加
|
||||||
|
-----------------------------------------------------------------------------------
|
||||||
|
--共有机构
|
||||||
|
insert overwrite table dm.dm_aia_hp_flag
|
||||||
|
with max_year as (
|
||||||
|
select
|
||||||
|
max(cast(year as int)) as max_year,
|
||||||
|
max(cast(year as int) - 1) as max_year_ly
|
||||||
|
from dwd.dwd_gnd_hospital_not_provided
|
||||||
|
)
|
||||||
|
,hp_flag as (
|
||||||
|
select
|
||||||
|
distinct concat(t1.year, '-Y') as hp_his_flag,
|
||||||
|
case when length(t1.cpa_hospital_code) < 7 then right(concat('0000000',t1.cpa_hospital_code),7) else t1.cpa_hospital_code end as cpa_hospital_code
|
||||||
|
from
|
||||||
|
dwd.dwd_gnd_hospital_not_provided t1
|
||||||
|
cross join max_year t2
|
||||||
|
where cast(t1.year as int) >= t2.max_year_ly
|
||||||
|
)
|
||||||
|
,hp_flag2 as (
|
||||||
|
SELECT
|
||||||
|
cpa_hospital_code,
|
||||||
|
CONCAT_WS(' ∩ ', SORT_ARRAY(COLLECT_LIST(hp_his_flag))) AS FLAG
|
||||||
|
FROM
|
||||||
|
hp_flag
|
||||||
|
GROUP BY
|
||||||
|
cpa_hospital_code
|
||||||
|
)
|
||||||
|
,inst_mapping as (
|
||||||
|
-- select
|
||||||
|
-- distinct right(concat('0000000', org_cd), 7) org_cd,
|
||||||
|
-- ins_cd
|
||||||
|
-- from
|
||||||
|
-- dwd.dwd_gnd_hospitalmapping
|
||||||
|
select distinct
|
||||||
|
case when length(cpa_hospital_code) < 7 then right(concat('0000000',cpa_hospital_code),7) else cpa_hospital_code end as cpa_hospital_code,
|
||||||
|
ins_cd_nl as inst_code
|
||||||
|
from dwd.dwd_gnd_hospital_not_provided
|
||||||
|
)
|
||||||
|
|
||||||
|
,ALL_INS as (
|
||||||
|
select distinct inst_code
|
||||||
|
from dm.dm_ext_aia_sales
|
||||||
|
)
|
||||||
|
|
||||||
|
,flag as (
|
||||||
|
select distinct
|
||||||
|
coalesce(t2.inst_code,t1.cpa_hospital_code) as inst_code,
|
||||||
|
t1.flag as aia_hp_flag,
|
||||||
|
'Y' as FLAG,
|
||||||
|
'AIA(Monthly)' as DATA_SOURCE
|
||||||
|
from hp_flag2 t1
|
||||||
|
left join inst_mapping t2 on t1.cpa_hospital_code = t2.cpa_hospital_code
|
||||||
|
where
|
||||||
|
length(t1.flag) > 6
|
||||||
|
union all
|
||||||
|
select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'IQVIA-CHPA(Monthly)' as DATA_SOURCE
|
||||||
|
union all
|
||||||
|
select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'XH Data(Quarterly)' as DATA_SOURCE
|
||||||
|
union all
|
||||||
|
select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'EC(Monthly)' as DATA_SOURCE
|
||||||
|
union all
|
||||||
|
select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'Retail(Quarterly)' as DATA_SOURCE
|
||||||
|
union all
|
||||||
|
select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'CHC(Quarterly)' as DATA_SOURCE
|
||||||
|
union all
|
||||||
|
select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'THC(Quarterly)' as DATA_SOURCE
|
||||||
|
union all
|
||||||
|
select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'IQVIA-COUNTY(Quarterly)' as DATA_SOURCE
|
||||||
|
)
|
||||||
|
|
||||||
|
select t1.inst_code,
|
||||||
|
NVL(t2.aia_hp_flag,'') as aia_hp_flag,
|
||||||
|
NVL(t2.FLAG,'N') as FLAG,
|
||||||
|
'AIA(Monthly)' as DATA_SOURCE
|
||||||
|
from ALL_INS t1
|
||||||
|
left join flag t2
|
||||||
|
on t1.inst_code = t2.inst_code
|
||||||
|
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
-----------------------------------------------------------------------------------
|
||||||
|
--修改时间:20241021
|
||||||
|
--修改人:FanXujia
|
||||||
|
--修改内容:
|
||||||
|
--只取最新年-1 ~ 最新年范围内的,称为“2年共有”。增加
|
||||||
|
-----------------------------------------------------------------------------------
|
||||||
|
--共有机构
|
||||||
|
insert overwrite table dm.dm_aia_hp_flag
|
||||||
|
with max_year as (
|
||||||
|
select
|
||||||
|
max(cast(year as int)) as max_year,
|
||||||
|
max(cast(year as int) - 1) as max_year_ly
|
||||||
|
from dwd.dwd_gnd_hospital_not_provided
|
||||||
|
)
|
||||||
|
,hp_flag as (
|
||||||
|
select
|
||||||
|
distinct concat(t1.year, '-Y') as hp_his_flag,
|
||||||
|
case when length(t1.cpa_hospital_code) < 7 then right(concat('0000000',t1.cpa_hospital_code),7) else t1.cpa_hospital_code end as cpa_hospital_code
|
||||||
|
from
|
||||||
|
dwd.dwd_gnd_hospital_not_provided t1
|
||||||
|
cross join max_year t2
|
||||||
|
where cast(t1.year as int) >= t2.max_year_ly
|
||||||
|
)
|
||||||
|
,hp_flag2 as (
|
||||||
|
SELECT
|
||||||
|
cpa_hospital_code,
|
||||||
|
CONCAT_WS(' ∩ ', SORT_ARRAY(COLLECT_LIST(hp_his_flag))) AS FLAG
|
||||||
|
FROM
|
||||||
|
hp_flag
|
||||||
|
GROUP BY
|
||||||
|
cpa_hospital_code
|
||||||
|
)
|
||||||
|
,inst_mapping as (
|
||||||
|
-- select
|
||||||
|
-- distinct right(concat('0000000', org_cd), 7) org_cd,
|
||||||
|
-- ins_cd
|
||||||
|
-- from
|
||||||
|
-- dwd.dwd_gnd_hospitalmapping
|
||||||
|
select distinct
|
||||||
|
case when length(cpa_hospital_code) < 7 then right(concat('0000000',cpa_hospital_code),7) else cpa_hospital_code end as cpa_hospital_code,
|
||||||
|
ins_cd_nl as inst_code
|
||||||
|
from dwd.dwd_gnd_hospital_not_provided
|
||||||
|
)
|
||||||
|
|
||||||
|
,ALL_INS as (
|
||||||
|
select distinct inst_code
|
||||||
|
from dm.dm_ext_aia_sales
|
||||||
|
)
|
||||||
|
|
||||||
|
,flag as (
|
||||||
|
select distinct
|
||||||
|
coalesce(t2.inst_code,t1.cpa_hospital_code) as inst_code,
|
||||||
|
t1.flag as aia_hp_flag,
|
||||||
|
'Y' as FLAG,
|
||||||
|
'AIA(Monthly)' as DATA_SOURCE
|
||||||
|
from hp_flag2 t1
|
||||||
|
left join inst_mapping t2 on t1.cpa_hospital_code = t2.cpa_hospital_code
|
||||||
|
where
|
||||||
|
length(t1.flag) > 6
|
||||||
|
union all
|
||||||
|
select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'IQVIA-CHPA(Monthly)' as DATA_SOURCE
|
||||||
|
union all
|
||||||
|
select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'XH Data(Quarterly)' as DATA_SOURCE
|
||||||
|
union all
|
||||||
|
select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'EC(Monthly)' as DATA_SOURCE
|
||||||
|
union all
|
||||||
|
select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'Retail(Quarterly)' as DATA_SOURCE
|
||||||
|
union all
|
||||||
|
select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'CHC(Quarterly)' as DATA_SOURCE
|
||||||
|
union all
|
||||||
|
select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'THC(Quarterly)' as DATA_SOURCE
|
||||||
|
union all
|
||||||
|
select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'IQVIA-COUNTY(Quarterly)' as DATA_SOURCE
|
||||||
|
)
|
||||||
|
|
||||||
|
select t1.inst_code,
|
||||||
|
NVL(t2.aia_hp_flag,'') as aia_hp_flag,
|
||||||
|
NVL(t2.FLAG,'N') as FLAG,
|
||||||
|
'AIA(Monthly)' as DATA_SOURCE
|
||||||
|
from ALL_INS t1
|
||||||
|
left join flag t2
|
||||||
|
on t1.inst_code = t2.inst_code
|
||||||
|
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
--连续取数机构
|
||||||
|
----------------------------------------------------------------------------------------------------------------------
|
||||||
|
--修改时间:20241029
|
||||||
|
--修改人:FanXujia
|
||||||
|
--修改内容:
|
||||||
|
--根据事实表最新月,往前推12个月(含最新月),按机构 + 年月粒度汇总,如果每个月销量都不等于0,这家机构称为:滚动一年有数
|
||||||
|
--根据事实表最新月,往前推24个月(含最新月),按机构 + 年月粒度汇总,如果每个月销量都不等于0,这家机构称为:滚动两年有数
|
||||||
|
--2022年没有销量为正常情况,判断时仅考虑2023年01月起销量是否等于0
|
||||||
|
----------------------------------------------------------------------------------------------------------------------
|
||||||
|
insert overwrite table dm.dm_aia_provided_flag
|
||||||
|
with max_ym as (
|
||||||
|
select max(ym) as max_ym
|
||||||
|
from dm.dm_ext_aia_sales
|
||||||
|
)
|
||||||
|
,ym_range as (
|
||||||
|
select max_ym,
|
||||||
|
case when max_ym <= '202412' then cast(right(max_ym,2) as int)
|
||||||
|
else cast(12 as int)
|
||||||
|
end ym_12m,
|
||||||
|
case when max_ym <= '202312' then '202401'
|
||||||
|
when right(max_ym,2) = '12' then concat(left(max_ym,4),'01')
|
||||||
|
else concat(cast(left(max_ym, 4) - 1 as int),right(concat('0', cast(right(max_ym, 2) + 1 as int)), 2))
|
||||||
|
end as ym_12m_before,
|
||||||
|
case when max_ym <= '202312' then cast(right(max_ym,2) as int)
|
||||||
|
when max_ym <= '202512' then cast(right(max_ym,2) as int) + 12
|
||||||
|
else cast(24 as int)
|
||||||
|
end as ym_24m,
|
||||||
|
case when max_ym <= '202512' then '202401'
|
||||||
|
when right(max_ym,2) = '12' then concat(cast(left(max_ym,4) - 1 as int),'01')
|
||||||
|
else concat(cast(left(max_ym, 4) -2 as int),right(concat('0', cast(right(max_ym, 2) + 1 as int)), 2))
|
||||||
|
end as ym_24m_before
|
||||||
|
from max_ym
|
||||||
|
)
|
||||||
|
,sales_1 as (
|
||||||
|
select
|
||||||
|
inst_code,
|
||||||
|
ym
|
||||||
|
from
|
||||||
|
dm.dm_ext_aia_sales
|
||||||
|
group by
|
||||||
|
ym,
|
||||||
|
inst_code
|
||||||
|
having
|
||||||
|
sum(value) <> 0
|
||||||
|
)
|
||||||
|
,sales_2 as (
|
||||||
|
select
|
||||||
|
t1.inst_code,
|
||||||
|
t1.ym,
|
||||||
|
t2.max_ym,
|
||||||
|
t2.ym_12m,
|
||||||
|
t2.ym_12m_before,
|
||||||
|
t2.ym_24m,
|
||||||
|
t2.ym_24m_before
|
||||||
|
from sales_1 t1
|
||||||
|
cross join ym_range t2
|
||||||
|
)
|
||||||
|
,flag_12m as (
|
||||||
|
select inst_code,
|
||||||
|
'滚动一年有数' as aia_provided_flag,
|
||||||
|
'AIA(Monthly)' as DATA_SOURCE
|
||||||
|
from sales_2
|
||||||
|
where ym <= max_ym
|
||||||
|
and ym >= ym_12m_before
|
||||||
|
group by inst_code
|
||||||
|
having count(1) = (select ym_12m from ym_range)
|
||||||
|
)
|
||||||
|
,flag_24m as (
|
||||||
|
select inst_code,
|
||||||
|
'滚动两年有数' as aia_provided_flag,
|
||||||
|
'AIA(Monthly)' as DATA_SOURCE
|
||||||
|
from sales_2
|
||||||
|
where ym <= max_ym
|
||||||
|
and ym >= ym_24m_before
|
||||||
|
group by inst_code
|
||||||
|
having count(1) = (select ym_24m from ym_range)
|
||||||
|
)
|
||||||
|
|
||||||
|
select inst_code,
|
||||||
|
aia_provided_flag,
|
||||||
|
DATA_SOURCE
|
||||||
|
from flag_12m
|
||||||
|
union all
|
||||||
|
select inst_code,
|
||||||
|
aia_provided_flag,
|
||||||
|
DATA_SOURCE
|
||||||
|
from flag_24m
|
||||||
|
union all
|
||||||
|
select '' as inst_code,'' as aia_provided_flag,'IQVIA-CHPA(Monthly)' as data_source
|
||||||
|
union all
|
||||||
|
select '' as inst_code,'' as aia_provided_flag,'XH Data(Quarterly)' as data_source
|
||||||
|
union all
|
||||||
|
select '' as inst_code,'' as aia_provided_flag,'EC(Monthly)' as data_source
|
||||||
|
union all
|
||||||
|
select '' as inst_code,'' as aia_provided_flag,'Retail(Quarterly)' as data_source
|
||||||
|
union all
|
||||||
|
select '' as inst_code,'' as aia_provided_flag,'CHC(Quarterly)' as data_source
|
||||||
|
union all
|
||||||
|
select '' as inst_code,'' as aia_provided_flag,'THC(Quarterly)' as data_source
|
||||||
|
union all
|
||||||
|
select '' as inst_code,'' as aia_provided_flag,'IQVIA-COUNTY(Quarterly)' as data_source
|
||||||
|
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
---------------------------------------------------------------------------
|
||||||
|
--修改时间:20241108
|
||||||
|
--修改人:Fanxujia
|
||||||
|
--修改内容:
|
||||||
|
--for AIA Dashboard
|
||||||
|
--增加sub_channel,dragon_flag,ka_flag,target_ins_level字段
|
||||||
|
---------------------------------------------------------------------------
|
||||||
|
---------------------------------------------------------------------------
|
||||||
|
--修改时间:20241209
|
||||||
|
--修改人:Fanxujia
|
||||||
|
--修改内容:
|
||||||
|
--增加sub channel的排序
|
||||||
|
---------------------------------------------------------------------------
|
||||||
|
--目标医院
|
||||||
|
insert overwrite table dm.dm_aia_targethp_flag --获取全部的机构 + 市场
|
||||||
|
with sales as (
|
||||||
|
select distinct t1.inst_code,t2.MARKET
|
||||||
|
from dm.dm_ext_aia_sales t1
|
||||||
|
left join dm.dm_aia_pack_property t2
|
||||||
|
on t1.pack_cod = t2.PACK_COD
|
||||||
|
)
|
||||||
|
--获取产品与市场的映射关系
|
||||||
|
,pack_market as (
|
||||||
|
select distinct
|
||||||
|
PACK_COD,
|
||||||
|
MARKET
|
||||||
|
from dm.dm_aia_pack_property
|
||||||
|
)
|
||||||
|
--辖区表,获取到KA_Flag、target_ins_level以及目标机构Flag(在辖区表里存在的就是目标机构)
|
||||||
|
,td_territory_inst as (
|
||||||
|
select distinct
|
||||||
|
inst_code,
|
||||||
|
trty_code,
|
||||||
|
family_code,
|
||||||
|
ka_flag,
|
||||||
|
TARGET_LEVEL as target_ins_level
|
||||||
|
from dm.dm_td_sd_territory_indication --eagle的非重点医院同样加为目标机构
|
||||||
|
where yyyymm in (
|
||||||
|
select
|
||||||
|
key_value
|
||||||
|
from
|
||||||
|
dm.dm_td_date_config
|
||||||
|
where
|
||||||
|
key_code = 'pfmc_cvh'
|
||||||
|
)
|
||||||
|
and bu not in ('CUBE','KA')
|
||||||
|
and key_hp<>3 --chenwu 20250904 排除双考影响
|
||||||
|
)
|
||||||
|
--汇报线表,获取到Channel,后续用于判断出sub_channel
|
||||||
|
,td_org as (
|
||||||
|
select distinct
|
||||||
|
t1.mr_trty_code,
|
||||||
|
case when t1.bu_code in ('PC14','PC16','PC18','PC19') then 'BU'
|
||||||
|
when t1.bu_code = 'SC01' and t1.sub_bu_code = 'NA29' then sub_bu_name
|
||||||
|
when t1.bu_code = 'SC01' then 'BU'
|
||||||
|
when t1.bu_code <> 'SC01' and t2.bm_name = 'BCBH' then 'BU'
|
||||||
|
when t1.bu_code <> 'SC01' then t1.bu_name
|
||||||
|
end as channel
|
||||||
|
from dm.dm_td_org t1
|
||||||
|
left join dws.dws_td_bu_active t2
|
||||||
|
on t1.bu_name = t2.bu_name
|
||||||
|
where t1.yyyymm in (
|
||||||
|
select
|
||||||
|
key_value
|
||||||
|
from
|
||||||
|
dm.dm_td_date_config
|
||||||
|
where
|
||||||
|
key_code = 'pfmc_cvh'
|
||||||
|
)
|
||||||
|
)
|
||||||
|
--Family粒度和Brand粒度的映射表
|
||||||
|
,product_wide as (
|
||||||
|
select distinct
|
||||||
|
prd_family_cd,
|
||||||
|
prd_brand_cd
|
||||||
|
from cdw_dwd_dim_product_wide
|
||||||
|
)
|
||||||
|
--内部Brand粒度与外部pack_cod的映射表
|
||||||
|
,imscode_azcode as (
|
||||||
|
select distinct
|
||||||
|
brandcode,
|
||||||
|
case when length(ims_pack_cod) < 12 and ims_pack_cod REGEXP '^[0-9]' then right(concat('00000000000',ims_pack_cod),12) else ims_pack_cod end as ims_pack_cod
|
||||||
|
from dwd.dwd_td_imscode_azcode
|
||||||
|
)
|
||||||
|
--综合上面的几个表,得到KA_flag、target_ins_level、channel、目标机构Flag
|
||||||
|
,target_hp_all as (
|
||||||
|
select
|
||||||
|
distinct
|
||||||
|
t1.inst_code,
|
||||||
|
t1.ka_flag,
|
||||||
|
t1.target_ins_level,
|
||||||
|
t2.channel,
|
||||||
|
t5.MARKET
|
||||||
|
from
|
||||||
|
td_territory_inst t1
|
||||||
|
inner join td_org t2 on t1.trty_code = t2.mr_trty_code
|
||||||
|
left join product_wide t3 on t3.prd_family_cd = t1.family_code
|
||||||
|
left join imscode_azcode t4 on t3.prd_brand_cd = t4.brandcode
|
||||||
|
left join pack_market t5 on t4.ims_pack_cod = t5.pack_cod
|
||||||
|
where t5.MARKET is not null
|
||||||
|
)
|
||||||
|
--target_hp_all里一个机构一个市场对应了多个属性,这里按优先级依次取唯一值
|
||||||
|
--ka_flag
|
||||||
|
,ka_flag1 as (
|
||||||
|
select distinct
|
||||||
|
inst_code,
|
||||||
|
MARKET,
|
||||||
|
ka_flag
|
||||||
|
from target_hp_all
|
||||||
|
)
|
||||||
|
,ka_flag2 as (
|
||||||
|
select
|
||||||
|
inst_code,
|
||||||
|
MARKET,
|
||||||
|
ka_flag,
|
||||||
|
row_number() over(partition by inst_code,market
|
||||||
|
order by case when ka_flag = 'KA' then 1
|
||||||
|
ELSE 2 end asc) as RN
|
||||||
|
from ka_flag1
|
||||||
|
)
|
||||||
|
,ka_flag_final as (
|
||||||
|
select
|
||||||
|
inst_code,
|
||||||
|
MARKET,
|
||||||
|
ka_flag
|
||||||
|
from ka_flag2
|
||||||
|
where RN = 1
|
||||||
|
)
|
||||||
|
--target_ins_level不取唯一值
|
||||||
|
--加个hardcoding,ONC Market和ALL Market设置成NA
|
||||||
|
,target_ins_level1 as (
|
||||||
|
select distinct
|
||||||
|
inst_code,
|
||||||
|
MARKET,
|
||||||
|
case when MARKET in ('AIA ALL Market','ONC Market') then 'NA'
|
||||||
|
else target_ins_level
|
||||||
|
end as target_ins_level
|
||||||
|
from target_hp_all
|
||||||
|
)
|
||||||
|
,target_ins_level_final as (
|
||||||
|
select distinct
|
||||||
|
inst_code,
|
||||||
|
MARKET,
|
||||||
|
target_ins_level
|
||||||
|
from target_ins_level1
|
||||||
|
)
|
||||||
|
--target_hp_all里一个机构一个市场对应了多个属性,这里按优先级依次取唯一值
|
||||||
|
--Channel
|
||||||
|
,Channel1 as (
|
||||||
|
select distinct
|
||||||
|
inst_code,
|
||||||
|
MARKET,
|
||||||
|
channel
|
||||||
|
from target_hp_all
|
||||||
|
)
|
||||||
|
,channel2 as (
|
||||||
|
select
|
||||||
|
inst_code,
|
||||||
|
MARKET,
|
||||||
|
channel,
|
||||||
|
row_number() over(partition by inst_code,market
|
||||||
|
order by case when channel = 'BU' then 1
|
||||||
|
when channel = 'BBU_County' then 2
|
||||||
|
when channel = 'CHC' then 3
|
||||||
|
when channel = 'Eagle' then 4
|
||||||
|
ELSE 5 end asc) as RN
|
||||||
|
from channel1
|
||||||
|
)
|
||||||
|
,channel_final as (
|
||||||
|
select
|
||||||
|
inst_code,
|
||||||
|
MARKET,
|
||||||
|
channel
|
||||||
|
from channel2
|
||||||
|
where RN = 1
|
||||||
|
)
|
||||||
|
--确定唯一值后重新放一起
|
||||||
|
,target_hp1 as (
|
||||||
|
select distinct
|
||||||
|
inst_code,
|
||||||
|
MARKET
|
||||||
|
from target_hp_all
|
||||||
|
)
|
||||||
|
,target_hp as (
|
||||||
|
select t1.inst_code,
|
||||||
|
t1.market,
|
||||||
|
t2.ka_flag,
|
||||||
|
t3.target_ins_level,
|
||||||
|
t4.channel
|
||||||
|
from target_hp1 t1
|
||||||
|
left join ka_flag_final t2
|
||||||
|
on t1.inst_code = t2.inst_code
|
||||||
|
and t1.market = t2.market
|
||||||
|
left join target_ins_level_final t3
|
||||||
|
on t1.inst_code = t3.inst_code
|
||||||
|
and t1.market = t3.market
|
||||||
|
left join channel_final t4
|
||||||
|
on t1.inst_code = t4.inst_code
|
||||||
|
and t1.market = t4.market
|
||||||
|
)
|
||||||
|
|
||||||
|
--获取Dragon_type
|
||||||
|
--判断Dragon_type是否为County
|
||||||
|
,county as (
|
||||||
|
select distinct inst_code,county_tier_desc
|
||||||
|
from dm.dm_td_institution
|
||||||
|
)
|
||||||
|
--判断另外2种
|
||||||
|
-- 20250825修改
|
||||||
|
,dragon_type as (
|
||||||
|
select src_hcc_code as inst_code,
|
||||||
|
src_prod_code as family_code,
|
||||||
|
max(is_core) is_core
|
||||||
|
from cdw_dwd_dwd_hospital_classification
|
||||||
|
where active_status = 1
|
||||||
|
and src_sales_cycle = (select key_value from dm.dm_td_date_config where key_code = 'pfmc_cvh')
|
||||||
|
group by src_hcc_code,
|
||||||
|
src_prod_code
|
||||||
|
)
|
||||||
|
--将Family_code转为pack_cod
|
||||||
|
,dragon_type_market1 as (
|
||||||
|
select distinct
|
||||||
|
t1.inst_code,
|
||||||
|
t1.is_core,
|
||||||
|
t4.market
|
||||||
|
from dragon_type t1
|
||||||
|
left join cdw_dwd_dim_product_wide t2
|
||||||
|
on t1.family_code = t2.prd_family_cd
|
||||||
|
left join dwd.dwd_td_imscode_azcode t3
|
||||||
|
on t2.prd_brand_cd = t3.brandcode
|
||||||
|
left join pack_market t4
|
||||||
|
on t3.ims_pack_cod = t4.pack_cod
|
||||||
|
where t4.market is not null
|
||||||
|
)
|
||||||
|
--按优先级取唯一值
|
||||||
|
,dragon_type_market2 as (
|
||||||
|
select distinct
|
||||||
|
inst_code,
|
||||||
|
is_core,
|
||||||
|
market,
|
||||||
|
row_number() over(partition by inst_code,market
|
||||||
|
order by case when is_core = 'CORE' then 1
|
||||||
|
when is_core = 'EMERGING' then 2
|
||||||
|
else 3 end asc) as RN
|
||||||
|
from dragon_type_market1
|
||||||
|
)
|
||||||
|
,dragon_type_market as (
|
||||||
|
select distinct
|
||||||
|
inst_code,
|
||||||
|
is_core,
|
||||||
|
market
|
||||||
|
from dragon_type_market2
|
||||||
|
where RN = 1
|
||||||
|
)
|
||||||
|
--综合前面几个表,得出dragon_flag
|
||||||
|
,dragon_flag1 as (
|
||||||
|
select distinct
|
||||||
|
t0.inst_code,
|
||||||
|
t0.market,
|
||||||
|
case when t1.county_tier_desc in ('County','County level city') then 'County'
|
||||||
|
when t2.is_core is null or length(t2.is_core) = 0 then 'Others'
|
||||||
|
else t2.is_core
|
||||||
|
end as dragon_flag
|
||||||
|
from target_hp t0
|
||||||
|
left join county t1
|
||||||
|
on t0.inst_code = t1.inst_code
|
||||||
|
left join dragon_type_market t2
|
||||||
|
on t0.inst_code = t2.inst_code
|
||||||
|
and t0.MARKET = t2.MARKET
|
||||||
|
)
|
||||||
|
--按优先级取唯一值
|
||||||
|
,dragon_flag2 as (
|
||||||
|
select inst_code,
|
||||||
|
market,
|
||||||
|
dragon_flag,
|
||||||
|
row_number() over(partition by inst_code,market
|
||||||
|
order by case when dragon_flag = 'CORE' then 1
|
||||||
|
when dragon_flag = 'EMERGING' then 2
|
||||||
|
when dragon_flag = 'County' then 3
|
||||||
|
else 4 end asc) as RN
|
||||||
|
from dragon_flag1
|
||||||
|
)
|
||||||
|
,dragon_flag as (
|
||||||
|
select inst_code,
|
||||||
|
market,
|
||||||
|
dragon_flag
|
||||||
|
from dragon_flag2
|
||||||
|
where RN = 1
|
||||||
|
)
|
||||||
|
|
||||||
|
--下面是为了把其他渠道并进来
|
||||||
|
,other_data_source as (
|
||||||
|
select explode(array('IQVIA-CHPA(Monthly)','XH Data(Quarterly)','EC(Monthly)','Retail(Quarterly)','CHC(Quarterly)','THC(Quarterly)','IQVIA-COUNTY(Quarterly)'))
|
||||||
|
)
|
||||||
|
,col as (
|
||||||
|
select '' as inst_code,
|
||||||
|
'' as MARKET,
|
||||||
|
'' as is_target_hp,
|
||||||
|
'' as sub_channel,
|
||||||
|
'' as dragon_flag,
|
||||||
|
'' as ka_flag,
|
||||||
|
'' as target_ins_level
|
||||||
|
)
|
||||||
|
|
||||||
|
,temp_1 as (
|
||||||
|
select distinct
|
||||||
|
case when t1.inst_code is null or t1.inst_code = '' then 'ROC'
|
||||||
|
else t1.inst_code
|
||||||
|
end as inst_code,
|
||||||
|
t1.MARKET,
|
||||||
|
case when t2.inst_code is null then 'N' else 'Y' end as is_target_hp,
|
||||||
|
case when t2.channel = 'BU' and geo.county_tier_desc in ('County','County level city') then 'BU_County'
|
||||||
|
when t2.channel = 'BU' and geo.county_tier_desc in ('City-3b','City-4a','City-4b') then 'BU_Emerging'
|
||||||
|
when t2.channel = 'BU' and t3.dragon_flag = 'CORE' then 'BU_Core'
|
||||||
|
when t2.channel = 'BU' then 'BU_Emerging'
|
||||||
|
when t2.channel in ('Eagle','CHC','BBU_County') then t2.channel
|
||||||
|
when t2.inst_code is null then 'NON_TARGET'
|
||||||
|
else 'NA'
|
||||||
|
end as sub_channel,
|
||||||
|
case when t3.dragon_flag is null then 'NON_TARGET'
|
||||||
|
else t3.dragon_flag
|
||||||
|
end as dragon_flag,
|
||||||
|
case when t2.ka_flag is null then 'NON_TARGET'
|
||||||
|
else t2.ka_flag
|
||||||
|
end as ka_flag,
|
||||||
|
case when t2.target_ins_level is null then 'NON_TARGET'
|
||||||
|
else t2.target_ins_level
|
||||||
|
end as target_ins_level,
|
||||||
|
'AIA(Monthly)' as DATA_SOURCE
|
||||||
|
from sales t1
|
||||||
|
left join target_hp t2
|
||||||
|
on t1.inst_code = t2.inst_code
|
||||||
|
and t1.MARKET = t2.MARKET
|
||||||
|
left join dragon_flag t3
|
||||||
|
on t1.inst_code = t3.inst_code
|
||||||
|
and t1.MARKET = t3.MARKET
|
||||||
|
left join dm.dm_td_institution inst
|
||||||
|
on t1.inst_code =inst.inst_code
|
||||||
|
left join dm.dm_td_geography geo
|
||||||
|
on inst.county_code = geo.geo_key
|
||||||
|
union all
|
||||||
|
select *
|
||||||
|
from col
|
||||||
|
cross join other_data_source
|
||||||
|
)
|
||||||
|
,sub_channel_rank as (
|
||||||
|
select distinct SUB_CHANNELSTD,SUB_CHANNEL_ORDER
|
||||||
|
from dm.dm_td_sd_channel
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
select t1.*,NVL(t2.SUB_CHANNEL_ORDER,100) as SUB_CHANNEL_ORDER
|
||||||
|
from temp_1 t1
|
||||||
|
left join sub_channel_rank t2
|
||||||
|
on t1.sub_channel = t2.SUB_CHANNELSTD
|
||||||
BIN
EXTERNAL/AIA/z_03 dm_aia_flag_备份用.sql:Zone.Identifier
Normal file
BIN
EXTERNAL/AIA/z_03 dm_aia_flag_备份用.sql:Zone.Identifier
Normal file
Binary file not shown.
231
EXTERNAL/CHC/01 dm_chc_pack_property.sql
Normal file
231
EXTERNAL/CHC/01 dm_chc_pack_property.sql
Normal file
@@ -0,0 +1,231 @@
|
|||||||
|
-- Databricks notebook source
|
||||||
|
-- CREATE OR REPLACE TABLE dm.dm_chc_pack_property (
|
||||||
|
-- PACK_COD STRING,
|
||||||
|
-- pack_des STRING,
|
||||||
|
-- stgh_des STRING,
|
||||||
|
-- pack_lch STRING,
|
||||||
|
-- PROD_COD STRING,
|
||||||
|
-- prod_des STRING,
|
||||||
|
-- prod_des_c STRING,
|
||||||
|
-- Family_Code STRING,
|
||||||
|
-- Family_Name STRING,
|
||||||
|
-- cmps_cod STRING,
|
||||||
|
-- cmps_des STRING,
|
||||||
|
-- cmps_des_c STRING,
|
||||||
|
-- atc1_cod STRING,
|
||||||
|
-- atc1_des STRING,
|
||||||
|
-- atc1_des_c STRING,
|
||||||
|
-- atc2_cod STRING,
|
||||||
|
-- atc2_des STRING,
|
||||||
|
-- atc2_des_c STRING,
|
||||||
|
-- atc3_cod STRING,
|
||||||
|
-- atc3_des STRING,
|
||||||
|
-- atc3_des_c STRING,
|
||||||
|
-- atc4_cod STRING,
|
||||||
|
-- atc4_des STRING,
|
||||||
|
-- atc4_des_c STRING,
|
||||||
|
-- app1_cod STRING,
|
||||||
|
-- app1_des STRING,
|
||||||
|
-- app1_des_c STRING,
|
||||||
|
-- app2_cod STRING,
|
||||||
|
-- app2_des STRING,
|
||||||
|
-- app2_des_c STRING,
|
||||||
|
-- app3_cod STRING,
|
||||||
|
-- app3_des STRING,
|
||||||
|
-- app3_des_c STRING,
|
||||||
|
-- bio_desc STRING,
|
||||||
|
-- gene_orig_desc STRING,
|
||||||
|
-- eth_otc_desc STRING,
|
||||||
|
-- nrdl_desc STRING,
|
||||||
|
-- NRDL_Entry_Date STRING,
|
||||||
|
-- edl_desc STRING,
|
||||||
|
-- tcm_desc STRING,
|
||||||
|
-- paed_desc STRING,
|
||||||
|
-- gqce_desc STRING,
|
||||||
|
-- vbp_desc STRING,
|
||||||
|
-- manu_cod STRING,
|
||||||
|
-- manu_des STRING,
|
||||||
|
-- manu_des_c STRING,
|
||||||
|
-- mnfl_cod STRING,
|
||||||
|
-- mnfl_des STRING,
|
||||||
|
-- corp_cod STRING,
|
||||||
|
-- corp_des STRING,
|
||||||
|
-- corp_des_c STRING,
|
||||||
|
-- BrandType STRING,
|
||||||
|
-- IS_AZ STRING,
|
||||||
|
-- AZ_MAIN STRING,
|
||||||
|
-- AZ_Related STRING,
|
||||||
|
-- countingunit STRING,
|
||||||
|
-- ETL_INSERT_DT TIMESTAMP,
|
||||||
|
-- ETL_UPDATE_DT TIMESTAMP)
|
||||||
|
-- USING delta
|
||||||
|
-- -- LOCATION 'abfss://master@azcdatalakeprd.dfs.core.chinacloudapi.cn/DM/dm_aia_pack_property';
|
||||||
|
-- -- 上面是生产环境location,下面是测试环境location
|
||||||
|
-- LOCATION 'abfss://master@retaildlstoragetest.dfs.core.chinacloudapi.cn/DM/dm_aia_pack_property';
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
update dwd.dwd_gnd_ext_chc_pack_property
|
||||||
|
set iqvia_pack_code = if( iqvia_pack_code REGEXP '^[0-9]',right(concat('000000000000',iqvia_pack_code),12),iqvia_pack_code)
|
||||||
|
,iqvia_prod_code = right(concat('000000000',iqvia_prod_code),9)
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
create or replace temporary view dwd_pack_property
|
||||||
|
as
|
||||||
|
select distinct
|
||||||
|
iqvia_pack_code ,
|
||||||
|
iqvia_prod_code ,
|
||||||
|
pack_cod ,
|
||||||
|
app1_cod ,
|
||||||
|
app1_des ,
|
||||||
|
app1_des_c ,
|
||||||
|
app2_cod ,
|
||||||
|
app2_des ,
|
||||||
|
app2_des_c ,
|
||||||
|
app3_cod ,
|
||||||
|
app3_des ,
|
||||||
|
app3_des_c ,
|
||||||
|
atc1_cod ,
|
||||||
|
atc1_des ,
|
||||||
|
atc1_des_c ,
|
||||||
|
atc2_cod ,
|
||||||
|
atc2_des ,
|
||||||
|
atc2_des_c ,
|
||||||
|
atc3_cod ,
|
||||||
|
atc3_des ,
|
||||||
|
atc3_des_c ,
|
||||||
|
atc4_cod ,
|
||||||
|
atc4_des ,
|
||||||
|
atc4_des_c ,
|
||||||
|
bio_desc ,
|
||||||
|
cmps_cod ,
|
||||||
|
cmps_des ,
|
||||||
|
cmps_des_c ,
|
||||||
|
corp_cod ,
|
||||||
|
corp_des ,
|
||||||
|
corp_des_c ,
|
||||||
|
edl_desc ,
|
||||||
|
eth_otc_desc ,
|
||||||
|
gene_orig_desc ,
|
||||||
|
gqce_desc ,
|
||||||
|
manu_cod ,
|
||||||
|
manu_des ,
|
||||||
|
manu_des_c ,
|
||||||
|
mnfl_cod ,
|
||||||
|
mnfl_des ,
|
||||||
|
nrdl_desc ,
|
||||||
|
pack_des ,
|
||||||
|
stgh_des ,
|
||||||
|
pack_lch ,
|
||||||
|
paed_desc ,
|
||||||
|
prod_des ,
|
||||||
|
prod_des_c ,
|
||||||
|
tcm_desc ,
|
||||||
|
vbp_desc ,
|
||||||
|
builtinarchivedate ,
|
||||||
|
source_file_path ,
|
||||||
|
source_file_name ,
|
||||||
|
etl_insert_dt ,
|
||||||
|
countingunit
|
||||||
|
from dwd.dwd_gnd_ext_chc_pack_property
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
-- create or replace table dm.dm_chc_pack_property
|
||||||
|
-- LOCATION 'abfss://master@retaildlstoragetest.dfs.core.chinacloudapi.cn/DM/dm_chc_pack_property'
|
||||||
|
-- as
|
||||||
|
insert overwrite table dm.dm_chc_pack_property
|
||||||
|
WITH dedup_l AS (
|
||||||
|
SELECT *,
|
||||||
|
ROW_NUMBER() OVER (PARTITION BY iqvia_pack_code ORDER BY iqvia_pack_code) AS rn
|
||||||
|
FROM dwd_pack_property
|
||||||
|
QUALIFY rn = 1
|
||||||
|
),
|
||||||
|
dedup_r AS (
|
||||||
|
SELECT *,
|
||||||
|
ROW_NUMBER() OVER (PARTITION BY PACK_COD ORDER BY PACK_COD) AS rn
|
||||||
|
FROM tmp.tmp_ext_chpa_pack_propetry
|
||||||
|
QUALIFY rn = 1
|
||||||
|
)
|
||||||
|
SELECT
|
||||||
|
l.iqvia_pack_code AS PACK_COD,
|
||||||
|
|
||||||
|
-- Pack / Product 描述
|
||||||
|
COALESCE(r.PACK_DES, l.pack_des) AS pack_des,
|
||||||
|
COALESCE(r.STGH_DES, l.stgh_des) AS stgh_des,
|
||||||
|
COALESCE(r.PACK_LCH, l.pack_lch) AS pack_lch,
|
||||||
|
COALESCE(r.PROD_COD, l.iqvia_prod_code) AS PROD_COD,
|
||||||
|
COALESCE(r.PROD_DES, l.prod_des) AS prod_des,
|
||||||
|
COALESCE(r.PROD_DES_C, l.prod_des_c) AS prod_des_c,
|
||||||
|
|
||||||
|
-- 右表独有:Family
|
||||||
|
r.Family_Code,
|
||||||
|
r.Family_Name,
|
||||||
|
|
||||||
|
-- 成分
|
||||||
|
COALESCE(r.CMPS_COD, l.cmps_cod) AS cmps_cod,
|
||||||
|
COALESCE(r.CMPS_DES, l.cmps_des) AS cmps_des,
|
||||||
|
COALESCE(r.CMPS_DES_C, l.cmps_des_c) AS cmps_des_c,
|
||||||
|
|
||||||
|
-- ATC
|
||||||
|
COALESCE(r.ATC1_COD, l.atc1_cod) AS atc1_cod,
|
||||||
|
COALESCE(r.atc1_des, l.atc1_des) AS atc1_des,
|
||||||
|
COALESCE(r.atc1_des_c, l.atc1_des_c) AS atc1_des_c,
|
||||||
|
COALESCE(r.ATC2_COD, l.atc2_cod) AS atc2_cod,
|
||||||
|
COALESCE(r.atc2_des, l.atc2_des) AS atc2_des,
|
||||||
|
COALESCE(r.atc2_des_c, l.atc2_des_c) AS atc2_des_c,
|
||||||
|
COALESCE(r.ATC3_COD, l.atc3_cod) AS atc3_cod,
|
||||||
|
COALESCE(r.atc3_des, l.atc3_des) AS atc3_des,
|
||||||
|
COALESCE(r.atc3_des_c, l.atc3_des_c) AS atc3_des_c,
|
||||||
|
COALESCE(r.ATC4_COD, l.atc4_cod) AS atc4_cod,
|
||||||
|
COALESCE(r.atc4_des, l.atc4_des) AS atc4_des,
|
||||||
|
COALESCE(r.atc4_des_c, l.atc4_des_c) AS atc4_des_c,
|
||||||
|
|
||||||
|
-- APP
|
||||||
|
COALESCE(r.APP1_COD, l.app1_cod) AS app1_cod,
|
||||||
|
COALESCE(r.app1_des, l.app1_des) AS app1_des,
|
||||||
|
COALESCE(r.app1_des_c, l.app1_des_c) AS app1_des_c,
|
||||||
|
COALESCE(r.APP2_COD, l.app2_cod) AS app2_cod,
|
||||||
|
COALESCE(r.app2_des, l.app2_des) AS app2_des,
|
||||||
|
COALESCE(r.app2_des_c, l.app2_des_c) AS app2_des_c,
|
||||||
|
COALESCE(r.APP3_COD, l.app3_cod) AS app3_cod,
|
||||||
|
COALESCE(r.app3_des, l.app3_des) AS app3_des,
|
||||||
|
COALESCE(r.app3_des_c, l.app3_des_c) AS app3_des_c,
|
||||||
|
|
||||||
|
-- 标志位
|
||||||
|
COALESCE(r.BIO_DESC, l.bio_desc) AS bio_desc,
|
||||||
|
COALESCE(r.GENE_ORIG_DESC, l.gene_orig_desc) AS gene_orig_desc,
|
||||||
|
COALESCE(r.ETH_OTC_DESC, l.eth_otc_desc) AS eth_otc_desc,
|
||||||
|
COALESCE(r.NRDL_DESC, l.nrdl_desc) AS nrdl_desc,
|
||||||
|
r.NRDL_Entry_Date,
|
||||||
|
COALESCE(r.EDL_DESC, l.edl_desc) AS edl_desc,
|
||||||
|
COALESCE(r.TCM_DESC, l.tcm_desc) AS tcm_desc,
|
||||||
|
COALESCE(r.PAED_DESC, l.paed_desc) AS paed_desc,
|
||||||
|
COALESCE(r.GQCE_DESC, l.gqce_desc) AS gqce_desc,
|
||||||
|
COALESCE(r.VBP_DESC, l.vbp_desc) AS vbp_desc,
|
||||||
|
|
||||||
|
-- 厂商
|
||||||
|
COALESCE(r.MANU_COD, l.manu_cod) AS manu_cod,
|
||||||
|
COALESCE(r.MANU_DES, l.manu_des) AS manu_des,
|
||||||
|
COALESCE(r.MANU_DES_C, l.manu_des_c) AS manu_des_c,
|
||||||
|
COALESCE(r.MNFL_COD, l.mnfl_cod) AS mnfl_cod,
|
||||||
|
COALESCE(r.MNFL_DES, l.mnfl_des) AS mnfl_des,
|
||||||
|
|
||||||
|
-- 公司
|
||||||
|
COALESCE(r.CORP_COD, l.corp_cod) AS corp_cod,
|
||||||
|
COALESCE(r.CORP_DES, l.corp_des) AS corp_des,
|
||||||
|
COALESCE(r.CORP_DES_C, l.corp_des_c) AS corp_des_c,
|
||||||
|
|
||||||
|
-- 右表独有:品牌 / AZ 标志
|
||||||
|
r.BrandType,
|
||||||
|
r.IS_AZ,
|
||||||
|
r.AZ_MAIN,
|
||||||
|
r.AZ_Related,
|
||||||
|
-- 左表独有
|
||||||
|
l.countingunit,
|
||||||
|
from_utc_timestamp(current_timestamp(),'UTC+8') as ETL_INSERT_DT,
|
||||||
|
from_utc_timestamp(current_timestamp(),'UTC+8') as ETL_UPDATE_DT
|
||||||
|
FROM dedup_l l
|
||||||
|
LEFT JOIN dedup_r r
|
||||||
|
ON l.iqvia_pack_code = r.PACK_COD
|
||||||
BIN
EXTERNAL/CHC/01 dm_chc_pack_property.sql:Zone.Identifier
Normal file
BIN
EXTERNAL/CHC/01 dm_chc_pack_property.sql:Zone.Identifier
Normal file
Binary file not shown.
418
EXTERNAL/CHC/02 DM_TF_EXT_CHC_SALES.sql
Normal file
418
EXTERNAL/CHC/02 DM_TF_EXT_CHC_SALES.sql
Normal file
@@ -0,0 +1,418 @@
|
|||||||
|
-- Databricks notebook source
|
||||||
|
-- CREATE OR REPLACE TABLE DM.DM_TF_EXT_CHC_SALES (
|
||||||
|
-- YYYYMM STRING,
|
||||||
|
-- PACK_CODE STRING,
|
||||||
|
-- CORP_CODE STRING,
|
||||||
|
-- AUDIT_CODE STRING,
|
||||||
|
-- PLATFORM_TYPE STRING,
|
||||||
|
-- STORE_NAME STRING,
|
||||||
|
-- STORE_TYPE STRING,
|
||||||
|
-- REGION_TYPE STRING,
|
||||||
|
-- PACK_FLAG INT,
|
||||||
|
-- PROD_FLAG INT,
|
||||||
|
-- DTP_FLAG INT,
|
||||||
|
-- SALES_UNIT_CAL DECIMAL(38,10),
|
||||||
|
-- SALES_UNIT_CAL_LY DECIMAL(38,10),
|
||||||
|
-- SALES_VALUE_CAL DECIMAL(38,10),
|
||||||
|
-- SALES_VALUE_CAL_LY DECIMAL(38,10),
|
||||||
|
-- CONUTING_UNIT DECIMAL(38,10),
|
||||||
|
-- CONUTING_UNIT_LY DECIMAL(38,10),
|
||||||
|
-- DATA_SOURCE STRING,
|
||||||
|
-- INST_CODE STRING COMMENT '内部机构编码',
|
||||||
|
-- CMPS_FLAG STRING COMMENT '分子式标签',
|
||||||
|
-- DEPT_NAME STRING COMMENT '科室名称',
|
||||||
|
-- PRESCRIPTION DECIMAL(38,10) COMMENT '处方张数',
|
||||||
|
-- PRESCRIPTION_LY DECIMAL(38,10) COMMENT '去年同期处方张数',
|
||||||
|
-- NEW_CODE STRING COMMENT '主数据关联CODE',
|
||||||
|
-- AREA STRING COMMENT '城市',
|
||||||
|
-- H_LEVEL STRING COMMENT '医院类型',
|
||||||
|
-- REIMBURSE STRING COMMENT '报销情况',
|
||||||
|
-- REIMBURSE_TYPE STRING COMMENT '报销类型',
|
||||||
|
-- PRESCRIPTION_SOURCE STRING COMMENT '处方来源',
|
||||||
|
-- ETL_INSERT_DT TIMESTAMP,
|
||||||
|
-- ETL_UPDATE_DT TIMESTAMP
|
||||||
|
-- )
|
||||||
|
-- USING delta
|
||||||
|
-- LOCATION 'abfss://master@azcdatalakeprd.dfs.core.chinacloudapi.cn/DM/dm_tf_ext_chc_sales';
|
||||||
|
-- 上面是生产环境location,下面是测试环境location
|
||||||
|
-- LOCATION 'abfss://master@retaildlstoragetest.dfs.core.chinacloudapi.cn/DM/dm_tf_ext_chc_sales';
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
CREATE OR REPLACE TEMPORARY VIEW RAW_DATA
|
||||||
|
AS
|
||||||
|
SELECT
|
||||||
|
CASE
|
||||||
|
WHEN SUBSTRING(YQ, 5, 2) = 'Q1'
|
||||||
|
THEN CONCAT (SUBSTRING(YQ, 1, 4), '03')
|
||||||
|
WHEN SUBSTRING(YQ, 5, 2) = 'Q2'
|
||||||
|
THEN CONCAT (SUBSTRING(YQ, 1, 4), '06')
|
||||||
|
WHEN SUBSTRING(YQ, 5, 2) = 'Q3'
|
||||||
|
THEN CONCAT (SUBSTRING(YQ, 1, 4), '09')
|
||||||
|
WHEN SUBSTRING(YQ, 5, 2) = 'Q4'
|
||||||
|
THEN CONCAT (SUBSTRING(YQ, 1, 4), '12')
|
||||||
|
ELSE YQ
|
||||||
|
END AS YQ,
|
||||||
|
CITY_C,
|
||||||
|
PACK_COD,
|
||||||
|
PROD_COD,
|
||||||
|
VALUE,
|
||||||
|
TOTALUNIT,
|
||||||
|
COUNTINGUNIT
|
||||||
|
FROM
|
||||||
|
DWD.DWD_GND_CHC_RAWDATA
|
||||||
|
WHERE
|
||||||
|
YEAR >= '2021'
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
CREATE OR REPLACE TEMPORARY VIEW FILTER_YM
|
||||||
|
AS
|
||||||
|
SELECT
|
||||||
|
MAX(YQ) AS MAX_YYYYMM,
|
||||||
|
CONCAT(CAST(FLOOR(MAX(YQ)/100-1) AS STRING),'01') AS MIN_YYYYMM
|
||||||
|
FROM
|
||||||
|
RAW_DATA
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
CREATE OR REPLACE TEMPORARY VIEW CORP_DATA
|
||||||
|
AS
|
||||||
|
SELECT
|
||||||
|
PACK_COD,
|
||||||
|
CORP_COD,
|
||||||
|
CORP_DES
|
||||||
|
FROM
|
||||||
|
DM.DM_IMS_TD_PACK_PROPERTY
|
||||||
|
GROUP BY
|
||||||
|
PACK_COD,
|
||||||
|
CORP_COD,
|
||||||
|
CORP_DES
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
CREATE OR REPLACE TEMPORARY VIEW PACK_DATA
|
||||||
|
AS
|
||||||
|
SELECT
|
||||||
|
DISTINCT
|
||||||
|
IQVIA_PACK_CODE,
|
||||||
|
IQVIA_PROD_CODE,
|
||||||
|
PACK_COD,
|
||||||
|
APP1_COD,
|
||||||
|
APP1_DES,
|
||||||
|
APP1_DES_C,
|
||||||
|
APP2_COD,
|
||||||
|
APP2_DES,
|
||||||
|
APP2_DES_C,
|
||||||
|
APP3_COD,
|
||||||
|
APP3_DES,
|
||||||
|
APP3_DES_C,
|
||||||
|
ATC1_COD,
|
||||||
|
ATC1_DES,
|
||||||
|
ATC1_DES_C,
|
||||||
|
ATC2_COD,
|
||||||
|
ATC2_DES,
|
||||||
|
ATC2_DES_C,
|
||||||
|
ATC3_COD,
|
||||||
|
ATC3_DES,
|
||||||
|
ATC3_DES_C,
|
||||||
|
ATC4_COD,
|
||||||
|
ATC4_DES,
|
||||||
|
ATC4_DES_C,
|
||||||
|
BIO_DESC,
|
||||||
|
CMPS_COD,
|
||||||
|
CMPS_DES,
|
||||||
|
CMPS_DES_C,
|
||||||
|
CORP_COD,
|
||||||
|
CORP_DES,
|
||||||
|
CORP_DES_C,
|
||||||
|
EDL_DESC,
|
||||||
|
ETH_OTC_DESC,
|
||||||
|
GENE_ORIG_DESC,
|
||||||
|
GQCE_DESC,
|
||||||
|
MANU_COD,
|
||||||
|
MANU_DES,
|
||||||
|
MANU_DES_C,
|
||||||
|
MNFL_COD,
|
||||||
|
MNFL_DES,
|
||||||
|
NRDL_DESC,
|
||||||
|
PACK_DES,
|
||||||
|
STGH_DES,
|
||||||
|
PACK_LCH,
|
||||||
|
PAED_DESC,
|
||||||
|
PROD_DES,
|
||||||
|
PROD_DES_C,
|
||||||
|
TCM_DESC,
|
||||||
|
VBP_DESC,
|
||||||
|
BUILTINARCHIVEDATE,
|
||||||
|
SOURCE_FILE_PATH,
|
||||||
|
SOURCE_FILE_NAME,
|
||||||
|
ETL_INSERT_DT,
|
||||||
|
COUNTINGUNIT
|
||||||
|
FROM
|
||||||
|
DWD.DWD_GND_EXT_CHC_PACK_PROPERTY
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
--CHC本身唯独表 存在CORP_COD以及 CORP_DES 与手工数据不一致的问题
|
||||||
|
CREATE OR REPLACE TEMPORARY VIEW PACK_WIHT_CORP_DATA
|
||||||
|
AS
|
||||||
|
SELECT
|
||||||
|
T1.PACK_COD,
|
||||||
|
T1.IQVIA_PACK_CODE,
|
||||||
|
T1.COUNTINGUNIT,
|
||||||
|
COALESCE(T2.CORP_COD, T1.CORP_COD) AS CORP_COD,
|
||||||
|
COALESCE(T2.CORP_DES, T1.CORP_DES) AS CORP_DES
|
||||||
|
FROM
|
||||||
|
PACK_DATA T1
|
||||||
|
LEFT JOIN
|
||||||
|
CORP_DATA T2
|
||||||
|
ON
|
||||||
|
T1.IQVIA_PACK_CODE = T2.PACK_COD
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
--GEO相关信息 在CHPA中没有 苏州、无锡、福州等数据,根据业务反馈,按照 CITY_C当作AUDIT_COD
|
||||||
|
CREATE OR REPLACE TEMPORARY VIEW DIM_EXCHC_GEO
|
||||||
|
AS
|
||||||
|
SELECT
|
||||||
|
T1.CITY_E,
|
||||||
|
CONCAT (T1.CITY_C, '市') AS CITY_C,
|
||||||
|
UPPER(NVL (T2.AUDIT_COD, T1.CITY_E)) AS AUDIT_COD
|
||||||
|
FROM
|
||||||
|
DWD.DWD_GND_EXT_CHC_GEO T1
|
||||||
|
LEFT JOIN
|
||||||
|
DM.DM_IMS_TD_GEO T2
|
||||||
|
ON
|
||||||
|
T1.CITY_E = T2.CITY
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
CREATE OR REPLACE TEMPORARY VIEW RAW_DATA_WIHT_PACK_CORP_GEO_DATA
|
||||||
|
AS
|
||||||
|
SELECT
|
||||||
|
T1.YQ,
|
||||||
|
T1.CITY_C,
|
||||||
|
T3.IQVIA_PACK_CODE AS PACK_COD,
|
||||||
|
T3.CORP_COD AS CORP_COD,
|
||||||
|
C.AUDIT_COD AS AUDIT_COD,
|
||||||
|
T1.PROD_COD,
|
||||||
|
T1.VALUE,
|
||||||
|
T1.TOTALUNIT,
|
||||||
|
T1.TOTALUNIT * NVL (T3.COUNTINGUNIT, 1) AS COUNTINGUNIT
|
||||||
|
FROM
|
||||||
|
RAW_DATA T1
|
||||||
|
LEFT JOIN
|
||||||
|
PACK_WIHT_CORP_DATA T3
|
||||||
|
ON
|
||||||
|
T1.PACK_COD = T3.PACK_COD
|
||||||
|
LEFT JOIN
|
||||||
|
DIM_EXCHC_GEO C
|
||||||
|
ON
|
||||||
|
T1.CITY_C = C.CITY_C
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
CREATE OR REPLACE TEMPORARY VIEW RAW_DATA_WIHT_LY
|
||||||
|
AS
|
||||||
|
SELECT
|
||||||
|
A.YQ,
|
||||||
|
A.AUDIT_COD,
|
||||||
|
A.PACK_COD,
|
||||||
|
A.CORP_COD,
|
||||||
|
SUM(A.VALUE) VALUE,
|
||||||
|
SUM(A.TOTALUNIT) TOTALUNIT,
|
||||||
|
SUM(A.COUNTINGUNIT) COUNTINGUNIT,
|
||||||
|
SUM(VALUE_LY) VALUE_LY,
|
||||||
|
SUM(TOTALUNIT_LY) TOTALUNIT_LY,
|
||||||
|
SUM(COUNTINGUNIT_LY) COUNTINGUNIT_LY
|
||||||
|
FROM
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
T1.YQ,
|
||||||
|
T1.AUDIT_COD,
|
||||||
|
T1.PACK_COD,
|
||||||
|
T1.CORP_COD,
|
||||||
|
T1.VALUE,
|
||||||
|
T1.TOTALUNIT,
|
||||||
|
T1.COUNTINGUNIT,
|
||||||
|
0 VALUE_LY,
|
||||||
|
0 TOTALUNIT_LY,
|
||||||
|
0 COUNTINGUNIT_LY
|
||||||
|
FROM
|
||||||
|
RAW_DATA_WIHT_PACK_CORP_GEO_DATA T1
|
||||||
|
UNION ALL
|
||||||
|
SELECT
|
||||||
|
CAST(T1.YQ + 100 AS INT),
|
||||||
|
T1.AUDIT_COD,
|
||||||
|
T1.PACK_COD,
|
||||||
|
T1.CORP_COD,
|
||||||
|
0 VALUE,
|
||||||
|
0 TOTALUNIT,
|
||||||
|
0 COUNTINGUNIT,
|
||||||
|
T1.VALUE VALUE_LY,
|
||||||
|
T1.TOTALUNIT TOTALUNIT_LY,
|
||||||
|
T1.COUNTINGUNIT COUNTINGUNIT_LY
|
||||||
|
FROM
|
||||||
|
RAW_DATA_WIHT_PACK_CORP_GEO_DATA AS T1
|
||||||
|
CROSS JOIN
|
||||||
|
FILTER_YM AS T2
|
||||||
|
WHERE
|
||||||
|
T1.YQ + 100 <= T2.MAX_YYYYMM
|
||||||
|
) A
|
||||||
|
GROUP BY
|
||||||
|
A.YQ,
|
||||||
|
A.AUDIT_COD,
|
||||||
|
A.PACK_COD,
|
||||||
|
A.CORP_COD
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
CREATE OR REPLACE TEMPORARY VIEW RAW_DATA_FINAL
|
||||||
|
AS
|
||||||
|
SELECT
|
||||||
|
T1.YQ AS YYYYMM,
|
||||||
|
T1.AUDIT_COD,
|
||||||
|
T1.PACK_COD,
|
||||||
|
T1.CORP_COD,
|
||||||
|
SUM(TOTALUNIT) AS SALES_UNIT_CAL, -- TOTALUNIT,
|
||||||
|
SUM(TOTALUNIT_LY) AS SALES_UNIT_CAL_LY, --TOTALUNIT_LY,
|
||||||
|
SUM(VALUE) AS SALES_VALUE_CAL, --VALUE,
|
||||||
|
SUM(VALUE_LY) AS SALES_VALUE_CAL_LY,
|
||||||
|
SUM(COUNTINGUNIT) AS CONUTING_UNIT, --COUNTINGUNIT,
|
||||||
|
SUM(COUNTINGUNIT_LY) AS CONUTING_UNIT_LY,
|
||||||
|
'CHC(Quarterly)' AS DATA_SOURCE
|
||||||
|
FROM
|
||||||
|
RAW_DATA_WIHT_LY T1
|
||||||
|
GROUP BY
|
||||||
|
T1.YQ,
|
||||||
|
T1.AUDIT_COD,
|
||||||
|
T1.PACK_COD,
|
||||||
|
T1.CORP_COD
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
CREATE OR REPLACE TEMPORARY VIEW DM_TF_EXT_CHC_SALES AS
|
||||||
|
SELECT
|
||||||
|
A.YYYYMM,
|
||||||
|
A.PACK_COD AS PACK_CODE,
|
||||||
|
A.AUDIT_COD AS AUDIT_CODE,
|
||||||
|
'CHC(Quarterly)' DATA_SOURCE,
|
||||||
|
-------------------------------------
|
||||||
|
SALES_UNIT_CAL,
|
||||||
|
SALES_UNIT_CAL_LY,
|
||||||
|
SALES_VALUE_CAL,
|
||||||
|
SALES_VALUE_CAL_LY,
|
||||||
|
CONUTING_UNIT,
|
||||||
|
CONUTING_UNIT_LY,
|
||||||
|
null as prescription,
|
||||||
|
null as prescription_ly,
|
||||||
|
-------------------------------------
|
||||||
|
--Retail藏数逻辑标签------------------
|
||||||
|
1 PACK_FLAG,
|
||||||
|
1 PROD_FLAG,
|
||||||
|
0 DTP_FLAG,
|
||||||
|
null as cmps_flag,
|
||||||
|
-------------------------------------
|
||||||
|
--EC数据标签--------------------------
|
||||||
|
'' PLATFORM_TYPE,
|
||||||
|
'' STORE_NAME,
|
||||||
|
'' STORE_TYPE,
|
||||||
|
-------------------------------------
|
||||||
|
--COUNTY数据标签----------------------
|
||||||
|
'' REGION_TYPE,
|
||||||
|
-------------------------------------
|
||||||
|
--AIA数据标签-------------------------
|
||||||
|
null inst_code, --可以移除,报告没有使用
|
||||||
|
-------------------------------------
|
||||||
|
--XIE HE 数据标签---------------------
|
||||||
|
'' as dept_name,
|
||||||
|
'' as new_code,
|
||||||
|
'' as area,
|
||||||
|
'' as h_level,
|
||||||
|
'' as reimburse,
|
||||||
|
'' as reimburse_type,
|
||||||
|
'' as prescription_source,
|
||||||
|
-------------------------------------
|
||||||
|
'' as CORP_CODE --可以移除,报告没有使用E
|
||||||
|
-------------------------------------
|
||||||
|
FROM
|
||||||
|
RAW_DATA_FINAL A
|
||||||
|
WHERE
|
||||||
|
YYYYMM
|
||||||
|
>= (
|
||||||
|
SELECT
|
||||||
|
CAST((LEFT(MAX(YYYYMM), 4) - 3) * 100 + 1 AS INT)
|
||||||
|
FROM
|
||||||
|
RAW_DATA_FINAL
|
||||||
|
)
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
INSERT OVERWRITE TABLE DM.DM_TF_EXT_CHC_SALES (
|
||||||
|
YYYYMM,
|
||||||
|
PACK_CODE,
|
||||||
|
AUDIT_CODE,
|
||||||
|
DATA_SOURCE,
|
||||||
|
SALES_UNIT_CAL,
|
||||||
|
SALES_UNIT_CAL_LY,
|
||||||
|
SALES_VALUE_CAL,
|
||||||
|
SALES_VALUE_CAL_LY,
|
||||||
|
CONUTING_UNIT,
|
||||||
|
CONUTING_UNIT_LY,
|
||||||
|
PRESCRIPTION,
|
||||||
|
PRESCRIPTION_LY,
|
||||||
|
PACK_FLAG,
|
||||||
|
PROD_FLAG,
|
||||||
|
DTP_FLAG,
|
||||||
|
CMPS_FLAG,
|
||||||
|
PLATFORM_TYPE,
|
||||||
|
STORE_NAME,
|
||||||
|
STORE_TYPE,
|
||||||
|
REGION_TYPE,
|
||||||
|
INST_CODE,
|
||||||
|
DEPT_NAME,
|
||||||
|
NEW_CODE,
|
||||||
|
AREA,
|
||||||
|
H_LEVEL,
|
||||||
|
REIMBURSE,
|
||||||
|
REIMBURSE_TYPE,
|
||||||
|
PRESCRIPTION_SOURCE,
|
||||||
|
CORP_CODE,
|
||||||
|
ETL_INSERT_DT,
|
||||||
|
ETL_UPDATE_DT
|
||||||
|
)
|
||||||
|
SELECT
|
||||||
|
YYYYMM,
|
||||||
|
NVL(NULLIF(PACK_CODE, ''), CONCAT('PACK_CODE_', DATA_SOURCE)) AS PACK_CODE,
|
||||||
|
NVL(NULLIF(AUDIT_CODE, ''), 'ROC') AS AUDIT_CODE,
|
||||||
|
DATA_SOURCE,
|
||||||
|
SALES_UNIT_CAL,
|
||||||
|
SALES_UNIT_CAL_LY,
|
||||||
|
SALES_VALUE_CAL,
|
||||||
|
SALES_VALUE_CAL_LY,
|
||||||
|
CONUTING_UNIT,
|
||||||
|
CONUTING_UNIT_LY,
|
||||||
|
PRESCRIPTION,
|
||||||
|
PRESCRIPTION_LY,
|
||||||
|
PACK_FLAG,
|
||||||
|
PROD_FLAG,
|
||||||
|
DTP_FLAG,
|
||||||
|
CMPS_FLAG,
|
||||||
|
PLATFORM_TYPE,
|
||||||
|
STORE_NAME,
|
||||||
|
STORE_TYPE,
|
||||||
|
REGION_TYPE,
|
||||||
|
INST_CODE,
|
||||||
|
DEPT_NAME,
|
||||||
|
NEW_CODE,
|
||||||
|
AREA,
|
||||||
|
H_LEVEL,
|
||||||
|
REIMBURSE,
|
||||||
|
REIMBURSE_TYPE,
|
||||||
|
PRESCRIPTION_SOURCE,
|
||||||
|
CORP_CODE,
|
||||||
|
FROM_UTC_TIMESTAMP(CURRENT_TIMESTAMP(), 'UTC+8') AS ETL_INSERT_DT,
|
||||||
|
FROM_UTC_TIMESTAMP(CURRENT_TIMESTAMP(), 'UTC+8') AS ETL_UPDATE_DT
|
||||||
|
FROM
|
||||||
|
DM_TF_EXT_CHC_SALES;
|
||||||
BIN
EXTERNAL/CHC/02 DM_TF_EXT_CHC_SALES.sql:Zone.Identifier
Normal file
BIN
EXTERNAL/CHC/02 DM_TF_EXT_CHC_SALES.sql:Zone.Identifier
Normal file
Binary file not shown.
147
EXTERNAL/CHC/03 DM_TD_EXT_CHC_PACK_PROPERTY.sql
Normal file
147
EXTERNAL/CHC/03 DM_TD_EXT_CHC_PACK_PROPERTY.sql
Normal file
@@ -0,0 +1,147 @@
|
|||||||
|
-- Databricks notebook source
|
||||||
|
-- CREATE OR REPLACE TABLE DM.DM_TD_EXT_CHC_PACK_PROPERTY
|
||||||
|
-- (
|
||||||
|
-- -- MARKET_PACK_KEY STRING,
|
||||||
|
-- PACK_CODE STRING,
|
||||||
|
-- PACK_DESC STRING,
|
||||||
|
-- STGH_DESC STRING,
|
||||||
|
-- PACK_LCH STRING,
|
||||||
|
-- FAMILY_CODE STRING,
|
||||||
|
-- FAMILY_NAME STRING,
|
||||||
|
-- PROD_CODE STRING,
|
||||||
|
-- PROD_DESC STRING,
|
||||||
|
-- PROD_DESC_C STRING,
|
||||||
|
-- CMPS_CODE STRING,
|
||||||
|
-- CMPS_DESC STRING,
|
||||||
|
-- CMPS_DESC_C STRING,
|
||||||
|
-- ATC1_CODE STRING,
|
||||||
|
-- ATC2_CODE STRING,
|
||||||
|
-- ATC3_CODE STRING,
|
||||||
|
-- ATC4_CODE STRING,
|
||||||
|
-- APP1_CODE STRING,
|
||||||
|
-- APP2_CODE STRING,
|
||||||
|
-- APP3_CODE STRING,
|
||||||
|
-- BIO_DESC STRING,
|
||||||
|
-- GENE_ORIG_DESC STRING,
|
||||||
|
-- ETH_OTC_DESC STRING,
|
||||||
|
-- NRDL_DESC STRING,
|
||||||
|
-- NRDL_ENTRY_DATE STRING,
|
||||||
|
-- EDL_DESC STRING,
|
||||||
|
-- TCM_DESC STRING,
|
||||||
|
-- PAED_DESC STRING,
|
||||||
|
-- GQCE_DESC STRING,
|
||||||
|
-- VBP_DESC_V STRING,
|
||||||
|
-- VBP_DESC STRING,
|
||||||
|
-- MANU_CODE STRING,
|
||||||
|
-- MANU_DESC STRING,
|
||||||
|
-- MANU_DESC_C STRING,
|
||||||
|
-- MNFL_CODE STRING,
|
||||||
|
-- MNFL_DESC STRING,
|
||||||
|
-- CORP_CODE STRING,
|
||||||
|
-- CORP_DESC STRING,
|
||||||
|
-- CORP_DESC_C STRING,
|
||||||
|
-- BRANDTYPE STRING,
|
||||||
|
-- -- MARKET STRING,
|
||||||
|
-- -- KEY_COMPETITOR STRING,
|
||||||
|
-- IS_AZ STRING,
|
||||||
|
-- AZ_MAIN STRING,
|
||||||
|
-- -- AZ_RELATED STRING,
|
||||||
|
-- ATC1_DESC STRING,
|
||||||
|
-- ATC1_DESC_C STRING,
|
||||||
|
-- ATC2_DESC STRING,
|
||||||
|
-- ATC2_DESC_C STRING,
|
||||||
|
-- ATC3_DESC STRING,
|
||||||
|
-- ATC3_DESC_C STRING,
|
||||||
|
-- ATC4_DESC STRING,
|
||||||
|
-- ATC4_DESC_C STRING,
|
||||||
|
-- APP1_DESC STRING,
|
||||||
|
-- APP1_DESC_C STRING,
|
||||||
|
-- APP2_DESC STRING,
|
||||||
|
-- APP2_DESC_C STRING,
|
||||||
|
-- APP3_DESC STRING,
|
||||||
|
-- APP3_DESC_C STRING,
|
||||||
|
-- -- CLASS STRING,
|
||||||
|
-- -- MARKET_RATIO STRING,
|
||||||
|
-- COUNTINGUNIT STRING,
|
||||||
|
-- VBP_BRAND STRING,
|
||||||
|
-- REPLENISH_FALG STRING,
|
||||||
|
-- ETL_INSERT_DT TIMESTAMP,
|
||||||
|
-- ETL_UPDATE_DT TIMESTAMP
|
||||||
|
-- )
|
||||||
|
-- USING delta
|
||||||
|
-- -- LOCATION 'abfss://master@azcdatalakeprd.dfs.core.chinacloudapi.cn/DM/dm_td_ext_chc_pack_property';
|
||||||
|
-- -- 上面是生产环境location,下面是测试环境location
|
||||||
|
-- LOCATION 'abfss://master@retaildlstoragetest.dfs.core.chinacloudapi.cn/DM/dm_td_ext_chc_pack_property';
|
||||||
|
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
INSERT OVERWRITE TABLE DM.DM_TD_EXT_CHC_PACK_PROPERTY
|
||||||
|
SELECT
|
||||||
|
-- MARKET_PACK_KEY,
|
||||||
|
PACK_COD AS PACK_CODE,
|
||||||
|
PACK_DES AS PACK_DESC,
|
||||||
|
STGH_DES AS STGH_DESC,
|
||||||
|
PACK_LCH,
|
||||||
|
FAMILY_CODE,
|
||||||
|
FAMILY_NAME,
|
||||||
|
PROD_COD AS PROD_CODE,
|
||||||
|
PROD_DES AS PROD_DESC,
|
||||||
|
PROD_DES_C AS PROD_DESC_C,
|
||||||
|
CMPS_COD AS CMPS_CODE,
|
||||||
|
CMPS_DES AS CMPS_DESC,
|
||||||
|
CMPS_DES_C AS CMPS_DESC_C,
|
||||||
|
ATC1_COD AS ATC1_CODE,
|
||||||
|
ATC2_COD AS ATC2_CODE,
|
||||||
|
ATC3_COD AS ATC3_CODE,
|
||||||
|
ATC4_COD AS ATC4_CODE,
|
||||||
|
APP1_COD AS APP1_CODE,
|
||||||
|
APP2_COD AS APP2_CODE,
|
||||||
|
APP3_COD AS APP3_CODE,
|
||||||
|
BIO_DESC,
|
||||||
|
GENE_ORIG_DESC,
|
||||||
|
ETH_OTC_DESC,
|
||||||
|
NRDL_DESC,
|
||||||
|
NRDL_ENTRY_DATE,
|
||||||
|
EDL_DESC,
|
||||||
|
TCM_DESC,
|
||||||
|
PAED_DESC,
|
||||||
|
GQCE_DESC,
|
||||||
|
null as VBP_DESC_V,
|
||||||
|
VBP_DESC,
|
||||||
|
MANU_COD AS MANU_CODE,
|
||||||
|
MANU_DES AS MANU_DESC,
|
||||||
|
MANU_DES_C AS MANU_DESC_C,
|
||||||
|
MNFL_COD AS MNFL_CODE,
|
||||||
|
MNFL_DES AS MNFL_DESC,
|
||||||
|
CORP_COD AS CORP_CODE,
|
||||||
|
CORP_DES AS CORP_DESC,
|
||||||
|
CORP_DES_C AS CORP_DESC_C,
|
||||||
|
BRANDTYPE,
|
||||||
|
-- MARKET,
|
||||||
|
-- KEY_COMPETITOR,
|
||||||
|
IS_AZ,
|
||||||
|
AZ_MAIN,
|
||||||
|
-- AZ_RELATED,
|
||||||
|
ATC1_DES AS ATC1_DESC,
|
||||||
|
ATC1_DES_C AS ATC1_DESC_C,
|
||||||
|
ATC2_DES AS ATC2_DESC,
|
||||||
|
ATC2_DES_C AS ATC2_DESC_C,
|
||||||
|
ATC3_DES AS ATC3_DESC,
|
||||||
|
ATC3_DES_C AS ATC3_DESC_C,
|
||||||
|
ATC4_DES AS ATC4_DESC,
|
||||||
|
ATC4_DES_C AS ATC4_DESC_C,
|
||||||
|
APP1_DES AS APP1_DESC,
|
||||||
|
APP1_DES_C AS APP1_DESC_C,
|
||||||
|
APP2_DES AS APP2_DESC,
|
||||||
|
APP2_DES_C AS APP2_DESC_C,
|
||||||
|
APP3_DES AS APP3_DESC,
|
||||||
|
APP3_DES_C AS APP3_DESC_C,
|
||||||
|
-- CLASS,
|
||||||
|
-- NULL AS MARKET_RATIO,
|
||||||
|
COUNTINGUNIT,
|
||||||
|
NULL AS VBP_BRAND,
|
||||||
|
NULL AS REPLENISH_FALG,
|
||||||
|
FROM_UTC_TIMESTAMP(CURRENT_TIMESTAMP(),'UTC+8') AS ETL_INSERT_DT,
|
||||||
|
FROM_UTC_TIMESTAMP(CURRENT_TIMESTAMP(),'UTC+8') AS ETL_UPDATE_DT
|
||||||
|
FROM DM.DM_CHC_PACK_PROPERTY;
|
||||||
BIN
EXTERNAL/CHC/03 DM_TD_EXT_CHC_PACK_PROPERTY.sql:Zone.Identifier
Normal file
BIN
EXTERNAL/CHC/03 DM_TD_EXT_CHC_PACK_PROPERTY.sql:Zone.Identifier
Normal file
Binary file not shown.
327
EXTERNAL/CHC/04 DM_TD_EXT_CHC_PACK2MARKET.sql
Normal file
327
EXTERNAL/CHC/04 DM_TD_EXT_CHC_PACK2MARKET.sql
Normal file
@@ -0,0 +1,327 @@
|
|||||||
|
-- Databricks notebook source
|
||||||
|
-- CREATE OR REPLACE TABLE DM.DM_TD_EXT_CHC_PACK2MARKET (
|
||||||
|
-- MARKET STRING,
|
||||||
|
-- PACK_CODE STRING,
|
||||||
|
-- PACK_DESC STRING,
|
||||||
|
-- STGH_DESC STRING,
|
||||||
|
-- PACK_LCH STRING,
|
||||||
|
-- PROD_CODE STRING,
|
||||||
|
-- CMPS_CODE STRING,
|
||||||
|
-- CMPS_DESC STRING,
|
||||||
|
-- ATC1_CODE STRING,
|
||||||
|
-- ATC2_CODE STRING,
|
||||||
|
-- ATC3_CODE STRING,
|
||||||
|
-- ATC4_CODE STRING,
|
||||||
|
-- APP1_CODE STRING,
|
||||||
|
-- APP2_CODE STRING,
|
||||||
|
-- APP3_CODE STRING,
|
||||||
|
-- BIO_DESC STRING,
|
||||||
|
-- GENE_ORIG_DESC STRING,
|
||||||
|
-- ETH_OTC_DESC STRING,
|
||||||
|
-- NRDL_DESC STRING,
|
||||||
|
-- NRDL_ENTRY_DATE STRING,
|
||||||
|
-- EDL_DESC STRING,
|
||||||
|
-- TCM_DESC STRING,
|
||||||
|
-- PAED_DESC STRING,
|
||||||
|
-- GQCE_DESC STRING,
|
||||||
|
-- VBP_DESC STRING,
|
||||||
|
-- MANU_CODE STRING,
|
||||||
|
-- MANU_DESC STRING,
|
||||||
|
-- MNFL_CODE STRING,
|
||||||
|
-- MNFL_DESC STRING,
|
||||||
|
-- CORP_CODE STRING,
|
||||||
|
-- CORP_DESC STRING,
|
||||||
|
-- BRANDTYPE STRING,
|
||||||
|
-- BU STRING,
|
||||||
|
-- STARTTIME STRING,
|
||||||
|
-- ENDTIME STRING,
|
||||||
|
-- MARKET_RATIO STRING,
|
||||||
|
-- ETL_INSERT_DT TIMESTAMP,
|
||||||
|
-- ETL_UPDATE_DT TIMESTAMP
|
||||||
|
-- )
|
||||||
|
-- USING delta
|
||||||
|
-- -- LOCATION 'abfss://master@azcdatalakeprd.dfs.core.chinacloudapi.cn/DM/dm_td_ext_chc_pack2market';
|
||||||
|
-- -- 上面是生产环境location,下面是测试环境location
|
||||||
|
-- LOCATION 'abfss://master@retaildlstoragetest.dfs.core.chinacloudapi.cn/DM/dm_td_ext_chc_pack2market';
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
-- CREATE OR REPLACE TABLE DM.DM_TD_EXT_chc_PACK2MARKET_KEYCOMPETITOR (
|
||||||
|
-- KEY_COMPETITOR STRING,
|
||||||
|
-- MARKET STRING,
|
||||||
|
-- PACK_CODE STRING)
|
||||||
|
-- USING delta
|
||||||
|
-- -- LOCATION 'abfss://master@azcdatalakeprd.dfs.core.chinacloudapi.cn/DM/dm_td_ext_chc_pack2market_keycompetitor';
|
||||||
|
-- -- 上面是生产环境location,下面是测试环境location
|
||||||
|
-- LOCATION 'abfss://master@retaildlstoragetest.dfs.core.chinacloudapi.cn/DM/dm_td_ext_chc_pack2market_keycompetitor';
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
-- CREATE or REPLACE TABLE DM.DM_TD_EXT_chc_PACK2MARKET_CLASS (
|
||||||
|
-- MARKET STRING,
|
||||||
|
-- PACK_CODE STRING,
|
||||||
|
-- CLASS STRING)
|
||||||
|
-- USING delta
|
||||||
|
-- -- LOCATION 'abfss://master@azcdatalakeprd.dfs.core.chinacloudapi.cn/DM/dm_td_ext_chc_pack2market_class';
|
||||||
|
-- -- 上面是生产环境location,下面是测试环境location
|
||||||
|
-- LOCATION 'abfss://master@retaildlstoragetest.dfs.core.chinacloudapi.cn/DM/dm_td_ext_chc_pack2market_class';
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
-- 使用 EXCEPT(集合差操作)简化逻辑
|
||||||
|
INSERT OVERWRITE TABLE DM.DM_TD_EXT_CHC_PACK2MARKET
|
||||||
|
-- 包含规则
|
||||||
|
SELECT DISTINCT
|
||||||
|
T2.MARKET,
|
||||||
|
T1.PACK_CODE,
|
||||||
|
T1.PACK_DESC,
|
||||||
|
T1.STGH_DESC,
|
||||||
|
T1.PACK_LCH,
|
||||||
|
T1.PROD_CODE,
|
||||||
|
T1.CMPS_CODE,
|
||||||
|
T1.CMPS_DESC,
|
||||||
|
T1.ATC1_CODE,
|
||||||
|
T1.ATC2_CODE,
|
||||||
|
T1.ATC3_CODE,
|
||||||
|
T1.ATC4_CODE,
|
||||||
|
T1.APP1_CODE,
|
||||||
|
T1.APP2_CODE,
|
||||||
|
T1.APP3_CODE,
|
||||||
|
T1.BIO_DESC,
|
||||||
|
T1.GENE_ORIG_DESC AS GENE_ORIG,
|
||||||
|
T1.ETH_OTC_DESC AS ETH_OTC_D,
|
||||||
|
T1.NRDL_DESC,
|
||||||
|
T1.NRDL_ENTRY_DATE AS NRDL_ENTR,
|
||||||
|
T1.EDL_DESC,
|
||||||
|
T1.TCM_DESC,
|
||||||
|
T1.PAED_DESC,
|
||||||
|
T1.GQCE_DESC,
|
||||||
|
T1.VBP_DESC,
|
||||||
|
T1.MANU_CODE,
|
||||||
|
T1.MANU_DESC,
|
||||||
|
T1.MNFL_CODE,
|
||||||
|
T1.MNFL_DESC,
|
||||||
|
T1.CORP_CODE,
|
||||||
|
T1.CORP_DESC,
|
||||||
|
T1.BRANDTYPE,
|
||||||
|
T2.BU,
|
||||||
|
T2.STARTTIME,
|
||||||
|
T2.ENDTIME,
|
||||||
|
COALESCE(T2.EXTEND_MARKET_RATIO, '1') AS MARKET_RATIO,
|
||||||
|
FROM_UTC_TIMESTAMP(CURRENT_TIMESTAMP(),'UTC+8') AS ETL_INSERT_DT,
|
||||||
|
FROM_UTC_TIMESTAMP(CURRENT_TIMESTAMP(),'UTC+8') AS ETL_UPDATE_DT
|
||||||
|
FROM DM.DM_TD_EXT_CHC_PACK_PROPERTY AS T1
|
||||||
|
LEFT JOIN (
|
||||||
|
SELECT
|
||||||
|
MARKET,
|
||||||
|
BU,
|
||||||
|
STARTTIME,
|
||||||
|
ENDTIME,
|
||||||
|
EXTEND_MARKET_RATIO,
|
||||||
|
ATC1_CODE,
|
||||||
|
ATC2_CODE,
|
||||||
|
ATC3_CODE,
|
||||||
|
ATC4_CODE,
|
||||||
|
NFC1_CODE,
|
||||||
|
NFC2_CODE,
|
||||||
|
NFC3_CODE,
|
||||||
|
CORPORATION_CODE,
|
||||||
|
MANUFACTURER_CODE,
|
||||||
|
PRODUCT_CODE,
|
||||||
|
PACK_CODE,
|
||||||
|
STRENGTH,
|
||||||
|
MOLECULE_CODE
|
||||||
|
FROM DWD.DWD_GND_IMS_TBLMARKET_BYMONTH
|
||||||
|
WHERE EXTEND_MARKET IS NULL AND (NOT_IN_FLAG IS NULL OR NOT_IN_FLAG = '1')
|
||||||
|
) AS T2
|
||||||
|
ON (T2.ATC1_CODE IS NULL OR T1.ATC1_CODE = T2.ATC1_CODE)
|
||||||
|
AND (T2.ATC2_CODE IS NULL OR T1.ATC2_CODE = T2.ATC2_CODE)
|
||||||
|
AND (T2.ATC3_CODE IS NULL OR T1.ATC3_CODE = T2.ATC3_CODE)
|
||||||
|
AND (T2.ATC4_CODE IS NULL OR T1.ATC4_CODE = T2.ATC4_CODE)
|
||||||
|
AND (T2.NFC1_CODE IS NULL OR T1.APP1_CODE = T2.NFC1_CODE)
|
||||||
|
AND (T2.NFC2_CODE IS NULL OR T1.APP2_CODE = T2.NFC2_CODE)
|
||||||
|
AND (T2.NFC3_CODE IS NULL OR T1.APP3_CODE = T2.NFC3_CODE)
|
||||||
|
AND (T2.CORPORATION_CODE IS NULL OR T1.CORP_CODE = T2.CORPORATION_CODE)
|
||||||
|
AND (T2.MANUFACTURER_CODE IS NULL OR T1.MANU_CODE = T2.MANUFACTURER_CODE)
|
||||||
|
AND (T2.PRODUCT_CODE IS NULL OR T1.PROD_CODE = T2.PRODUCT_CODE)
|
||||||
|
AND (T2.PACK_CODE IS NULL OR T1.PACK_CODE = T2.PACK_CODE)
|
||||||
|
AND (T2.STRENGTH IS NULL OR T1.STGH_DESC = T2.STRENGTH)
|
||||||
|
AND (T2.MOLECULE_CODE IS NULL OR T1.CMPS_CODE = T2.MOLECULE_CODE)
|
||||||
|
WHERE T2.MARKET IS NOT NULL
|
||||||
|
|
||||||
|
EXCEPT
|
||||||
|
|
||||||
|
-- 排除规则
|
||||||
|
SELECT DISTINCT
|
||||||
|
T2.MARKET,
|
||||||
|
T1.PACK_CODE,
|
||||||
|
T1.PACK_DESC,
|
||||||
|
T1.STGH_DESC,
|
||||||
|
T1.PACK_LCH,
|
||||||
|
T1.PROD_CODE,
|
||||||
|
T1.CMPS_CODE,
|
||||||
|
T1.CMPS_DESC,
|
||||||
|
T1.ATC1_CODE,
|
||||||
|
T1.ATC2_CODE,
|
||||||
|
T1.ATC3_CODE,
|
||||||
|
T1.ATC4_CODE,
|
||||||
|
T1.APP1_CODE,
|
||||||
|
T1.APP2_CODE,
|
||||||
|
T1.APP3_CODE,
|
||||||
|
T1.BIO_DESC,
|
||||||
|
T1.GENE_ORIG_DESC AS GENE_ORIG,
|
||||||
|
T1.ETH_OTC_DESC AS ETH_OTC_D,
|
||||||
|
T1.NRDL_DESC,
|
||||||
|
T1.NRDL_ENTRY_DATE AS NRDL_ENTR,
|
||||||
|
T1.EDL_DESC,
|
||||||
|
T1.TCM_DESC,
|
||||||
|
T1.PAED_DESC,
|
||||||
|
T1.GQCE_DESC,
|
||||||
|
T1.VBP_DESC,
|
||||||
|
T1.MANU_CODE,
|
||||||
|
T1.MANU_DESC,
|
||||||
|
T1.MNFL_CODE,
|
||||||
|
T1.MNFL_DESC,
|
||||||
|
T1.CORP_CODE,
|
||||||
|
T1.CORP_DESC,
|
||||||
|
T1.BRANDTYPE,
|
||||||
|
T2.BU,
|
||||||
|
T2.STARTTIME,
|
||||||
|
T2.ENDTIME,
|
||||||
|
COALESCE(T2.EXTEND_MARKET_RATIO, '1') AS MARKET_RATIO,
|
||||||
|
FROM_UTC_TIMESTAMP(CURRENT_TIMESTAMP(),'UTC+8') AS ETL_INSERT_DT,
|
||||||
|
FROM_UTC_TIMESTAMP(CURRENT_TIMESTAMP(),'UTC+8') AS ETL_UPDATE_DT
|
||||||
|
FROM DM.DM_TD_EXT_CHC_PACK_PROPERTY AS T1
|
||||||
|
LEFT JOIN (
|
||||||
|
SELECT
|
||||||
|
MARKET,
|
||||||
|
BU,
|
||||||
|
STARTTIME,
|
||||||
|
ENDTIME,
|
||||||
|
EXTEND_MARKET_RATIO,
|
||||||
|
ATC1_CODE,
|
||||||
|
ATC2_CODE,
|
||||||
|
ATC3_CODE,
|
||||||
|
ATC4_CODE,
|
||||||
|
NFC1_CODE,
|
||||||
|
NFC2_CODE,
|
||||||
|
NFC3_CODE,
|
||||||
|
CORPORATION_CODE,
|
||||||
|
MANUFACTURER_CODE,
|
||||||
|
PRODUCT_CODE,
|
||||||
|
PACK_CODE,
|
||||||
|
STRENGTH,
|
||||||
|
MOLECULE_CODE
|
||||||
|
FROM DWD.DWD_GND_IMS_TBLMARKET_BYMONTH
|
||||||
|
WHERE EXTEND_MARKET IS NULL AND NOT_IN_FLAG = '0'
|
||||||
|
) AS T2
|
||||||
|
ON (T2.ATC1_CODE IS NULL OR T1.ATC1_CODE = T2.ATC1_CODE)
|
||||||
|
AND (T2.ATC2_CODE IS NULL OR T1.ATC2_CODE = T2.ATC2_CODE)
|
||||||
|
AND (T2.ATC3_CODE IS NULL OR T1.ATC3_CODE = T2.ATC3_CODE)
|
||||||
|
AND (T2.ATC4_CODE IS NULL OR T1.ATC4_CODE = T2.ATC4_CODE)
|
||||||
|
AND (T2.NFC1_CODE IS NULL OR T1.APP1_CODE = T2.NFC1_CODE)
|
||||||
|
AND (T2.NFC2_CODE IS NULL OR T1.APP2_CODE = T2.NFC2_CODE)
|
||||||
|
AND (T2.NFC3_CODE IS NULL OR T1.APP3_CODE = T2.NFC3_CODE)
|
||||||
|
AND (T2.CORPORATION_CODE IS NULL OR T1.CORP_CODE = T2.CORPORATION_CODE)
|
||||||
|
AND (T2.MANUFACTURER_CODE IS NULL OR T1.MANU_CODE = T2.MANUFACTURER_CODE)
|
||||||
|
AND (T2.PRODUCT_CODE IS NULL OR T1.PROD_CODE = T2.PRODUCT_CODE)
|
||||||
|
AND (T2.PACK_CODE IS NULL OR T1.PACK_CODE = T2.PACK_CODE)
|
||||||
|
AND (T2.STRENGTH IS NULL OR T1.STGH_DESC = T2.STRENGTH)
|
||||||
|
AND (T2.MOLECULE_CODE IS NULL OR T1.CMPS_CODE = T2.MOLECULE_CODE)
|
||||||
|
WHERE T2.MARKET IS NOT NULL;
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
INSERT OVERWRITE TABLE DM.DM_TD_EXT_CHC_PACK2MARKET_KEYCOMPETITOR
|
||||||
|
WITH kc AS (
|
||||||
|
SELECT
|
||||||
|
keycompetitor, no,
|
||||||
|
ROW_NUMBER() OVER (ORDER BY
|
||||||
|
CASE
|
||||||
|
WHEN ATC1_Code IS NOT NULL THEN 1
|
||||||
|
WHEN ATC2_Code IS NOT NULL THEN 2
|
||||||
|
WHEN ATC3_Code IS NOT NULL THEN 3
|
||||||
|
WHEN ATC4_Code IS NOT NULL THEN 4
|
||||||
|
WHEN Molecule_Code IS NOT NULL THEN 5
|
||||||
|
WHEN Product_Code IS NOT NULL THEN 6
|
||||||
|
WHEN Pack_Code IS NOT NULL THEN 7
|
||||||
|
ELSE 999
|
||||||
|
END,
|
||||||
|
CASE WHEN NFC1_CODE IS NULL THEN 0 ELSE 1 END,
|
||||||
|
CASE WHEN NFC2_CODE IS NULL THEN 0 ELSE 1 END,
|
||||||
|
CASE WHEN NFC3_CODE IS NULL THEN 0 ELSE 1 END
|
||||||
|
) AS no1,
|
||||||
|
ATC1_Code, ATC2_Code, ATC3_Code, ATC4_Code,
|
||||||
|
NFC1_Code, NFC2_Code, NFC3_Code,
|
||||||
|
corporation_code, Manufacturer_Code,
|
||||||
|
Product_Code, Pack_Code, Strength, Molecule_Code, market
|
||||||
|
FROM dwd.dwd_gnd_ims_tblkeycompetitor
|
||||||
|
),
|
||||||
|
joined AS (
|
||||||
|
SELECT
|
||||||
|
/*+ BROADCAST(kc) */
|
||||||
|
COALESCE(kc.keycompetitor, 'OTHERS') AS Key_Competitor, -- ✅ UPDATE 逻辑内联
|
||||||
|
t1.market, t1.PACK_CODE
|
||||||
|
/*bug 调试区
|
||||||
|
,
|
||||||
|
kc.no,
|
||||||
|
kc.no1,
|
||||||
|
t1.PROD_COD, t1.CMPS_COD, t1.CMPS_DES,
|
||||||
|
t1.ATC1_COD, t1.ATC2_COD, t1.ATC3_COD, t1.ATC4_COD,
|
||||||
|
t1.APP1_COD, t1.APP2_COD, t1.APP3_COD,
|
||||||
|
t1.BIO_DESC, t1.GENE_ORIG_DESC, t1.ETH_OTC_DESC,
|
||||||
|
t1.NRDL_DESC, t1.NRDL_Entry_Date, t1.EDL_DESC, t1.TCM_DESC,
|
||||||
|
t1.PAED_DESC, t1.GQCE_DESC, t1.VBP_DESC,
|
||||||
|
t1.MANU_COD, t1.MANU_DES, t1.MNFL_COD, t1.MNFL_DES,
|
||||||
|
t1.CORP_COD, t1.CORP_DES, t1.BrandType, t1.bu, t1.Market_Ratio
|
||||||
|
*/
|
||||||
|
,ROW_NUMBER() OVER (
|
||||||
|
PARTITION BY t1.market, t1.PACK_CODE, t1.PROD_CODE
|
||||||
|
ORDER BY kc.no1 DESC, kc.no DESC
|
||||||
|
) AS _rn
|
||||||
|
FROM DM.DM_TD_EXT_CHC_PACK2MARKET t1
|
||||||
|
LEFT JOIN kc
|
||||||
|
ON (kc.ATC1_Code IS NULL OR t1.ATC1_CODE = kc.ATC1_Code)
|
||||||
|
AND (kc.ATC2_Code IS NULL OR t1.ATC2_CODE = kc.ATC2_Code)
|
||||||
|
AND (kc.ATC3_Code IS NULL OR t1.ATC3_CODE = kc.ATC3_Code)
|
||||||
|
AND (kc.ATC4_Code IS NULL OR t1.ATC4_CODE = kc.ATC4_Code)
|
||||||
|
AND (kc.NFC1_Code IS NULL OR t1.APP1_CODE = kc.NFC1_Code)
|
||||||
|
AND (kc.NFC2_Code IS NULL OR t1.APP2_CODE = kc.NFC2_Code)
|
||||||
|
AND (kc.NFC3_Code IS NULL OR t1.APP3_CODE = kc.NFC3_Code)
|
||||||
|
AND (kc.corporation_code IS NULL OR t1.CORP_CODE = kc.corporation_code)
|
||||||
|
AND (kc.Manufacturer_Code IS NULL OR t1.MANU_CODE = kc.Manufacturer_Code)
|
||||||
|
AND (kc.Product_Code IS NULL OR t1.PROD_CODE = kc.Product_Code)
|
||||||
|
AND (kc.Pack_Code IS NULL OR t1.PACK_CODE = kc.Pack_Code)
|
||||||
|
AND (kc.Strength IS NULL OR t1.STGH_DESC = kc.Strength)
|
||||||
|
AND (kc.Molecule_Code IS NULL OR t1.CMPS_CODE = kc.Molecule_Code)
|
||||||
|
AND (kc.market IS NULL OR t1.market = kc.market)
|
||||||
|
)
|
||||||
|
SELECT distinct Key_Competitor,market,pack_code
|
||||||
|
FROM joined
|
||||||
|
WHERE _rn = 1;
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
INSERT OVERWRITE TABLE DM.DM_TD_EXT_CHC_PACK2MARKET_CLASS
|
||||||
|
SELECT
|
||||||
|
/*+ BROADCAST(T2) */
|
||||||
|
DISTINCT
|
||||||
|
T1.MARKET,
|
||||||
|
T1.PACK_CODE,
|
||||||
|
COALESCE( T2.CLASS, 'OTHERS') AS CLASS
|
||||||
|
FROM DM.DM_TD_EXT_CHC_PACK2MARKET T1
|
||||||
|
LEFT JOIN DWD.DWD_GND_TBLCLASS T2
|
||||||
|
ON (T2.MARKET IS NULL OR T1.MARKET = T2.MARKET)
|
||||||
|
AND (T2.PACK_CODE IS NULL OR T1.PACK_CODE = T2.PACK_CODE)
|
||||||
|
AND (T2.PRODUCT_CODE IS NULL OR T1.PROD_CODE = T2.PRODUCT_CODE)
|
||||||
|
AND (T2.MOLECULE_CODE IS NULL OR T1.CMPS_CODE = T2.MOLECULE_CODE)
|
||||||
|
AND (T2.CORPORATION_CODE IS NULL OR T1.CORP_CODE = T2.CORPORATION_CODE)
|
||||||
|
AND (T2.MANUFACTURER_CODE IS NULL OR T1.MANU_CODE = T2.MANUFACTURER_CODE)
|
||||||
|
AND (T2.ATC1_CODE IS NULL OR T1.ATC1_CODE = T2.ATC1_CODE)
|
||||||
|
AND (T2.ATC2_CODE IS NULL OR T1.ATC2_CODE = T2.ATC2_CODE)
|
||||||
|
AND (T2.ATC3_CODE IS NULL OR T1.ATC3_CODE = T2.ATC3_CODE)
|
||||||
|
AND (T2.ATC4_CODE IS NULL OR T1.ATC4_CODE = T2.ATC4_CODE)
|
||||||
|
AND (T2.NFC1_CODE IS NULL OR T1.APP1_CODE = T2.NFC1_CODE)
|
||||||
|
AND (T2.NFC2_CODE IS NULL OR T1.APP2_CODE = T2.NFC2_CODE)
|
||||||
|
AND (T2.NFC3_CODE IS NULL OR T1.APP3_CODE = T2.NFC3_CODE)
|
||||||
|
AND (T2.STRENGTH IS NULL OR T1.STGH_DESC = T2.STRENGTH)
|
||||||
BIN
EXTERNAL/CHC/04 DM_TD_EXT_CHC_PACK2MARKET.sql:Zone.Identifier
Normal file
BIN
EXTERNAL/CHC/04 DM_TD_EXT_CHC_PACK2MARKET.sql:Zone.Identifier
Normal file
Binary file not shown.
30
EXTERNAL/CHC/05 DM_TD_EXT_CHC_MARKET_RATIO.sql
Normal file
30
EXTERNAL/CHC/05 DM_TD_EXT_CHC_MARKET_RATIO.sql
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
-- Databricks notebook source
|
||||||
|
-- CREATE OR REPLACE TABLE DM.DM_TD_EXT_CHC_MARKET_RATIO (
|
||||||
|
-- MARKET STRING,
|
||||||
|
-- PACK_CODE STRING,
|
||||||
|
-- CMPS_CODE STRING,
|
||||||
|
-- STARTTIME STRING,
|
||||||
|
-- ENDTIME STRING,
|
||||||
|
-- MARKET_RATIO STRING,
|
||||||
|
-- ETL_INSERT_DT TIMESTAMP,
|
||||||
|
-- ETL_UPDATE_DT TIMESTAMP)
|
||||||
|
-- USING delta
|
||||||
|
-- -- LOCATION 'abfss://master@azcdatalakeprd.dfs.core.chinacloudapi.cn/DM/dm_td_ext_chc_market_ratio';
|
||||||
|
-- -- 上面是生产环境location,下面是测试环境location
|
||||||
|
-- LOCATION 'abfss://master@retaildlstoragetest.dfs.core.chinacloudapi.cn/DM/dm_td_ext_chc_market_ratio';
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
INSERT OVERWRITE TABLE DM.DM_TD_EXT_CHC_MARKET_RATIO
|
||||||
|
SELECT
|
||||||
|
DISTINCT
|
||||||
|
MARKET,
|
||||||
|
PACK_CODE,
|
||||||
|
CMPS_CODE,
|
||||||
|
STARTTIME,
|
||||||
|
ENDTIME,
|
||||||
|
MARKET_RATIO,
|
||||||
|
FROM_UTC_TIMESTAMP(CURRENT_TIMESTAMP(),'UTC+8') AS ETL_INSERT_DT,
|
||||||
|
FROM_UTC_TIMESTAMP(CURRENT_TIMESTAMP(),'UTC+8') AS ETL_UPDATE_DT
|
||||||
|
FROM DM.DM_TD_EXT_CHC_PACK2MARKET
|
||||||
|
WHERE UPPER(MARKET) NOT LIKE '%ALL%'
|
||||||
BIN
EXTERNAL/CHC/05 DM_TD_EXT_CHC_MARKET_RATIO.sql:Zone.Identifier
Normal file
BIN
EXTERNAL/CHC/05 DM_TD_EXT_CHC_MARKET_RATIO.sql:Zone.Identifier
Normal file
Binary file not shown.
163
EXTERNAL/CHC/06 DM_TD_EXT_CHC_MARKET_BRAND_RATIO.sql
Normal file
163
EXTERNAL/CHC/06 DM_TD_EXT_CHC_MARKET_BRAND_RATIO.sql
Normal file
@@ -0,0 +1,163 @@
|
|||||||
|
-- Databricks notebook source
|
||||||
|
-- CREATE OR REPLACE TABLE DM.DM_TD_EXT_CHC_MARKET_BRAND_RATIO (
|
||||||
|
-- MARKET STRING,
|
||||||
|
-- PACK_CODE STRING,
|
||||||
|
-- VALUE_BRAND_RATIO FLOAT,
|
||||||
|
-- VALUE_BRAND_RATIO_START STRING,
|
||||||
|
-- VALUE_BRAND_RATIO_END STRING,
|
||||||
|
-- UNIT_BRAND_RATIO FLOAT,
|
||||||
|
-- UNIT_BRAND_START STRING,
|
||||||
|
-- UNIT_BRAND_END STRING,
|
||||||
|
-- COUNTINGUNIT_BRAND_RATIO FLOAT,
|
||||||
|
-- COUNTINGUNIT_BRAND_START STRING,
|
||||||
|
-- COUNTINGUNIT_BRAND_END STRING,
|
||||||
|
-- PDOT_BRAND_RATIO FLOAT,
|
||||||
|
-- PDOT_BRAND_START STRING,
|
||||||
|
-- PDOT_BRAND_END STRING,
|
||||||
|
-- ETL_INSERT_DT TIMESTAMP,
|
||||||
|
-- ETL_UPDATE_DT TIMESTAMP)
|
||||||
|
-- USING delta
|
||||||
|
-- -- LOCATION 'abfss://master@azcdatalakeprd.dfs.core.chinacloudapi.cn/DM/dm_td_ext_chc_market_brand_ratio';
|
||||||
|
-- -- 上面是生产环境location,下面是测试环境location
|
||||||
|
-- LOCATION 'abfss://master@retaildlstoragetest.dfs.core.chinacloudapi.cn/DM/dm_td_ext_chc_market_brand_ratio';
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
INSERT OVERWRITE TABLE DM.DM_TD_EXT_CHC_MARKET_BRAND_RATIO
|
||||||
|
SELECT
|
||||||
|
DISTINCT
|
||||||
|
T1.MARKET,
|
||||||
|
T1.PACK_CODE,
|
||||||
|
CASE
|
||||||
|
WHEN T2.RATIO IS NOT NULL THEN T2.RATIO
|
||||||
|
ELSE NVL(T5.RATIO, 1) END AS VALUE_BRAND_RATIO,
|
||||||
|
CASE
|
||||||
|
WHEN T2.RATIO IS NOT NULL THEN T2.STARTTIME
|
||||||
|
ELSE NVL(T5.STARTTIME, 200001) END AS VALUE_BRAND_RATIO_START,
|
||||||
|
CASE
|
||||||
|
WHEN T2.RATIO IS NOT NULL THEN T2.ENDTIME
|
||||||
|
ELSE NVL(T5.ENDTIME, 299912) END AS VALUE_BRAND_RATIO_END,
|
||||||
|
CASE
|
||||||
|
WHEN T3.RATIO IS NOT NULL THEN T3.RATIO
|
||||||
|
ELSE NVL(T6.RATIO, 1) END AS UNIT_BRAND_RATIO,
|
||||||
|
CASE
|
||||||
|
WHEN T3.RATIO IS NOT NULL THEN T3.STARTTIME
|
||||||
|
ELSE NVL(T6.STARTTIME, 200001) END AS UNIT_BRAND_START,
|
||||||
|
CASE
|
||||||
|
WHEN T3.RATIO IS NOT NULL THEN T3.ENDTIME
|
||||||
|
ELSE NVL(T6.ENDTIME, 299912) END AS UNIT_BRAND_END,
|
||||||
|
1 AS COUNTINGUNIT_BRAND_RATIO,
|
||||||
|
200001 AS COUNTINGUNIT_BRAND_START,
|
||||||
|
299912 AS COUNTINGUNIT_BRAND_END,
|
||||||
|
CASE
|
||||||
|
WHEN T4.RATIO IS NOT NULL THEN T4.RATIO
|
||||||
|
ELSE NVL(T7.RATIO, 1) END AS PDOT_BRAND_RATIO,
|
||||||
|
CASE
|
||||||
|
WHEN T4.RATIO IS NOT NULL THEN T4.STARTTIME
|
||||||
|
ELSE NVL(T7.STARTTIME, 200001) END AS PDOT_BRAND_START,
|
||||||
|
CASE
|
||||||
|
WHEN T4.RATIO IS NOT NULL THEN T4.ENDTIME
|
||||||
|
ELSE NVL(T7.ENDTIME, 299912) END AS PDOT_BRAND_END,
|
||||||
|
FROM_UTC_TIMESTAMP(CURRENT_TIMESTAMP(),'UTC+8') AS ETL_INSERT_DT,
|
||||||
|
FROM_UTC_TIMESTAMP(CURRENT_TIMESTAMP(),'UTC+8') AS ETL_UPDATE_DT
|
||||||
|
FROM DM.DM_TD_EXT_CHC_MARKET_RATIO T1
|
||||||
|
LEFT JOIN ( --VALUE--PACK
|
||||||
|
SELECT
|
||||||
|
DISTINCT
|
||||||
|
MARKET,
|
||||||
|
CMPS_COD,
|
||||||
|
CMPS_DES,
|
||||||
|
PACK_COD,
|
||||||
|
PACK_DES,
|
||||||
|
LEVEL,
|
||||||
|
TYPE,
|
||||||
|
CAST(RATIO AS FLOAT) AS RATIO,
|
||||||
|
CAST(STARTTIME AS INT) STARTTIME,
|
||||||
|
CAST(ENDTIME AS INT) ENDTIME
|
||||||
|
FROM DWD.DWD_GND_IMS_TBLBRANDRATIO_BYMONTH
|
||||||
|
WHERE UPPER(TYPE) = 'VALUE' AND UPPER(LEVEL) = 'PACK'
|
||||||
|
) T2 ON T1.MARKET = T2.MARKET AND T1.PACK_CODE = T2.PACK_COD
|
||||||
|
LEFT JOIN ( --UNIT--PACK
|
||||||
|
SELECT
|
||||||
|
DISTINCT
|
||||||
|
MARKET,
|
||||||
|
CMPS_COD,
|
||||||
|
CMPS_DES,
|
||||||
|
PACK_COD,
|
||||||
|
PACK_DES,
|
||||||
|
LEVEL,
|
||||||
|
TYPE,
|
||||||
|
CAST(RATIO AS FLOAT) AS RATIO,
|
||||||
|
CAST(STARTTIME AS INT) STARTTIME,
|
||||||
|
CAST(ENDTIME AS INT) ENDTIME
|
||||||
|
FROM DWD.DWD_GND_IMS_TBLBRANDRATIO_BYMONTH
|
||||||
|
WHERE UPPER(TYPE) = 'UNIT' AND UPPER(LEVEL) = 'PACK'
|
||||||
|
) T3 ON T1.MARKET = T3.MARKET AND T1.PACK_CODE = T3.PACK_COD
|
||||||
|
LEFT JOIN ( --COUNTINGUNIT--PACK
|
||||||
|
SELECT
|
||||||
|
DISTINCT
|
||||||
|
MARKET,
|
||||||
|
CMPS_COD,
|
||||||
|
CMPS_DES,
|
||||||
|
PACK_COD,
|
||||||
|
PACK_DES,
|
||||||
|
LEVEL,
|
||||||
|
TYPE,
|
||||||
|
CAST(RATIO AS FLOAT) AS RATIO,
|
||||||
|
CAST(STARTTIME AS INT) STARTTIME,
|
||||||
|
CAST(ENDTIME AS INT) ENDTIME
|
||||||
|
FROM DWD.DWD_GND_IMS_TBLBRANDRATIO_BYMONTH
|
||||||
|
WHERE UPPER(TYPE) = 'COUNTINGUNIT' AND UPPER(LEVEL) = 'PACK'
|
||||||
|
) T4 ON T1.MARKET = T4.MARKET AND T1.PACK_CODE = T4.PACK_COD
|
||||||
|
LEFT JOIN ( --VALUE--MOLECULE
|
||||||
|
SELECT
|
||||||
|
DISTINCT
|
||||||
|
MARKET,
|
||||||
|
CMPS_COD,
|
||||||
|
CMPS_DES,
|
||||||
|
PACK_COD,
|
||||||
|
PACK_DES,
|
||||||
|
LEVEL,
|
||||||
|
TYPE,
|
||||||
|
CAST(RATIO AS FLOAT) AS RATIO,
|
||||||
|
CAST(STARTTIME AS INT) STARTTIME,
|
||||||
|
CAST(ENDTIME AS INT) ENDTIME
|
||||||
|
FROM DWD.DWD_GND_IMS_TBLBRANDRATIO_BYMONTH
|
||||||
|
WHERE UPPER(TYPE) = 'VALUE' AND UPPER(LEVEL) = 'MOLECULE'
|
||||||
|
) T5 ON T1.MARKET = T5.MARKET AND T1.CMPS_CODE = T5.CMPS_COD
|
||||||
|
LEFT JOIN ( --UNIT--MOLECULE
|
||||||
|
SELECT
|
||||||
|
DISTINCT
|
||||||
|
MARKET,
|
||||||
|
CMPS_COD,
|
||||||
|
CMPS_DES,
|
||||||
|
PACK_COD,
|
||||||
|
PACK_DES,
|
||||||
|
LEVEL,
|
||||||
|
TYPE,
|
||||||
|
CAST(RATIO AS FLOAT) AS RATIO,
|
||||||
|
CAST(STARTTIME AS INT) STARTTIME,
|
||||||
|
CAST(ENDTIME AS INT) ENDTIME
|
||||||
|
FROM DWD.DWD_GND_IMS_TBLBRANDRATIO_BYMONTH
|
||||||
|
WHERE UPPER(TYPE) = 'UNIT' AND UPPER(LEVEL) = 'MOLECULE'
|
||||||
|
) T6 ON T1.MARKET = T6.MARKET AND T1.CMPS_CODE = T6.CMPS_COD
|
||||||
|
LEFT JOIN ( --COUNTINGUNIT--MOLECULE
|
||||||
|
SELECT
|
||||||
|
DISTINCT
|
||||||
|
MARKET,
|
||||||
|
CMPS_COD,
|
||||||
|
CMPS_DES,
|
||||||
|
PACK_COD,
|
||||||
|
PACK_DES,
|
||||||
|
LEVEL,
|
||||||
|
TYPE,
|
||||||
|
CAST(RATIO AS FLOAT) AS RATIO,
|
||||||
|
CAST(STARTTIME AS INT) STARTTIME,
|
||||||
|
CAST(ENDTIME AS INT) ENDTIME
|
||||||
|
FROM DWD.DWD_GND_IMS_TBLBRANDRATIO_BYMONTH
|
||||||
|
WHERE UPPER(TYPE) = 'COUNTINGUNIT' AND UPPER(LEVEL) = 'MOLECULE'
|
||||||
|
) T7 ON T1.MARKET = T7.MARKET
|
||||||
|
AND T1.CMPS_CODE = T7.CMPS_COD
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
Binary file not shown.
71
EXTERNAL/CHC/07 DM_TD_EXT_CHC_MARKET_PACK_MAPPING.sql
Normal file
71
EXTERNAL/CHC/07 DM_TD_EXT_CHC_MARKET_PACK_MAPPING.sql
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
-- Databricks notebook source
|
||||||
|
-- CREATE OR REPLACE TABLE DM.DM_TD_EXT_CHC_MARKET_PACK_MAPPING
|
||||||
|
-- (
|
||||||
|
-- PACK_CODE STRING,
|
||||||
|
-- MARKET STRING,
|
||||||
|
-- CLASS STRING,
|
||||||
|
-- KEY_COMPETITOR STRING,
|
||||||
|
-- MARKET_RATIO STRING,
|
||||||
|
-- STARTTIME STRING,
|
||||||
|
-- ENDTIME STRING,
|
||||||
|
-- VALUE_BRAND_RATIO STRING,
|
||||||
|
-- VALUE_BRAND_RATIO_START STRING,
|
||||||
|
-- VALUE_BRAND_RATIO_END STRING,
|
||||||
|
-- UNIT_BRAND_RATIO STRING,
|
||||||
|
-- UNIT_BRAND_START STRING,
|
||||||
|
-- UNIT_BRAND_END STRING,
|
||||||
|
-- COUNTINGUNIT_BRAND_RATIO STRING,
|
||||||
|
-- COUNTINGUNIT_BRAND_START STRING,
|
||||||
|
-- COUNTINGUNIT_BRAND_END STRING,
|
||||||
|
-- PDOT_BRAND_RATIO STRING,
|
||||||
|
-- PDOT_BRAND_START STRING,
|
||||||
|
-- PDOT_BRAND_END STRING,
|
||||||
|
-- ETL_INSERT_DT TIMESTAMP,
|
||||||
|
-- ETL_UPDATE_DT TIMESTAMP
|
||||||
|
-- )
|
||||||
|
-- USING delta
|
||||||
|
-- -- LOCATION 'abfss://master@azcdatalakeprd.dfs.core.chinacloudapi.cn/DM/dm_td_ext_chc_market_pack_mapping';
|
||||||
|
-- -- 上面是生产环境location,下面是测试环境location
|
||||||
|
-- LOCATION 'abfss://master@retaildlstoragetest.dfs.core.chinacloudapi.cn/DM/dm_td_ext_chc_market_pack_mapping';
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
INSERT OVERWRITE TABLE DM.DM_TD_EXT_CHC_MARKET_PACK_MAPPING
|
||||||
|
SELECT DISTINCT
|
||||||
|
T1.PACK_CODE,
|
||||||
|
T1.MARKET,
|
||||||
|
T4.CLASS,
|
||||||
|
T5.KEY_COMPETITOR,
|
||||||
|
T2.MARKET_RATIO,
|
||||||
|
T2.STARTTIME,
|
||||||
|
T2.ENDTIME,
|
||||||
|
T3.VALUE_BRAND_RATIO,
|
||||||
|
T3.VALUE_BRAND_RATIO_START,
|
||||||
|
T3.VALUE_BRAND_RATIO_END,
|
||||||
|
T3.UNIT_BRAND_RATIO,
|
||||||
|
T3.UNIT_BRAND_START,
|
||||||
|
T3.UNIT_BRAND_END,
|
||||||
|
T3.COUNTINGUNIT_BRAND_RATIO,
|
||||||
|
T3.COUNTINGUNIT_BRAND_START,
|
||||||
|
T3.COUNTINGUNIT_BRAND_END,
|
||||||
|
T3.PDOT_BRAND_RATIO,
|
||||||
|
T3.PDOT_BRAND_START,
|
||||||
|
T3.PDOT_BRAND_END,
|
||||||
|
FROM_UTC_TIMESTAMP(CURRENT_TIMESTAMP(), 'UTC+8') AS ETL_INSERT_DT,
|
||||||
|
FROM_UTC_TIMESTAMP(CURRENT_TIMESTAMP(), 'UTC+8') AS ETL_UPDATE_DT
|
||||||
|
FROM
|
||||||
|
DM.DM_TD_EXT_CHC_PACK2MARKET T1
|
||||||
|
LEFT JOIN DM.DM_TD_EXT_CHC_MARKET_RATIO T2
|
||||||
|
ON T1.MARKET = T2.MARKET
|
||||||
|
AND T1.PACK_CODE = T2.PACK_CODE
|
||||||
|
LEFT JOIN DM.DM_TD_EXT_CHC_MARKET_BRAND_RATIO T3
|
||||||
|
ON T1.MARKET = T3.MARKET
|
||||||
|
AND T1.PACK_CODE = T3.PACK_CODE
|
||||||
|
LEFT JOIN DM.DM_TD_EXT_CHC_PACK2MARKET_CLASS T4
|
||||||
|
ON T1.PACK_CODE = T4.PACK_CODE
|
||||||
|
AND T1.MARKET = T4.MARKET
|
||||||
|
LEFT JOIN DM.DM_TD_EXT_CHC_PACK2MARKET_KEYCOMPETITOR T5
|
||||||
|
ON T1.PACK_CODE = T5.PACK_CODE
|
||||||
|
AND T1.MARKET = T5.MARKET
|
||||||
|
WHERE
|
||||||
|
UPPER(T1.MARKET) NOT LIKE "%ALL%"
|
||||||
Binary file not shown.
42
EXTERNAL/CHPA/01 dwd_ims_atc_hierarchy.sql
Normal file
42
EXTERNAL/CHPA/01 dwd_ims_atc_hierarchy.sql
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
-- Databricks notebook source
|
||||||
|
--dwd to dws
|
||||||
|
--清洗数据的操作
|
||||||
|
|
||||||
|
|
||||||
|
---处理宽维度表 202108
|
||||||
|
--PRINT(GETDATE())
|
||||||
|
|
||||||
|
---ATC 处理成宽表
|
||||||
|
|
||||||
|
insert overwrite table dwd.dwd_ims_atc_hierarchy
|
||||||
|
SELECT * --into DIM_ATC_Hierarchy
|
||||||
|
FROM (SELECT Therapeutic_ID AS ATC1_ID,
|
||||||
|
Therapeutic_Code AS ATC1_CODE,
|
||||||
|
Therapeutic_Name AS ATC1_DES
|
||||||
|
FROM dwd.dwd_ims_td_therapeutic_class ATC
|
||||||
|
WHERE ATC. Therapeutic_Level = '1') ATC1
|
||||||
|
|
||||||
|
LEFT JOIN (SELECT Therapeutic_ID AS ATC2_ID,
|
||||||
|
Therapeutic_Code AS ATC2_CODE,
|
||||||
|
Therapeutic_Name AS ATC2_DES
|
||||||
|
FROM dwd.dwd_ims_td_therapeutic_class ATC
|
||||||
|
WHERE ATC. Therapeutic_Level = '2') ATC2
|
||||||
|
ON ATC1. ATC1_CODE =
|
||||||
|
LEFT (ATC2. ATC2_CODE, 1)
|
||||||
|
|
||||||
|
LEFT JOIN (SELECT Therapeutic_ID AS ATC3_ID,
|
||||||
|
Therapeutic_Code AS ATC3_CODE,
|
||||||
|
Therapeutic_Name AS ATC3_DES
|
||||||
|
FROM dwd.dwd_ims_td_therapeutic_class ATC
|
||||||
|
WHERE ATC. Therapeutic_Level = '3') ATC3
|
||||||
|
ON ATC2. ATC2_CODE =
|
||||||
|
LEFT (ATC3. ATC3_CODE, 3)
|
||||||
|
|
||||||
|
LEFT JOIN (SELECT Therapeutic_ID AS ATC4_ID,
|
||||||
|
Therapeutic_Code AS ATC4_CODE,
|
||||||
|
Therapeutic_Name AS ATC4_DES
|
||||||
|
FROM dwd.dwd_ims_td_therapeutic_class ATC
|
||||||
|
WHERE ATC. Therapeutic_Level = '4') ATC4
|
||||||
|
ON ATC3. ATC3_CODE =
|
||||||
|
LEFT (ATC4. ATC4_CODE, 4)
|
||||||
|
;
|
||||||
BIN
EXTERNAL/CHPA/01 dwd_ims_atc_hierarchy.sql:Zone.Identifier
Normal file
BIN
EXTERNAL/CHPA/01 dwd_ims_atc_hierarchy.sql:Zone.Identifier
Normal file
Binary file not shown.
26
EXTERNAL/CHPA/01 dwd_ims_nfc_hierarchy.sql
Normal file
26
EXTERNAL/CHPA/01 dwd_ims_nfc_hierarchy.sql
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
-- Databricks notebook source
|
||||||
|
--NFC 处理成宽表
|
||||||
|
insert overwrite table dwd.dwd_ims_nfc_hierarchy
|
||||||
|
SELECT *
|
||||||
|
FROM (SELECT NewFormClass_ID as NFC1_ID,
|
||||||
|
NewFormClass_Code as NFC1_CODE,
|
||||||
|
NewFormClass_Name as NFC1_DES
|
||||||
|
FROM dwd.dwd_ims_td_new_form_class NFC
|
||||||
|
WHERE NewFormClass_Level = '1') NFC1
|
||||||
|
|
||||||
|
LEFT JOIN (SELECT NewFormClass_ID as NFC2_ID,
|
||||||
|
NewFormClass_Code as NFC2_CODE,
|
||||||
|
NewFormClass_Name as NFC2_DES
|
||||||
|
FROM dwd.dwd_ims_td_new_form_class NFC
|
||||||
|
WHERE NewFormClass_Level = '2') NFC2
|
||||||
|
ON NFC1.NFC1_CODE =
|
||||||
|
LEFT (NFC2.NFC2_CODE, 1)
|
||||||
|
|
||||||
|
LEFT JOIN (SELECT NewFormClass_ID as NFC3_ID,
|
||||||
|
NewFormClass_Code as NFC3_CODE,
|
||||||
|
NewFormClass_Name as NFC3_DES
|
||||||
|
FROM dwd.dwd_ims_td_new_form_class NFC
|
||||||
|
WHERE NewFormClass_Level = '3') NFC3
|
||||||
|
ON NFC2.NFC2_CODE =
|
||||||
|
LEFT (NFC3.NFC3_CODE, 2)
|
||||||
|
;
|
||||||
BIN
EXTERNAL/CHPA/01 dwd_ims_nfc_hierarchy.sql:Zone.Identifier
Normal file
BIN
EXTERNAL/CHPA/01 dwd_ims_nfc_hierarchy.sql:Zone.Identifier
Normal file
Binary file not shown.
24
EXTERNAL/CHPA/01 dwd_ims_td_manufacturer_corp.sql
Normal file
24
EXTERNAL/CHPA/01 dwd_ims_td_manufacturer_corp.sql
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
-- Databricks notebook source
|
||||||
|
-----------------------------------------------------------------------------------------------------------------
|
||||||
|
--修改时间:20240904
|
||||||
|
--修改人:FanXujia
|
||||||
|
--修改内容:
|
||||||
|
--id为93的数,manufacturer_abbr不是SH/了,所以不能使用这个语句更新了。
|
||||||
|
------------------------------------------------------------------------
|
||||||
|
--MANU维度表,ManufacturerType_ID中存在为0的数据,关联不出对应的ManufacturerType
|
||||||
|
--经过检查,ManufacturerType_ID = 0 的数据,其下属子集或者本身为MNC,所以将其修改成2,让报告上能正常识别成MNC
|
||||||
|
UPDATE dwd.dwd_ims_td_manufacturer SET ManufacturerType_ID = 2 WHERE ManufacturerType_ID = 0;
|
||||||
|
-- SH/的一行数据 中 / 为转义符,后面的数据不能有效识别,需要手动更新
|
||||||
|
-- update dwd.dwd_ims_td_manufacturer set manufacturer_abbr ='SH/' where manufacturer_id = '93';
|
||||||
|
-- update dwd.dwd_ims_td_manufacturer set corporation_id ='93' where manufacturer_id = '93';
|
||||||
|
-- update dwd.dwd_ims_td_manufacturer set corporation_code ='00221' where manufacturer_id = '93';
|
||||||
|
-- update dwd.dwd_ims_td_manufacturer set manufacturertype_id ='1' where manufacturer_id = '93';
|
||||||
|
-----------------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
--处理manu跟corp的mapping关系
|
||||||
|
insert overwrite table dwd.dwd_ims_td_manufacturer_corp
|
||||||
|
SELECT T1.*,T2.Corporation_Code AS CORP_MASTER_CODE,T2.Manufacturer_Abbr AS CORP_ABBR,T2.Manufacturer_Name AS CORP_DES
|
||||||
|
FROM dwd.dwd_ims_td_manufacturer T1
|
||||||
|
LEFT JOIN dwd.dwd_ims_td_manufacturer T2
|
||||||
|
ON T1.Corporation_Code = T2.Manufacturer_CODE
|
||||||
|
;
|
||||||
Binary file not shown.
89
EXTERNAL/CHPA/01 dwd_ims_td_pack_property.sql
Normal file
89
EXTERNAL/CHPA/01 dwd_ims_td_pack_property.sql
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
-- Databricks notebook source
|
||||||
|
|
||||||
|
-------使用新的DIM表,生成PackProfile,保证后续的逻辑
|
||||||
|
-- 20260119 依赖前面 dwd.dwd_ims_nfc_hierarchy、dwd.dwd_ims_atc_hierarchy、dwd.dwd_ims_td_manufacturer_corp
|
||||||
|
-- 20260119 依赖上面补0 dwd.dwd_gnd_ims_tblbrandtype
|
||||||
|
|
||||||
|
insert overwrite table dwd.dwd_ims_td_pack_property
|
||||||
|
select DISTINCT
|
||||||
|
if( PACK.Pack_Code REGEXP '^[0-9]',right(concat('000000000000',PACK.Pack_Code),12),PACK.Pack_Code) as PACK_COD, --RIGHT(concat('0000000',PACK.Pack_Code),7)
|
||||||
|
PACK.Pack_Description AS PACK_DES,
|
||||||
|
PACK.STRENGTH AS STGH_DES,
|
||||||
|
concat('Y',LEFT(PACK.LAUNCHTIME,4),'M',RIGHT(PACK.LAUNCHTIME,2)) AS PACK_LCH,
|
||||||
|
|
||||||
|
RIGHT(concat('000000000',PROD.Product_Code),9) AS PROD_COD, --RIGHT(concat('00000',PROD.Product_Code),5)
|
||||||
|
RIGHT(concat('000000',MOLE.MoleCompCode),6) AS CMPS_COD,
|
||||||
|
MOLE.MoleCompDesc AS CMPS_DES,
|
||||||
|
|
||||||
|
ATCH.ATC1_CODE AS ATC1_COD,
|
||||||
|
ATCH.ATC2_CODE AS ATC2_COD,
|
||||||
|
ATCH.ATC3_CODE AS ATC3_COD,
|
||||||
|
ATCH.ATC4_CODE AS ATC4_COD,
|
||||||
|
|
||||||
|
NFCH.NFC1_CODE AS APP1_COD,
|
||||||
|
NFCH.NFC2_CODE AS APP2_COD,
|
||||||
|
NFCH.NFC3_CODE AS APP3_COD,
|
||||||
|
|
||||||
|
MOLE.BIO AS BIO_DESC,
|
||||||
|
MOLE.Gene_Orig AS GENE_ORIG_DESC,
|
||||||
|
MOLE.Rx_Flag AS ETH_OTC_DESC,
|
||||||
|
MOLE.NRDL AS NRDL_DESC,
|
||||||
|
MOLE.NRDL_Entry_Date,
|
||||||
|
MOLE.EDL AS EDL_DESC,
|
||||||
|
MOLE.TCMEX AS TCM_DESC,
|
||||||
|
MOLE.PAED AS PAED_DESC,
|
||||||
|
MOLE.GQCE AS GQCE_DESC,
|
||||||
|
MOLE.VBP AS VBP_DESC,
|
||||||
|
MANU.Manufacturer_Abbr AS MANU_COD,
|
||||||
|
MANU.Manufacturer_Name AS MANU_DES,
|
||||||
|
MANUT.ManufacturerType_CODE AS MNFL_COD,
|
||||||
|
MANUT.ManufacturerType_Name AS MNFL_DES,
|
||||||
|
MANU.CORP_ABBR AS CORP_COD,
|
||||||
|
MANU.CORP_DES AS CORP_DES,
|
||||||
|
BRANDTYPE.Brand_Type AS BrandType
|
||||||
|
from dwd.dwd_ims_td_pack PACK
|
||||||
|
LEFT JOIN dwd.dwd_ims_td_product PROD ON PACK.Product_ID = PROD.Product_ID
|
||||||
|
LEFT JOIN dwd.dwd_ims_td_new_form_class NFC ON PACK.NewFormClass_ID = NFC.NewFormClass_ID
|
||||||
|
LEFT JOIN dwd.dwd_ims_nfc_hierarchy NFCH ON NFC.NewFormClass_Code = NFCH.NFC3_CODE
|
||||||
|
LEFT JOIN dwd.dwd_ims_td_therapeutic_class ATC ON PACK.Therapeutic_ID = ATC.Therapeutic_ID
|
||||||
|
LEFT JOIN dwd.dwd_ims_atc_hierarchy ATCH ON ATC.Therapeutic_Code = ATCH.ATC4_CODE
|
||||||
|
LEFT JOIN dwd.dwd_ims_td_pack_additional_attribute MOLE ON PACK.Pack_ID = MOLE.Pack_ID
|
||||||
|
LEFT JOIN dwd.dwd_ims_td_manufacturer_corp MANU ON PROD.Manufacturer_ID = MANU.Manufacturer_ID
|
||||||
|
LEFT JOIN dwd.dwd_ims_td_manufacturertype MANUT ON MANU.ManufacturerType_ID = MANUT.ManufacturerType_ID
|
||||||
|
LEFT JOIN dwd.dwd_gnd_ims_tblbrandtype BRANDTYPE ON PACK.Pack_Code = BRANDTYPE.PACK_COD
|
||||||
|
;
|
||||||
|
|
||||||
|
update dwd.dwd_ims_td_pack_property set CORP_COD ='' where CORP_COD is null ;
|
||||||
|
update dwd.dwd_ims_td_pack_property set STGH_DES ='' where STGH_DES is null ;
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
-- 20260119 修改 dwd.dwd_ims_td_pack_property 我感觉可以拉到前面去
|
||||||
|
-- 要调研下,为什么前面没有写,而在后面写。
|
||||||
|
--Update AZ BRANDS SCOPE---MANU的页面显示
|
||||||
|
---by Product&Pack add in 'Fluimucil, Broncho-vaxom, Ai Rui Zhuo', by AC 202103
|
||||||
|
update dwd.dwd_ims_td_pack_property
|
||||||
|
set CORP_COD = 'A5Z', CORP_DES = 'ASTRAZENECA GROUP'
|
||||||
|
where PROD_COD in(
|
||||||
|
select
|
||||||
|
right(concat('0000000000',PROD_COD),9) as PROD_COD --right(concat('000000',PROD_COD),5)
|
||||||
|
from dwd.dwd_gnd_tbl_corp_change where corp_cod='A5Z')
|
||||||
|
or PACK_COD in(
|
||||||
|
select
|
||||||
|
if( PACK_COD REGEXP '^[0-9]',right(concat('000000000000',PACK_COD),12),PACK_COD) as PACK_COD --right(concat('000000',PACK_COD),7)
|
||||||
|
from dwd.dwd_gnd_tbl_corp_change where corp_cod='A5Z')
|
||||||
|
-- where PROD_COD in ('97336','73148') OR PACK_COD = '0795918'
|
||||||
|
;
|
||||||
|
---by Product&Pack remove out tblAZScope.xlsx', by AC 202103
|
||||||
|
update dwd.dwd_ims_td_pack_property
|
||||||
|
set CORP_COD = 'A5ZD', CORP_DES = 'AZDealed',MANU_COD = 'A5ZD' ,MANU_DES = 'AZDealed'
|
||||||
|
where PROD_COD in(
|
||||||
|
select
|
||||||
|
right(concat('0000000000',PROD_COD),9) as PROD_COD
|
||||||
|
from dwd.dwd_gnd_tbl_corp_change where corp_cod='A5ZD')
|
||||||
|
or PACK_COD in(
|
||||||
|
select
|
||||||
|
if( PACK_COD REGEXP '^[0-9]',right(concat('000000000000',PACK_COD),12),PACK_COD) as PACK_COD
|
||||||
|
from dwd.dwd_gnd_tbl_corp_change where corp_cod='A5ZD')
|
||||||
|
-- where PROD_COD in ('02267','14691','58423','12754','06482','97266','14164')
|
||||||
|
;
|
||||||
BIN
EXTERNAL/CHPA/01 dwd_ims_td_pack_property.sql:Zone.Identifier
Normal file
BIN
EXTERNAL/CHPA/01 dwd_ims_td_pack_property.sql:Zone.Identifier
Normal file
Binary file not shown.
32
EXTERNAL/CHPA/01 dwd_update.sql
Normal file
32
EXTERNAL/CHPA/01 dwd_update.sql
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
-- Databricks notebook source
|
||||||
|
--对折算规则文件中的Pack,Mole code 补0
|
||||||
|
--时间条件为空的,赋默认值
|
||||||
|
--地理维度表增加KCT和ROC
|
||||||
|
UPDATE dwd.dwd_gnd_ims_tblBrandRatio
|
||||||
|
SET
|
||||||
|
PACK_COD = if( CAST(PACK_COD AS string) REGEXP '^[0-9]',right(concat('000000000000',CAST(PACK_COD AS string)),12),CAST(PACK_COD AS string)),
|
||||||
|
CMPS_COD = RIGHT(concat('000000',CAST(CMPS_COD AS string)),6),
|
||||||
|
StartTime = CASE WHEN StartTime IS NULL OR UPPER(StartTime)= 'ALL' THEN '190001' ELSE StartTime END,
|
||||||
|
EndTime = CASE WHEN EndTime IS NULL OR UPPER(EndTime)= 'ALL' THEN '209901' ELSE EndTime END
|
||||||
|
;
|
||||||
|
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
-- 20260119 这个补0操作,可以放最前面,或者专门找个地方放。
|
||||||
|
----确保Market和KeyCompetitor配置表中,Pack_Code,Product_Code,Molecule_Code 三个字段都已经补0
|
||||||
|
|
||||||
|
UPDATE dwd.dwd_gnd_ims_tblmarket
|
||||||
|
--20250428 chenwu iqivacode扩到12位,brandcode 扩到9位
|
||||||
|
SET Pack_Code = if( Pack_Code REGEXP '^[0-9]',right(concat('000000000000',Pack_Code),12),Pack_Code)
|
||||||
|
,Product_Code = RIGHT(concat('000000000',Product_Code),9) --RIGHT(concat('00000',Product_Code),5)
|
||||||
|
,Molecule_Code = RIGHT(concat('000000',Molecule_Code),6)
|
||||||
|
;
|
||||||
|
UPDATE dwd.dwd_gnd_ims_tblkeycompetitor
|
||||||
|
SET Pack_Code = if( Pack_Code REGEXP '^[0-9]',right(concat('000000000000',Pack_Code),12),Pack_Code)
|
||||||
|
,Product_Code = RIGHT(concat('000000000',Product_Code),9) --RIGHT(concat('00000',Product_Code),5)
|
||||||
|
,Molecule_Code = RIGHT(concat('000000',Molecule_Code),6)
|
||||||
|
;
|
||||||
|
UPDATE dwd.dwd_gnd_ims_tblbrandtype
|
||||||
|
SET PACK_COD = if( PACK_COD REGEXP '^[0-9]',right(concat('000000000000',PACK_COD),12),PACK_COD) --RIGHT(concat('0000000',PACK_COD),7)
|
||||||
|
;
|
||||||
BIN
EXTERNAL/CHPA/01 dwd_update.sql:Zone.Identifier
Normal file
BIN
EXTERNAL/CHPA/01 dwd_update.sql:Zone.Identifier
Normal file
Binary file not shown.
210
EXTERNAL/CHPA/01_FB_BLOB_TO_DWD.sql
Normal file
210
EXTERNAL/CHPA/01_FB_BLOB_TO_DWD.sql
Normal file
@@ -0,0 +1,210 @@
|
|||||||
|
-- Databricks notebook source
|
||||||
|
-- MAGIC %md
|
||||||
|
-- MAGIC ### 从blob读取csv文件作为chap法伯的事实表
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
-- 源表:文件
|
||||||
|
-- 目标表:DWD.DWD_GND_PHARBERS_PROV_FACT
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
-- MAGIC %run ../../../Common/config
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
-- MAGIC %python
|
||||||
|
-- MAGIC from datetime import datetime, timedelta
|
||||||
|
-- MAGIC import pandas as pd
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
-- MAGIC %python
|
||||||
|
-- MAGIC if ENVIRONMENT == PRD_ENVIRONMENT_VALUE:
|
||||||
|
-- MAGIC factsales_file_path_template = "abfss://master@azcdatalakeprd.dfs.core.chinacloudapi.cn/ODS/GND/UserUpload/"
|
||||||
|
-- MAGIC elif ENVIRONMENT == TEST_ENVIRONMENT_VALUE:
|
||||||
|
-- MAGIC factsales_file_path_template = "abfss://master@retaildlstoragetest.dfs.core.chinacloudapi.cn/ODS/GND/UserUpload/"
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
-- MAGIC %python
|
||||||
|
-- MAGIC # 计算时间得到当天的路径
|
||||||
|
-- MAGIC current_date = datetime.utcnow() + timedelta(hours=8)
|
||||||
|
-- MAGIC date_path = current_date.strftime("%Y/%m/%d/")
|
||||||
|
-- MAGIC base_path = factsales_file_path_template + date_path
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
-- MAGIC %python
|
||||||
|
-- MAGIC # 路径是否存在
|
||||||
|
-- MAGIC def path_exists(path):
|
||||||
|
-- MAGIC try:
|
||||||
|
-- MAGIC dbutils.fs.ls(path)
|
||||||
|
-- MAGIC return True
|
||||||
|
-- MAGIC except Exception as e:
|
||||||
|
-- MAGIC if "java.io.FileNotFoundException" in str(e):
|
||||||
|
-- MAGIC return False
|
||||||
|
-- MAGIC else:
|
||||||
|
-- MAGIC print(f"检查路径 {path} 时出错: {e}")
|
||||||
|
-- MAGIC raise
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
-- MAGIC %python
|
||||||
|
-- MAGIC # 列出blob上的文件列表
|
||||||
|
-- MAGIC def list_file_name(path):
|
||||||
|
-- MAGIC first_path_list = [i.path for i in dbutils.fs.ls(path)]
|
||||||
|
-- MAGIC second_path_list = [dbutils.fs.ls(i)[0] for i in first_path_list ]
|
||||||
|
-- MAGIC return second_path_list
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
-- MAGIC %python
|
||||||
|
-- MAGIC # 从blob下载文件到local
|
||||||
|
-- MAGIC def download_file(file_path, local_path):
|
||||||
|
-- MAGIC # dbutils.fs.cp(file_path, local_path.replace("/dbfs", ""))
|
||||||
|
-- MAGIC dbutils.fs.cp(file_path, local_path)
|
||||||
|
-- MAGIC print(f"已下载 {file_path} 到 {local_path}")
|
||||||
|
-- MAGIC return local_path
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
-- MAGIC %md
|
||||||
|
-- MAGIC ### 获取路径下的文件名称
|
||||||
|
-- MAGIC - 并挑出符合条件的文件路径
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
-- MAGIC %python
|
||||||
|
-- MAGIC try:
|
||||||
|
-- MAGIC if path_exists(base_path):
|
||||||
|
-- MAGIC all_file_list = list_file_name(base_path)
|
||||||
|
-- MAGIC # 生成df来筛选内容
|
||||||
|
-- MAGIC files_df = pd.DataFrame([{
|
||||||
|
-- MAGIC 'path':f.path,
|
||||||
|
-- MAGIC 'modificationtime': f.modificationTime,
|
||||||
|
-- MAGIC 'name': f.name
|
||||||
|
-- MAGIC } for f in all_file_list])
|
||||||
|
-- MAGIC print(f"{base_path} 路径存在")
|
||||||
|
-- MAGIC else:
|
||||||
|
-- MAGIC print(f"{base_path} 路径不存在")
|
||||||
|
-- MAGIC except Exception as e:
|
||||||
|
-- MAGIC print(e)
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
-- MAGIC %python
|
||||||
|
-- MAGIC try:
|
||||||
|
-- MAGIC files_df = files_df.sort_values('modificationtime', ascending=False).drop_duplicates('name').sort_index()
|
||||||
|
-- MAGIC files_df = files_df[files_df['name'].str.match(r'^Pharbers_PROV_Fact.*\.csv$')]
|
||||||
|
-- MAGIC files_df
|
||||||
|
-- MAGIC except Exception as e:
|
||||||
|
-- MAGIC print(e)
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
-- MAGIC %md
|
||||||
|
-- MAGIC ### 读取文件内容
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
-- MAGIC %python
|
||||||
|
-- MAGIC import os
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
-- MAGIC %python
|
||||||
|
-- MAGIC # 下载数据到local
|
||||||
|
-- MAGIC try:
|
||||||
|
-- MAGIC if files_df['path'].tolist():
|
||||||
|
-- MAGIC # 如果列表不为空
|
||||||
|
-- MAGIC df_all = []
|
||||||
|
-- MAGIC for file in files_df['path'].tolist():
|
||||||
|
-- MAGIC local_path = download_file(file, f"/Volumes/{NGBI_CATALOG}/tmp/volume_tmp/tmp/{os.path.basename(file)}")
|
||||||
|
-- MAGIC file_df = (spark.read.option("header", "true").option("quote", '"').option("escape", '"').option("multiLine", "true").option("mode", "PERMISSIVE").csv(local_path))
|
||||||
|
-- MAGIC file_df = file_df.drop("TA",'Market')
|
||||||
|
-- MAGIC print(f'已读取{local_path}')
|
||||||
|
-- MAGIC df_all.append(file_df)
|
||||||
|
-- MAGIC # df_combine = pd.concat(df_all)
|
||||||
|
-- MAGIC df_ifexists = True
|
||||||
|
-- MAGIC else:
|
||||||
|
-- MAGIC print('没有符合条件的文件')
|
||||||
|
-- MAGIC df_ifexists= False
|
||||||
|
-- MAGIC except Exception as e:
|
||||||
|
-- MAGIC print(e)
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
-- MAGIC %python
|
||||||
|
-- MAGIC try:
|
||||||
|
-- MAGIC if df_ifexists: # 如果不为空
|
||||||
|
-- MAGIC spark.sql(f"TRUNCATE table tmp.tmp_chpa_raw_data")
|
||||||
|
-- MAGIC num = 1
|
||||||
|
-- MAGIC for i in df_all:
|
||||||
|
-- MAGIC i.withColumnRenamed('IMS.药品ID','IMS_DRUG_ID') \
|
||||||
|
-- MAGIC .withColumnRenamed('是否法伯编码','IS_HOSP_CODE') \
|
||||||
|
-- MAGIC .withColumnRenamed('规格','SPEC') \
|
||||||
|
-- MAGIC .withColumnRenamed('转换比','CONVERSION_RATIO') \
|
||||||
|
-- MAGIC .withColumnRenamed('剂型','DOSAGE_FORM') \
|
||||||
|
-- MAGIC .withColumnRenamed('价格','PRICE')
|
||||||
|
-- MAGIC # i.columns = ['area','city','yyyyqq','yyyymm','reimburse','prescription_source','prescription_dept_lv1','prescription_dept_lv2','prescription_dept_lv3','ATC','new_code','common_name','product_name','pack_des','PackSize','PackageType','nfc','manu_des','drug_delivery_route','prescription','sales_vol','sales_value'] #'h_level',
|
||||||
|
-- MAGIC # sdf = spark.createDataFrame(i)
|
||||||
|
-- MAGIC i.createOrReplaceTempView('fact_sales')
|
||||||
|
-- MAGIC spark.sql(f"INSERT into tmp.tmp_chpa_raw_data SELECT * FROM fact_sales")
|
||||||
|
-- MAGIC print(f'第{num}个')
|
||||||
|
-- MAGIC num +=1
|
||||||
|
-- MAGIC except Exception as e:
|
||||||
|
-- MAGIC print(e)
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
-- MAGIC %md
|
||||||
|
-- MAGIC ### 将读取到的dataframe写入表中
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
-- 全量覆盖
|
||||||
|
insert overwrite dwd.dwd_gnd_pharbers_prov_fact
|
||||||
|
select
|
||||||
|
year,
|
||||||
|
ym,
|
||||||
|
province_c,
|
||||||
|
ims_drug_id,
|
||||||
|
is_hosp_code,
|
||||||
|
prod_corp,
|
||||||
|
prod_cod,
|
||||||
|
pack_cod,
|
||||||
|
phcd,
|
||||||
|
prod_des,
|
||||||
|
cmps_des,
|
||||||
|
corp_des,
|
||||||
|
mnfl_cod,
|
||||||
|
prod_des_c,
|
||||||
|
cmps_c,
|
||||||
|
corp_des_c,
|
||||||
|
pack_des,
|
||||||
|
spec,
|
||||||
|
conversion_ratio,
|
||||||
|
dosage_form,
|
||||||
|
atc4_cod,
|
||||||
|
app1_cod,
|
||||||
|
app1_des,
|
||||||
|
app1_des_c,
|
||||||
|
app2_cod,
|
||||||
|
app2_des,
|
||||||
|
app2_des_c,
|
||||||
|
app3_cod,
|
||||||
|
app3_des,
|
||||||
|
app3_des_c,
|
||||||
|
vbp_batch,
|
||||||
|
vbp,
|
||||||
|
value,
|
||||||
|
totalunit,
|
||||||
|
countingunit,
|
||||||
|
price,
|
||||||
|
manu_des,
|
||||||
|
manu_des_c,
|
||||||
|
NULL AS source_file_path,
|
||||||
|
NULL AS source_file_name,
|
||||||
|
from_utc_timestamp(current_timestamp(),'UTC+8') AS etl_insert_dt
|
||||||
|
from tmp.tmp_chpa_raw_data
|
||||||
BIN
EXTERNAL/CHPA/01_FB_BLOB_TO_DWD.sql:Zone.Identifier
Normal file
BIN
EXTERNAL/CHPA/01_FB_BLOB_TO_DWD.sql:Zone.Identifier
Normal file
Binary file not shown.
19
EXTERNAL/CHPA/02 DWS_IMS_TD_GEO.sql
Normal file
19
EXTERNAL/CHPA/02 DWS_IMS_TD_GEO.sql
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
-- Databricks notebook source
|
||||||
|
-- 20260119 地理数据 我感觉可以拉到前面去
|
||||||
|
--地理维度信息 Audit
|
||||||
|
-- 20260122 因为城市数据不再提供,改为硬编码省份数据. By Yusuo/Chenwu
|
||||||
|
INSERT OVERWRITE TABLE DWS.DWS_IMS_TD_GEO
|
||||||
|
SELECT
|
||||||
|
AUDIT_COD,
|
||||||
|
AUDIT_DES,
|
||||||
|
AUDIT_DES_C,
|
||||||
|
AUDIT_TYPE,
|
||||||
|
CITY_TIER,
|
||||||
|
AZ_CITY_TIER,
|
||||||
|
PROVINCE,
|
||||||
|
PROVINCE_C,
|
||||||
|
REGIONCENTER,
|
||||||
|
ETL_INSERT_DT,
|
||||||
|
ETL_UPDATE_DT
|
||||||
|
FROM
|
||||||
|
tmp.tmp_province_rawdata
|
||||||
BIN
EXTERNAL/CHPA/02 DWS_IMS_TD_GEO.sql:Zone.Identifier
Normal file
BIN
EXTERNAL/CHPA/02 DWS_IMS_TD_GEO.sql:Zone.Identifier
Normal file
Binary file not shown.
33
EXTERNAL/CHPA/02 dws_ims_td_atc_cn.sql
Normal file
33
EXTERNAL/CHPA/02 dws_ims_td_atc_cn.sql
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
-- Databricks notebook source
|
||||||
|
-- 源表:DWD.DWD_GND_IMS_TBLATC,DWD.DWD_IMS_ATC_HIERARCHY
|
||||||
|
-- 目标表:DWS.DWS_IMS_TD_ATC_CN
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
--ATC挂中文名
|
||||||
|
insert overwrite table dws.dws_ims_td_atc_cn
|
||||||
|
SELECT DISTINCT
|
||||||
|
T1.ATC1_CODE AS ATC1_COD,
|
||||||
|
T1.ATC1_DES,
|
||||||
|
case when ATC1.ATC1_DES_C is null then T1.ATC1_DES else ATC1.ATC1_DES_C end AS ATC1_DES_C,
|
||||||
|
T1.ATC2_CODe AS ATC2_COD,
|
||||||
|
T1.ATC2_DES,
|
||||||
|
case when ATC2.ATC2_DES_C is null then T1.ATC2_DES else ATC2.ATC2_DES_C end AS ATC2_DES_C,
|
||||||
|
T1.ATC3_CODe AS ATC3_COD,
|
||||||
|
T1.ATC3_DES,
|
||||||
|
case when ATC3.ATC3_DES_C is null then T1.ATC3_DES else ATC3.ATC3_DES_C end AS ATC3_DES_C,
|
||||||
|
T1.ATC4_CODe AS ATC4_COD,
|
||||||
|
T1.ATC4_DES,
|
||||||
|
case when ATC4.ATC4_DES_C is null then T1.ATC4_DES else ATC4.ATC4_DES_C end AS ATC4_DES_C,
|
||||||
|
from_utc_timestamp(current_timestamp(),'UTC+8'),
|
||||||
|
from_utc_timestamp(current_timestamp(),'UTC+8')
|
||||||
|
FROM dwd.dwd_ims_ATC_Hierarchy T1
|
||||||
|
LEFT JOIN (SELECT DISTINCT ATC4_COD,ATC4_DES_C FROM dwd.dwd_gnd_ims_tblATC) ATC4
|
||||||
|
ON T1.ATC4_CODE = ATC4.ATC4_COD
|
||||||
|
LEFT JOIN (SELECT DISTINCT ATC3_COD,ATC3_DES_C FROM dwd.dwd_gnd_ims_tblATC) ATC3
|
||||||
|
ON T1.ATC3_CODE = ATC3.ATC3_COD
|
||||||
|
LEFT JOIN (SELECT DISTINCT ATC2_COD,ATC2_DES_C FROM dwd.dwd_gnd_ims_tblATC) ATC2
|
||||||
|
ON T1.ATC2_CODE = ATC2.ATC2_COD
|
||||||
|
LEFT JOIN (SELECT DISTINCT ATC1_COD,ATC1_DES_C FROM dwd.dwd_gnd_ims_tblATC) ATC1
|
||||||
|
ON T1.ATC1_CODE = ATC1.ATC1_COD
|
||||||
|
;
|
||||||
BIN
EXTERNAL/CHPA/02 dws_ims_td_atc_cn.sql:Zone.Identifier
Normal file
BIN
EXTERNAL/CHPA/02 dws_ims_td_atc_cn.sql:Zone.Identifier
Normal file
Binary file not shown.
19
EXTERNAL/CHPA/02 dws_ims_td_corp_cn.sql
Normal file
19
EXTERNAL/CHPA/02 dws_ims_td_corp_cn.sql
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
-- Databricks notebook source
|
||||||
|
-- 20260119 CORP 公司名中文信息 我感觉可以拉到前面去
|
||||||
|
--CORP 公司名中文信息
|
||||||
|
insert overwrite table dws.dws_ims_td_corp_cn
|
||||||
|
SELECT DISTINCT T1.CORP_ABBR AS CORP_COD,T1.CORP_DES,T2.nameC AS CORP_DES_C,
|
||||||
|
from_utc_timestamp(current_timestamp(),'UTC+8'),
|
||||||
|
from_utc_timestamp(current_timestamp(),'UTC+8')
|
||||||
|
FROM dwd.dwd_ims_td_manufacturer_corp T1
|
||||||
|
LEFT JOIN dwd.dwd_gnd_ims_tblmanucn T2
|
||||||
|
ON T1.CORP_ABBR = T2.abbrev
|
||||||
|
WHERE CORP_ABBR IS NOT NULL
|
||||||
|
;
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
--新增AZDealed部分的维度
|
||||||
|
INSERT INTO dws.dws_ims_td_corp_cn SELECT 'A5ZD','AZDealed','AZDealed',
|
||||||
|
from_utc_timestamp(current_timestamp(),'UTC+8'),
|
||||||
|
from_utc_timestamp(current_timestamp(),'UTC+8');
|
||||||
BIN
EXTERNAL/CHPA/02 dws_ims_td_corp_cn.sql:Zone.Identifier
Normal file
BIN
EXTERNAL/CHPA/02 dws_ims_td_corp_cn.sql:Zone.Identifier
Normal file
Binary file not shown.
28
EXTERNAL/CHPA/02 dws_ims_td_date.sql
Normal file
28
EXTERNAL/CHPA/02 dws_ims_td_date.sql
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
-- Databricks notebook source
|
||||||
|
-- 依赖事实表 tmp.tmp_ims_tf_fact_sales; 所以一定要放后面。
|
||||||
|
--基于Fact数据动态计算时间维度 最近五年
|
||||||
|
insert overwrite table dws.dws_ims_td_date
|
||||||
|
select distinct
|
||||||
|
YM,
|
||||||
|
left(cast(YM as string),4) as YEAR,
|
||||||
|
right(cast(YM as string),2) as MONTH,
|
||||||
|
concat(
|
||||||
|
'Q',quarter(
|
||||||
|
date(
|
||||||
|
concat(
|
||||||
|
left(cast(YM as string),4),'-',right(cast(YM as string),2),'-01'
|
||||||
|
)))) as QUARTER,
|
||||||
|
concat(
|
||||||
|
left(cast(YM as string),4),'Q',quarter(
|
||||||
|
date(
|
||||||
|
concat(
|
||||||
|
left(cast(YM as string),4),'-',right(cast(YM as string),2),'-01'
|
||||||
|
)))) as YQ,
|
||||||
|
case when YM=(select max(YM) from tmp.tmp_ims_tf_fact_sales) then 'R' else right(cast(YM as string),2) end as DATE_FLAG,
|
||||||
|
case when YM%100>6 then concat(left(cast(YM as string),4),'H2') else concat(left(cast(YM as string),4),'H1') end as HALF_YEAR,
|
||||||
|
from_utc_timestamp(current_timestamp(),'UTC+8'),
|
||||||
|
from_utc_timestamp(current_timestamp(),'UTC+8')
|
||||||
|
from tmp.tmp_ims_tf_fact_sales
|
||||||
|
where YM > (select max(YM)-900 from tmp.tmp_ims_tf_fact_sales)
|
||||||
|
order by YM desc
|
||||||
|
;
|
||||||
BIN
EXTERNAL/CHPA/02 dws_ims_td_date.sql:Zone.Identifier
Normal file
BIN
EXTERNAL/CHPA/02 dws_ims_td_date.sql:Zone.Identifier
Normal file
Binary file not shown.
18
EXTERNAL/CHPA/02 dws_ims_td_manu_cn.sql
Normal file
18
EXTERNAL/CHPA/02 dws_ims_td_manu_cn.sql
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
-- Databricks notebook source
|
||||||
|
-- 20260119 MANU 生产商名中文信息 我感觉可以拉到前面去
|
||||||
|
---MANU 生产商名中文信息
|
||||||
|
insert overwrite table dws.dws_ims_td_manu_cn
|
||||||
|
SELECT DISTINCT T1.Manufacturer_Abbr AS MANU_COD,T1.Manufacturer_Name AS MANU_DES,T2.nameC AS MANU_DES_C,
|
||||||
|
from_utc_timestamp(current_timestamp(),'UTC+8'),
|
||||||
|
from_utc_timestamp(current_timestamp(),'UTC+8')
|
||||||
|
FROM dwd.dwd_ims_td_manufacturer_corp T1
|
||||||
|
LEFT JOIN dwd.dwd_gnd_ims_tblmanucn T2
|
||||||
|
ON T1.Manufacturer_Abbr = T2.abbrev
|
||||||
|
WHERE T1.Manufacturer_Abbr IS NOT NULL
|
||||||
|
;
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
INSERT INTO dws.dws_ims_td_manu_cn SELECT 'A5ZD','AZDealed','AZDealed',
|
||||||
|
from_utc_timestamp(current_timestamp(),'UTC+8'),
|
||||||
|
from_utc_timestamp(current_timestamp(),'UTC+8');
|
||||||
BIN
EXTERNAL/CHPA/02 dws_ims_td_manu_cn.sql:Zone.Identifier
Normal file
BIN
EXTERNAL/CHPA/02 dws_ims_td_manu_cn.sql:Zone.Identifier
Normal file
Binary file not shown.
238
EXTERNAL/CHPA/02 dws_ims_td_market.sql
Normal file
238
EXTERNAL/CHPA/02 dws_ims_td_market.sql
Normal file
@@ -0,0 +1,238 @@
|
|||||||
|
-- Databricks notebook source
|
||||||
|
-- 20260119 依赖上面 dwd.dwd_ims_td_pack_property
|
||||||
|
-- 20260119 依赖上面补0 dwd.dwd_ims_td_pack_property dwd.dwd_gnd_ims_tblmarket
|
||||||
|
-----MARKET
|
||||||
|
--第一步:
|
||||||
|
--先处理 Extend_Market 和 NOT_IN_FLAG 都为空的数据,代表有明确定义得市场
|
||||||
|
insert overwrite table tmp.tmp_ims_td_temp_market
|
||||||
|
select distinct t2.market,t1.*,
|
||||||
|
t2.bu,CASE WHEN t2.extend_market_ratio IS NULL THEN '1' ELSE t2.extend_market_ratio END AS Market_Ratio
|
||||||
|
from dwd.dwd_ims_td_pack_property t1
|
||||||
|
left join (select * from dwd.dwd_gnd_ims_tblmarket WHERE Extend_Market IS NULL AND ( NOT_IN_FLAG IS NULL or NOT_IN_FLAG = '1' )) t2
|
||||||
|
on t1.ATC1_COD = case when t2.ATC1_Code is null then t1.ATC1_COD else t2.ATC1_Code end
|
||||||
|
and t1.ATC2_COD = case when t2.ATC2_Code is null then t1.ATC2_COD else t2.ATC2_Code end
|
||||||
|
and t1.ATC3_COD = case when t2.ATC3_Code is null then t1.ATC3_COD else t2.ATC3_Code end
|
||||||
|
and t1.ATC4_COD = case when t2.ATC4_Code is null then t1.ATC4_COD else t2.ATC4_Code end
|
||||||
|
and t1.APP1_COD = case when t2.NFC1_Code is null then t1.APP1_COD else t2.NFC1_Code end
|
||||||
|
and t1.APP2_COD = case when t2.NFC2_Code is null then t1.APP2_COD else t2.NFC2_Code end
|
||||||
|
and t1.APP3_COD = case when t2.NFC3_Code is null then t1.APP3_COD else t2.NFC3_Code end
|
||||||
|
and t1.CORP_COD = case when t2.corporation_code is null then t1.CORP_COD else t2.corporation_code end
|
||||||
|
and t1.MANU_COD = case when t2.Manufacturer_Code is null then t1.MANU_COD else t2.Manufacturer_Code end
|
||||||
|
and t1.PROD_COD = case when t2.Product_Code is null then t1.PROD_COD else t2.Product_Code end
|
||||||
|
and t1.PACK_COD = case when t2.Pack_Code is null then t1.PACK_COD else t2.Pack_Code end
|
||||||
|
and t1.STGH_DES = case when t2.Strength is null then t1.STGH_DES else t2.Strength end
|
||||||
|
and t1.CMPS_COD = case when t2.Molecule_Code is null then t1.CMPS_COD else t2.Molecule_Code end
|
||||||
|
where t2.market is not null
|
||||||
|
;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
--第二步:
|
||||||
|
--处理 Extend_Market 为空,并且 NOT_IN_FLAG 有值的数据,代表反选定义市场的规则
|
||||||
|
insert overwrite table tmp.tmp_ims_td_temp_market_del
|
||||||
|
select distinct t2.market,t1.*,t2.bu,CASE WHEN t2.extend_market_ratio IS NULL THEN '1' ELSE t2.extend_market_ratio END AS Market_Ratio
|
||||||
|
from dwd.dwd_ims_td_pack_property t1
|
||||||
|
left join (select * from dwd.dwd_gnd_ims_tblmarket WHERE Extend_Market IS NULL AND NOT_IN_FLAG = '0' ) t2
|
||||||
|
on t1.ATC1_COD = case when t2.ATC1_Code is null then t1.ATC1_COD else t2.ATC1_Code end
|
||||||
|
and t1.ATC2_COD = case when t2.ATC2_Code is null then t1.ATC2_COD else t2.ATC2_Code end
|
||||||
|
and t1.ATC3_COD = case when t2.ATC3_Code is null then t1.ATC3_COD else t2.ATC3_Code end
|
||||||
|
and t1.ATC4_COD = case when t2.ATC4_Code is null then t1.ATC4_COD else t2.ATC4_Code end
|
||||||
|
and t1.APP1_COD = case when t2.NFC1_Code is null then t1.APP1_COD else t2.NFC1_Code end
|
||||||
|
and t1.APP2_COD = case when t2.NFC2_Code is null then t1.APP2_COD else t2.NFC2_Code end
|
||||||
|
and t1.APP3_COD = case when t2.NFC3_Code is null then t1.APP3_COD else t2.NFC3_Code end
|
||||||
|
and t1.CORP_COD = case when t2.corporation_code is null then t1.CORP_COD else t2.corporation_code end
|
||||||
|
and t1.MANU_COD = case when t2.Manufacturer_Code is null then t1.MANU_COD else t2.Manufacturer_Code end
|
||||||
|
and t1.PROD_COD = case when t2.Product_Code is null then t1.PROD_COD else t2.Product_Code end
|
||||||
|
and t1.PACK_COD = case when t2.Pack_Code is null then t1.PACK_COD else t2.Pack_Code end
|
||||||
|
and t1.STGH_DES = case when t2.Strength is null then t1.STGH_DES else t2.Strength end
|
||||||
|
and t1.CMPS_COD = case when t2.Molecule_Code is null then t1.CMPS_COD else t2.Molecule_Code end
|
||||||
|
where t2.market is not null
|
||||||
|
;
|
||||||
|
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
--!!!注意此段是反选的规则,故执行的是从已经定义好的TempMKT中删除数据
|
||||||
|
--需要看TempMKT的字段结构,没有值的字段,在Value里面放个空值
|
||||||
|
MERGE INTO tmp.tmp_ims_td_temp_market AS t1
|
||||||
|
USING tmp.tmp_ims_td_temp_market_del AS t2
|
||||||
|
on t1.ATC1_COD = t2.ATC1_COD
|
||||||
|
and t1.ATC2_COD = t2.ATC2_COD
|
||||||
|
and t1.ATC3_COD = t2.ATC3_COD
|
||||||
|
and t1.ATC4_COD = t2.ATC4_COD
|
||||||
|
and t1.APP1_COD = t2.APP1_COD
|
||||||
|
and t1.APP2_COD = t2.APP2_COD
|
||||||
|
and t1.APP3_COD = t2.APP3_COD
|
||||||
|
and t1.CORP_COD = t2.CORP_COD
|
||||||
|
and t1.MANU_COD = t2.MANU_COD
|
||||||
|
and t1.PROD_COD = t2.PROD_COD
|
||||||
|
and t1.PACK_COD = t2.PACK_COD
|
||||||
|
and t1.STGH_DES = t2.STGH_DES
|
||||||
|
and t1.CMPS_COD = t2.CMPS_COD
|
||||||
|
and t1.market = t2.market
|
||||||
|
WHEN MATCHED THEN DELETE
|
||||||
|
;
|
||||||
|
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
---第三步:
|
||||||
|
--处理 Extend_Market 有值得数据,用已有市场,定义新的市场,带上系数
|
||||||
|
insert into tmp.tmp_ims_td_temp_market
|
||||||
|
select distinct
|
||||||
|
t2.Market,
|
||||||
|
t1.PACK_COD ,
|
||||||
|
t1.PACK_DES ,
|
||||||
|
t1.STGH_DES ,
|
||||||
|
t1.PACK_LCH ,
|
||||||
|
t1.PROD_COD ,
|
||||||
|
t1.CMPS_COD ,
|
||||||
|
t1.CMPS_DES ,
|
||||||
|
t1.ATC1_COD ,
|
||||||
|
t1.ATC2_COD ,
|
||||||
|
t1.ATC3_COD ,
|
||||||
|
t1.ATC4_COD ,
|
||||||
|
t1.APP1_COD ,
|
||||||
|
t1.APP2_COD ,
|
||||||
|
t1.APP3_COD ,
|
||||||
|
t1.BIO_DESC ,
|
||||||
|
t1.GENE_ORIG_DESC ,
|
||||||
|
t1.ETH_OTC_DESC ,
|
||||||
|
t1.NRDL_DESC ,
|
||||||
|
t1.NRDL_Entry_Date ,
|
||||||
|
t1.EDL_DESC ,
|
||||||
|
t1.TCM_DESC ,
|
||||||
|
t1.PAED_DESC ,
|
||||||
|
t1.GQCE_DESC ,
|
||||||
|
t1.VBP_DESC ,
|
||||||
|
t1.MANU_COD ,
|
||||||
|
t1.MANU_DES ,
|
||||||
|
t1.MNFL_COD ,
|
||||||
|
t1.MNFL_DES ,
|
||||||
|
t1.CORP_COD ,
|
||||||
|
t1.CORP_DES ,
|
||||||
|
t2.BU,
|
||||||
|
t1.BrandType ,
|
||||||
|
case when t2.Extend_Market_Ratio is null then '1' end AS Market_Ratio--市场的系数若没有维护,默认为1
|
||||||
|
from tmp.tmp_ims_td_temp_market t1
|
||||||
|
left join (select Market, BU,Extend_Market,Extend_Market_Ratio from dwd.dwd_gnd_ims_tblmarket WHERE Extend_Market IS NOT NULL) t2 on t1.Market=t2.Extend_Market
|
||||||
|
where t2.Market is not null
|
||||||
|
;
|
||||||
|
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
--将,市场的系数为空值的数据,初始化为1
|
||||||
|
UPDATE tmp.tmp_ims_td_temp_market SET Market_Ratio = 1 WHERE Market_Ratio IS NULL;
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
-- 20260119 依赖 tmp.tmp_ims_td_temp_market
|
||||||
|
-- 20260119 依赖上面补0 dwd.dwd_gnd_ims_tblkeycompetitor
|
||||||
|
|
||||||
|
--KeyCompetitor部分
|
||||||
|
--定义Key Competitor
|
||||||
|
insert overwrite table tmp.tmp_ims_td_temp_market_upd1
|
||||||
|
select distinct t2.keycompetitor as Key_Competitor,t2.no,t2.no1,t1.*
|
||||||
|
from tmp.tmp_ims_td_temp_market t1
|
||||||
|
left join (
|
||||||
|
select
|
||||||
|
ROW_NUMBER() OVER(ORDER BY
|
||||||
|
CASE
|
||||||
|
WHEN ATC1_Code IS NOT NULL THEN 1
|
||||||
|
WHEN ATC2_Code IS NOT NULL THEN 2
|
||||||
|
WHEN ATC3_Code IS NOT NULL THEN 3
|
||||||
|
WHEN ATC4_Code IS NOT NULL THEN 4
|
||||||
|
WHEN Molecule_Code IS NOT NULL THEN 5
|
||||||
|
WHEN Product_Code IS NOT NULL THEN 6
|
||||||
|
WHEN Pack_Code IS NOT NULL THEN 7
|
||||||
|
ELSE 999 END,
|
||||||
|
CASE WHEN NFC1_CODE IS NULL THEN 0 ELSE 1 END,
|
||||||
|
CASE WHEN NFC2_CODE IS NULL THEN 0 ELSE 1 END,
|
||||||
|
CASE WHEN NFC2_CODE IS NULL THEN 0 ELSE 1 END,
|
||||||
|
CASE WHEN NFC3_CODE IS NULL THEN 0 ELSE 1 END
|
||||||
|
) AS no1,* from dwd.dwd_gnd_ims_tblkeycompetitor
|
||||||
|
)t2
|
||||||
|
on t1.ATC1_COD = case when t2.ATC1_Code is null then t1.ATC1_COD else t2.ATC1_Code end
|
||||||
|
and t1.ATC2_COD = case when t2.ATC2_Code is null then t1.ATC2_COD else t2.ATC2_Code end
|
||||||
|
and t1.ATC3_COD = case when t2.ATC3_Code is null then t1.ATC3_COD else t2.ATC3_Code end
|
||||||
|
and t1.ATC4_COD = case when t2.ATC4_Code is null then t1.ATC4_COD else t2.ATC4_Code end
|
||||||
|
and t1.APP1_COD = case when t2.NFC1_Code is null then t1.APP1_COD else t2.NFC1_Code end
|
||||||
|
and t1.APP2_COD = case when t2.NFC2_Code is null then t1.APP2_COD else t2.NFC2_Code end
|
||||||
|
and t1.APP3_COD = case when t2.NFC3_Code is null then t1.APP3_COD else t2.NFC3_Code end
|
||||||
|
and t1.CORP_COD = case when t2.corporation_code is null then t1.CORP_COD else t2.corporation_code end
|
||||||
|
and t1.MANU_COD = case when t2.Manufacturer_Code is null then t1.MANU_COD else t2.Manufacturer_Code end
|
||||||
|
and t1.PROD_COD = case when t2.Product_Code is null then t1.PROD_COD else t2.Product_Code end
|
||||||
|
and t1.PACK_COD = case when t2.Pack_Code is null then t1.PACK_COD else t2.Pack_Code end
|
||||||
|
and t1.STGH_DES = case when t2.Strength is null then t1.STGH_DES else t2.Strength end
|
||||||
|
and t1.CMPS_COD = case when t2.Molecule_Code is null then t1.CMPS_COD else t2.Molecule_Code end
|
||||||
|
and t1.market = case when t2.market is null then t1.market else t2.market end
|
||||||
|
;
|
||||||
|
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
--删除被重复定义的Key Competitor,删除写入时间早的(No 小的)的对应行即可,保留No最大的一行即最新的规则
|
||||||
|
insert overwrite table tmp.tmp_ims_td_temp_market_upd2
|
||||||
|
select * from(
|
||||||
|
select row_number() over(partition by market,pack_cod,prod_cod order by no1 desc,no desc) as id,*
|
||||||
|
from tmp.tmp_ims_td_temp_market_upd1) where id>1
|
||||||
|
;
|
||||||
|
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
MERGE INTO tmp.tmp_ims_td_temp_market_upd1 AS t1
|
||||||
|
USING tmp.tmp_ims_td_temp_market_upd2 AS t2
|
||||||
|
on t1.ATC1_COD = t2.ATC1_COD
|
||||||
|
and t1.ATC2_COD = t2.ATC2_COD
|
||||||
|
and t1.ATC3_COD = t2.ATC3_COD
|
||||||
|
and t1.ATC4_COD = t2.ATC4_COD
|
||||||
|
and t1.APP1_COD = t2.APP1_COD
|
||||||
|
and t1.APP2_COD = t2.APP2_COD
|
||||||
|
and t1.APP3_COD = t2.APP3_COD
|
||||||
|
and t1.CORP_COD = t2.CORP_COD
|
||||||
|
and t1.MANU_COD = t2.MANU_COD
|
||||||
|
and t1.PROD_COD = t2.PROD_COD
|
||||||
|
and t1.PACK_COD = t2.PACK_COD
|
||||||
|
and t1.STGH_DES = t2.STGH_DES
|
||||||
|
and t1.CMPS_COD = t2.CMPS_COD
|
||||||
|
and t1.market = t2.market
|
||||||
|
and t1.Key_Competitor = t2.Key_Competitor
|
||||||
|
and t1.no=t2.no
|
||||||
|
WHEN MATCHED THEN DELETE
|
||||||
|
;
|
||||||
|
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
--没有挂上KeyCompetitor的,更新为Others ,最后去掉No,写入临时表备用
|
||||||
|
update tmp.tmp_ims_td_temp_market_upd1 set Key_Competitor = 'Others' where Key_Competitor is NULL;
|
||||||
|
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
insert overwrite table tmp.tmp_ims_td_temp_market_upd
|
||||||
|
select
|
||||||
|
Key_Competitor,market,PACK_COD,PACK_DES,STGH_DES,PACK_LCH,PROD_COD,CMPS_COD,CMPS_DES,
|
||||||
|
ATC1_COD,ATC2_COD,ATC3_COD,ATC4_COD,APP1_COD,APP2_COD,APP3_COD,BIO_DESC,GENE_ORIG_DESC,
|
||||||
|
ETH_OTC_DESC,NRDL_DESC,NRDL_Entry_Date,EDL_DESC,TCM_DESC,PAED_DESC,GQCE_DESC,VBP_DESC,
|
||||||
|
MANU_COD,MANU_DES,MNFL_COD,MNFL_DES,CORP_COD,CORP_DES,BrandType,bu,Market_Ratio
|
||||||
|
from tmp.tmp_ims_td_temp_market_upd1
|
||||||
|
;
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
-- 20260119 市场维度信息 我感觉可以拉到前面去
|
||||||
|
--市场维度信息
|
||||||
|
insert overwrite table dws.dws_ims_td_market
|
||||||
|
select distinct market,PACK_COD,PACK_DES,STGH_DES,PACK_LCH,PROD_COD,CMPS_COD,CMPS_DES,
|
||||||
|
ATC1_COD,ATC2_COD,ATC3_COD,ATC4_COD,APP1_COD,APP2_COD,APP3_COD,BIO_DESC,GENE_ORIG_DESC,
|
||||||
|
ETH_OTC_DESC,NRDL_DESC,NRDL_Entry_Date,EDL_DESC,TCM_DESC,PAED_DESC,GQCE_DESC,VBP_DESC,
|
||||||
|
MANU_COD,MANU_DES,MNFL_COD,MNFL_DES,CORP_COD,CORP_DES,BrandType,bu,Market_Ratio,Key_Competitor,
|
||||||
|
from_utc_timestamp(current_timestamp(),'UTC+8'),
|
||||||
|
from_utc_timestamp(current_timestamp(),'UTC+8')
|
||||||
|
from tmp.tmp_ims_td_temp_market_upd
|
||||||
|
;
|
||||||
BIN
EXTERNAL/CHPA/02 dws_ims_td_market.sql:Zone.Identifier
Normal file
BIN
EXTERNAL/CHPA/02 dws_ims_td_market.sql:Zone.Identifier
Normal file
Binary file not shown.
7
EXTERNAL/CHPA/02 dws_ims_td_market_ta.sql
Normal file
7
EXTERNAL/CHPA/02 dws_ims_td_market_ta.sql
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
-- Databricks notebook source
|
||||||
|
-- 20260119 市场&TA 关系信息 我感觉可以拉到前面去
|
||||||
|
--市场&TA 关系信息
|
||||||
|
insert overwrite table dws.dws_ims_td_market_ta
|
||||||
|
select *
|
||||||
|
from dwd.dwd_gnd_ims_tblmarket_ta_map
|
||||||
|
;
|
||||||
BIN
EXTERNAL/CHPA/02 dws_ims_td_market_ta.sql:Zone.Identifier
Normal file
BIN
EXTERNAL/CHPA/02 dws_ims_td_market_ta.sql:Zone.Identifier
Normal file
Binary file not shown.
22
EXTERNAL/CHPA/02 dws_ims_td_nfc_cn.sql
Normal file
22
EXTERNAL/CHPA/02 dws_ims_td_nfc_cn.sql
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
-- Databricks notebook source
|
||||||
|
--NFC挂中文名
|
||||||
|
insert overwrite table dws.dws_ims_td_nfc_cn
|
||||||
|
SELECT DISTINCT T1.NFC1_CODE AS APP1_COD ,
|
||||||
|
T1.NFC1_DES AS APP1_DES,
|
||||||
|
case when NFC1.APP1_DES_C is null then T1.NFC1_DES else NFC1.APP1_DES_C end AS APP1_DES_C,
|
||||||
|
T1.NFC2_CODE AS APP2_COD ,
|
||||||
|
T1.NFC2_DES AS APP2_DES,
|
||||||
|
case when NFC2.APP2_DES_C is null then T1.NFC2_DES else NFC2.APP2_DES_C end AS APP2_DES_C,
|
||||||
|
T1.NFC3_CODE AS APP3_COD,
|
||||||
|
T1.NFC3_DES AS APP3_DES,
|
||||||
|
case when NFC3.APP3_DES_C is null then T1.NFC3_DES else NFC3.APP3_DES_C end AS APP3_DES_C,
|
||||||
|
from_utc_timestamp(current_timestamp(),'UTC+8'),
|
||||||
|
from_utc_timestamp(current_timestamp(),'UTC+8')
|
||||||
|
FROM dwd.dwd_ims_NFC_Hierarchy T1
|
||||||
|
LEFT JOIN (SELECT DISTINCT APP3_COD,APP3_DES_C FROM dwd.dwd_gnd_ims_tblAPP) NFC3
|
||||||
|
ON T1.NFC3_CODE = NFC3.APP3_COD
|
||||||
|
LEFT JOIN (SELECT DISTINCT APP2_COD,APP2_DES_C FROM dwd.dwd_gnd_ims_tblAPP) NFC2
|
||||||
|
ON T1.NFC2_CODE = NFC2.APP2_COD
|
||||||
|
LEFT JOIN (SELECT DISTINCT APP1_COD,APP1_DES_C FROM dwd.dwd_gnd_ims_tblAPP) NFC1
|
||||||
|
ON T1.NFC1_CODE = NFC1.APP1_COD
|
||||||
|
;
|
||||||
BIN
EXTERNAL/CHPA/02 dws_ims_td_nfc_cn.sql:Zone.Identifier
Normal file
BIN
EXTERNAL/CHPA/02 dws_ims_td_nfc_cn.sql:Zone.Identifier
Normal file
Binary file not shown.
7
EXTERNAL/CHPA/02 dws_ims_td_prod_cn.sql
Normal file
7
EXTERNAL/CHPA/02 dws_ims_td_prod_cn.sql
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
-- Databricks notebook source
|
||||||
|
--产品挂中文名
|
||||||
|
insert overwrite table dws.dws_ims_td_prod_cn
|
||||||
|
SELECT distinct RIGHT(concat('0000000000',prodcode),9) AS PROD_COD,ename as PROD_DES,namec as PROD_DES_C, gene_name as CMPS_DES_C,1 AS RANK_TYPE,
|
||||||
|
from_utc_timestamp(current_timestamp(),'UTC+8'),
|
||||||
|
from_utc_timestamp(current_timestamp(),'UTC+8')
|
||||||
|
FROM dwd.dwd_gnd_ims_tblprodcn
|
||||||
BIN
EXTERNAL/CHPA/02 dws_ims_td_prod_cn.sql:Zone.Identifier
Normal file
BIN
EXTERNAL/CHPA/02 dws_ims_td_prod_cn.sql:Zone.Identifier
Normal file
Binary file not shown.
32
EXTERNAL/CHPA/02 tmp_ims_td_prod_tmp.sql
Normal file
32
EXTERNAL/CHPA/02 tmp_ims_td_prod_tmp.sql
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
-- Databricks notebook source
|
||||||
|
-- DBTITLE 1,产品挂中文名
|
||||||
|
|
||||||
|
|
||||||
|
--Product中文名字,若有超过5个CORP或者MANU生产,则将其报告上排序的优先级调低
|
||||||
|
insert overwrite table tmp.tmp_ims_td_prod_tmp
|
||||||
|
SELECT DISTINCT PROD.PROD_COD
|
||||||
|
FROM dws.dws_ims_td_prod_cn PROD
|
||||||
|
LEFT JOIN dwd.dwd_ims_td_pack_property PACK
|
||||||
|
ON PROD.PROD_COD = PACK.PROD_COD
|
||||||
|
LEFT JOIN dws.dws_ims_td_corp_cn CORP
|
||||||
|
ON PACK.CORP_COD = CORP.CORP_COD
|
||||||
|
WHERE PROD.PROD_DES_C IN
|
||||||
|
(
|
||||||
|
--先计算出超过10个厂家生产的Prod_CN
|
||||||
|
|
||||||
|
--先计算出有多个厂家生产的Prod_CN
|
||||||
|
SELECT PROD.PROD_DES_C
|
||||||
|
FROM dwd.dwd_ims_td_pack_property PACK
|
||||||
|
LEFT JOIN dws.dws_ims_td_prod_cn PROD
|
||||||
|
ON PACK.PROD_COD = PROD.PROD_COD
|
||||||
|
GROUP BY PROD.PROD_DES_C
|
||||||
|
HAVING ( COUNT(DISTINCT CORP_DES) > 5 OR COUNT(DISTINCT MANU_DES) > 5 )
|
||||||
|
AND PROD.PROD_DES_C IS NOT NULL
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
UPDATE dws.dws_ims_td_prod_cn SET RANK_TYPE = 0 where PROD_COD in (
|
||||||
|
SELECT DISTINCT PROD_COD FROM tmp.tmp_ims_td_prod_tmp
|
||||||
|
);
|
||||||
BIN
EXTERNAL/CHPA/02 tmp_ims_td_prod_tmp.sql:Zone.Identifier
Normal file
BIN
EXTERNAL/CHPA/02 tmp_ims_td_prod_tmp.sql:Zone.Identifier
Normal file
Binary file not shown.
206
EXTERNAL/CHPA/02 tmp_ims_tf_fact_sales.sql
Normal file
206
EXTERNAL/CHPA/02 tmp_ims_tf_fact_sales.sql
Normal file
@@ -0,0 +1,206 @@
|
|||||||
|
-- Databricks notebook source
|
||||||
|
-- 20260130 临时方案,解决测试环境有NULL值问题
|
||||||
|
create or replace temporary view dwd_ims_td_pack
|
||||||
|
as
|
||||||
|
select
|
||||||
|
distinct
|
||||||
|
pack_id,
|
||||||
|
pack_code
|
||||||
|
from dwd.dwd_ims_td_pack
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
CREATE OR REPLACE TEMPORARY VIEW FACT_CHPA_SALES_TEMP_WITH_PREVIOUS
|
||||||
|
AS
|
||||||
|
SELECT
|
||||||
|
CAST(YM AS INT) AS YM,
|
||||||
|
CAST(YEAR AS INT) AS YEAR,
|
||||||
|
CAST(REPLACE(YM,YEAR,'') AS INT) AS MONTH,
|
||||||
|
CAST(VALUE AS DECIMAL(38,10)) AS VALUE,
|
||||||
|
CAST(COUNTINGUNIT AS DECIMAL(38,10)) AS COUNTINGUNIT,
|
||||||
|
CAST(TOTALUNIT AS DECIMAL(38,10)) AS TOTALUNIT,
|
||||||
|
PROVINCE_C,
|
||||||
|
PHCD,
|
||||||
|
conversion_ratio
|
||||||
|
FROM dwd.dwd_gnd_pharbers_prov_fact
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
CREATE OR REPLACE TEMPORARY VIEW DIM_PROVINCE
|
||||||
|
AS
|
||||||
|
select
|
||||||
|
-- t2.geo_key ,
|
||||||
|
t2.province_code,
|
||||||
|
CASE
|
||||||
|
WHEN
|
||||||
|
t2.province_name
|
||||||
|
IN ('上海','北京','天津','重庆')
|
||||||
|
THEN
|
||||||
|
CONCAT(t2.province_name,'市')
|
||||||
|
WHEN
|
||||||
|
t2.province_name
|
||||||
|
IN ('内蒙古','西藏')
|
||||||
|
THEN
|
||||||
|
CONCAT(t2.province_name,'自治区')
|
||||||
|
WHEN
|
||||||
|
t2.province_name = '宁夏'
|
||||||
|
THEN CONCAT(t2.province_name,'回族自治区')
|
||||||
|
WHEN
|
||||||
|
t2.province_name = '广西'
|
||||||
|
THEN CONCAT(t2.province_name,'壮族自治区')
|
||||||
|
WHEN
|
||||||
|
t2.province_name = '新疆'
|
||||||
|
THEN CONCAT(t2.province_name,'维吾尔自治区')
|
||||||
|
ELSE CONCAT(t2.province_name,'省')
|
||||||
|
END AS province_name
|
||||||
|
from dm.dm_td_geography t2
|
||||||
|
where t2.geo_key = t2.province_code
|
||||||
|
and province_name not in ('台湾','澳门','香港')
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
CREATE OR REPLACE TEMPORARY VIEW DIM_CHPA_PACK_INFO
|
||||||
|
AS
|
||||||
|
SELECT
|
||||||
|
PACK_COD,
|
||||||
|
MAX(IQVIA_PACK_CODE) AS IQVIA_PACK_CODE,
|
||||||
|
MAX(countingunit) as COUNTINGUNIT_RATIO
|
||||||
|
FROM
|
||||||
|
dwd.dwd_gnd_dept_pack_property
|
||||||
|
GROUP BY
|
||||||
|
PACK_COD
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
CREATE OR REPLACE TEMPORARY VIEW FACT_CHPA_SALES
|
||||||
|
AS
|
||||||
|
SELECT
|
||||||
|
YM,
|
||||||
|
YEAR,
|
||||||
|
REPLACE(YM,YEAR,'') AS MONTH,
|
||||||
|
T3.IQVIA_PACK_CODE AS PACK_CODE,
|
||||||
|
T2.PROVINCE_CODE AS AUDIT_COD,
|
||||||
|
VALUE AS MTH00LC,
|
||||||
|
0 AS MTH00LCLY,
|
||||||
|
CASE WHEN T3.COUNTINGUNIT_RATIO IS NULL THEN TOTALUNIT*T1.conversion_ratio ELSE TOTALUNIT*T3.COUNTINGUNIT_RATIO END AS MTH00CN,
|
||||||
|
0 AS MTH00CNLY,
|
||||||
|
TOTALUNIT AS MTH00UN,
|
||||||
|
0 AS MTH00UNLY
|
||||||
|
FROM FACT_CHPA_SALES_TEMP_WITH_PREVIOUS AS T1
|
||||||
|
LEFT JOIN DIM_PROVINCE AS T2 ON T1.PROVINCE_C = T2.PROVINCE_NAME
|
||||||
|
LEFT JOIN DIM_CHPA_PACK_INFO AS T3 ON T1.PHCD = T3.PACK_COD
|
||||||
|
UNION ALL
|
||||||
|
SELECT
|
||||||
|
CAST((YEAR+1)*100+REPLACE(YM,YEAR,'') AS INT) AS YM,
|
||||||
|
YEAR+1,
|
||||||
|
CAST(REPLACE(YM,YEAR,'') AS INT) AS MONTH,
|
||||||
|
T3.IQVIA_PACK_CODE AS PACK_CODE,
|
||||||
|
T2.PROVINCE_CODE AS AUDIT_COD,
|
||||||
|
0 AS MTH00LC,
|
||||||
|
VALUE AS MTH00LCLY,
|
||||||
|
0 AS MTH00CN,
|
||||||
|
CASE WHEN T3.COUNTINGUNIT_RATIO IS NULL THEN TOTALUNIT*T1.conversion_ratio ELSE TOTALUNIT*T3.COUNTINGUNIT_RATIO END AS MTH00CNLY,
|
||||||
|
0 AS MTH00UN,
|
||||||
|
TOTALUNIT AS MTH00UNLY
|
||||||
|
FROM FACT_CHPA_SALES_TEMP_WITH_PREVIOUS AS T1
|
||||||
|
LEFT JOIN DIM_PROVINCE AS T2 ON T1.PROVINCE_C = T2.PROVINCE_NAME
|
||||||
|
LEFT JOIN DIM_CHPA_PACK_INFO AS T3 ON T1.PHCD = T3.PACK_COD
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
-- 20260119 dwd.dwd_ims_td_pack、dwd.dwd_ims_tf_fact_sales 不依赖
|
||||||
|
--删除最近五年的dim pack数据,将最新的dim pack写入
|
||||||
|
delete from dws.dws_ims_td_pack_ym
|
||||||
|
where ym + 500 >
|
||||||
|
(select max(year * 100 + month) from dwd.dwd_ims_tf_fact_sales);
|
||||||
|
insert into dws.dws_ims_td_pack_ym
|
||||||
|
select
|
||||||
|
distinct ym,
|
||||||
|
pack_id,
|
||||||
|
pack_code,
|
||||||
|
from_utc_timestamp(current_timestamp(), 'UTC+8'),
|
||||||
|
from_utc_timestamp(current_timestamp(), 'UTC+8')
|
||||||
|
from
|
||||||
|
(
|
||||||
|
select
|
||||||
|
distinct year * 100 + month as ym
|
||||||
|
from
|
||||||
|
dwd.dwd_ims_tf_fact_sales
|
||||||
|
where
|
||||||
|
year * 100 + month + 500 > (
|
||||||
|
select max(year * 100 + month) from dwd.dwd_ims_tf_fact_sales
|
||||||
|
)
|
||||||
|
) full
|
||||||
|
-- join dwd.dwd_ims_td_pack
|
||||||
|
join dwd_ims_td_pack
|
||||||
|
;
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
--IMS 原始数据预处理
|
||||||
|
--FACT SALES 处理
|
||||||
|
insert overwrite table tmp.tmp_ims_tf_fact_sales
|
||||||
|
select distinct
|
||||||
|
FACT.YM,AUDIT.Audit_Code AS AUDIT_COD,FACT.Pack_Code AS PACK_COD,
|
||||||
|
sum(MTH00LC) as MTH00LC,sum(MTH00LCLY) as MTH00LCLY,
|
||||||
|
sum(MTH00CN) as MTH00CN,sum(MTH00CNLY) as MTH00CNLY,
|
||||||
|
sum(MTH00UN) as MTH00UN,sum(MTH00UNLY) as MTH00UNLY
|
||||||
|
from(
|
||||||
|
select
|
||||||
|
Year*100+Month as YM,
|
||||||
|
Year ,
|
||||||
|
Month ,
|
||||||
|
t2.pack_code ,
|
||||||
|
Audit_ID ,
|
||||||
|
SalesValue_LC as MTH00LC,
|
||||||
|
0 as MTH00LCLY,
|
||||||
|
Counting_Unit as MTH00CN,
|
||||||
|
0 as MTH00CNLY,
|
||||||
|
Sales_Unit as MTH00UN,
|
||||||
|
0 as MTH00UNLY
|
||||||
|
from dwd.dwd_ims_tf_fact_sales t1
|
||||||
|
--补充历史的PACK ID 和 PACK CODE的关联关系,带上YM将最新dim pack表full join 最近五年
|
||||||
|
left join dws.dws_ims_td_pack_ym t2 on t1.Pack_ID=t2.pack_id and t1.Year*100+t1.Month=t2.ym
|
||||||
|
union all
|
||||||
|
select
|
||||||
|
(Year+1)*100+Month as YM,
|
||||||
|
Year+1 ,
|
||||||
|
Month ,
|
||||||
|
t2.pack_code ,
|
||||||
|
Audit_ID ,
|
||||||
|
0 as MTH00LC,
|
||||||
|
SalesValue_LC as MTH00LCLY,
|
||||||
|
0 as MTH00CN,
|
||||||
|
Counting_Unit as MTH00CNLY,
|
||||||
|
0 as MTH00UN,
|
||||||
|
Sales_Unit as MTH00UNLY
|
||||||
|
from dwd.dwd_ims_tf_fact_sales t1
|
||||||
|
--补充历史的PACK ID 和 PACK CODE的关联关系,带上YM将最新dim pack表full join 最近五年
|
||||||
|
left join dws.dws_ims_td_pack_ym t2 on t1.Pack_ID=t2.pack_id and t1.Year*100+t1.Month=t2.ym
|
||||||
|
)FACT
|
||||||
|
LEFT JOIN dwd.dwd_ims_td_audit AUDIT
|
||||||
|
ON FACT.Audit_ID = AUDIT.Audit_ID
|
||||||
|
where FACT.YM<=(select max(Year*100+Month) from dwd.dwd_ims_tf_fact_sales)
|
||||||
|
----------20260320 chenwu CHPA的数据只保留202201之后的
|
||||||
|
and FACT.YM>=202201
|
||||||
|
----------
|
||||||
|
AND AUDIT.Audit_Code = 'CHT'
|
||||||
|
group by FACT.YM,AUDIT.Audit_Code,FACT.Pack_Code
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
-- 20260120新增
|
||||||
|
-- 以前 全国和省份数据放在一起,现在全国是上面,省份是新的,单独放在下面。
|
||||||
|
union all
|
||||||
|
select distinct
|
||||||
|
CAST(YM AS INT) AS YM,
|
||||||
|
AUDIT_COD,Pack_Code AS PACK_COD,
|
||||||
|
sum(MTH00LC) as MTH00LC,sum(MTH00LCLY) as MTH00LCLY,
|
||||||
|
sum(MTH00CN) as MTH00CN,sum(MTH00CNLY) as MTH00CNLY,
|
||||||
|
sum(MTH00UN) as MTH00UN,sum(MTH00UNLY) as MTH00UNLY
|
||||||
|
from
|
||||||
|
FACT_CHPA_SALES
|
||||||
|
where
|
||||||
|
YM<=(select max(YM) from dwd.dwd_gnd_pharbers_prov_fact)
|
||||||
|
group by YM,Audit_Cod,Pack_Code
|
||||||
|
;
|
||||||
BIN
EXTERNAL/CHPA/02 tmp_ims_tf_fact_sales.sql:Zone.Identifier
Normal file
BIN
EXTERNAL/CHPA/02 tmp_ims_tf_fact_sales.sql:Zone.Identifier
Normal file
Binary file not shown.
160
EXTERNAL/CHPA/03 DM_TD_EXT_CHPA_MARKET_BRAND_RATIO.sql
Normal file
160
EXTERNAL/CHPA/03 DM_TD_EXT_CHPA_MARKET_BRAND_RATIO.sql
Normal file
@@ -0,0 +1,160 @@
|
|||||||
|
-- Databricks notebook source
|
||||||
|
-- CREATE OR REPLACE TABLE DM.DM_TD_EXT_CHPA_MARKET_BRAND_RATIO (
|
||||||
|
-- MARKET STRING,
|
||||||
|
-- PACK_CODE STRING,
|
||||||
|
-- VALUE_BRAND_RATIO FLOAT,
|
||||||
|
-- VALUE_BRAND_RATIO_START STRING,
|
||||||
|
-- VALUE_BRAND_RATIO_END STRING,
|
||||||
|
-- UNIT_BRAND_RATIO FLOAT,
|
||||||
|
-- UNIT_BRAND_START STRING,
|
||||||
|
-- UNIT_BRAND_END STRING,
|
||||||
|
-- COUNTINGUNIT_BRAND_RATIO FLOAT,
|
||||||
|
-- COUNTINGUNIT_BRAND_START STRING,
|
||||||
|
-- COUNTINGUNIT_BRAND_END STRING,
|
||||||
|
-- PDOT_BRAND_RATIO FLOAT,
|
||||||
|
-- PDOT_BRAND_START STRING,
|
||||||
|
-- PDOT_BRAND_END STRING,
|
||||||
|
-- ETL_INSERT_DT TIMESTAMP,
|
||||||
|
-- ETL_UPDATE_DT TIMESTAMP)
|
||||||
|
-- USING delta
|
||||||
|
-- -- LOCATION 'abfss://master@azcdatalakeprd.dfs.core.chinacloudapi.cn/DM/dm_td_ext_chpa_market_brand_ratio';
|
||||||
|
-- -- 上面是生产环境location,下面是测试环境location
|
||||||
|
-- LOCATION 'abfss://master@retaildlstoragetest.dfs.core.chinacloudapi.cn/DM/dm_td_ext_chpa_market_brand_ratio';
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
INSERT OVERWRITE TABLE DM.DM_TD_EXT_CHPA_MARKET_BRAND_RATIO
|
||||||
|
SELECT
|
||||||
|
DISTINCT
|
||||||
|
T1.MARKET,
|
||||||
|
T1.PACK_CODE,
|
||||||
|
CASE
|
||||||
|
WHEN T2.RATIO IS NOT NULL THEN T2.RATIO
|
||||||
|
ELSE NVL(T5.RATIO, 1) END AS VALUE_BRAND_RATIO,
|
||||||
|
CASE
|
||||||
|
WHEN T2.RATIO IS NOT NULL THEN T2.STARTTIME
|
||||||
|
ELSE NVL(T5.STARTTIME, 200001) END AS VALUE_BRAND_RATIO_START,
|
||||||
|
CASE
|
||||||
|
WHEN T2.RATIO IS NOT NULL THEN T2.ENDTIME
|
||||||
|
ELSE NVL(T5.ENDTIME, 299912) END AS VALUE_BRAND_RATIO_END,
|
||||||
|
CASE
|
||||||
|
WHEN T3.RATIO IS NOT NULL THEN T3.RATIO
|
||||||
|
ELSE NVL(T6.RATIO, 1) END AS UNIT_BRAND_RATIO,
|
||||||
|
CASE
|
||||||
|
WHEN T3.RATIO IS NOT NULL THEN T3.STARTTIME
|
||||||
|
ELSE NVL(T6.STARTTIME, 200001) END AS UNIT_BRAND_START,
|
||||||
|
CASE
|
||||||
|
WHEN T3.RATIO IS NOT NULL THEN T3.ENDTIME
|
||||||
|
ELSE NVL(T6.ENDTIME, 299912) END AS UNIT_BRAND_END,
|
||||||
|
1 AS COUNTINGUNIT_BRAND_RATIO,
|
||||||
|
200001 AS COUNTINGUNIT_BRAND_START,
|
||||||
|
299912 AS COUNTINGUNIT_BRAND_END,
|
||||||
|
CASE
|
||||||
|
WHEN T4.RATIO IS NOT NULL THEN T4.RATIO
|
||||||
|
ELSE NVL(T7.RATIO, 1) END AS PDOT_BRAND_RATIO,
|
||||||
|
CASE
|
||||||
|
WHEN T4.RATIO IS NOT NULL THEN T4.STARTTIME
|
||||||
|
ELSE NVL(T7.STARTTIME, 200001) END AS PDOT_BRAND_START,
|
||||||
|
CASE
|
||||||
|
WHEN T4.RATIO IS NOT NULL THEN T4.ENDTIME
|
||||||
|
ELSE NVL(T7.ENDTIME, 299912) END AS PDOT_BRAND_END,
|
||||||
|
FROM_UTC_TIMESTAMP(CURRENT_TIMESTAMP(),'UTC+8') AS ETL_INSERT_DT,
|
||||||
|
FROM_UTC_TIMESTAMP(CURRENT_TIMESTAMP(),'UTC+8') AS ETL_UPDATE_DT
|
||||||
|
FROM DM.DM_TD_EXT_CHPA_MARKET_RATIO T1
|
||||||
|
LEFT JOIN ( --VALUE--PACK
|
||||||
|
SELECT
|
||||||
|
DISTINCT
|
||||||
|
MARKET,
|
||||||
|
CMPS_COD,
|
||||||
|
CMPS_DES,
|
||||||
|
PACK_COD,
|
||||||
|
PACK_DES,
|
||||||
|
LEVEL,
|
||||||
|
TYPE,
|
||||||
|
CAST(RATIO AS FLOAT) AS RATIO,
|
||||||
|
CAST(STARTTIME AS INT) STARTTIME,
|
||||||
|
CAST(ENDTIME AS INT) ENDTIME
|
||||||
|
FROM DWD.DWD_GND_IMS_TBLBRANDRATIO_BYMONTH
|
||||||
|
WHERE UPPER(TYPE) = 'VALUE' AND UPPER(LEVEL) = 'PACK'
|
||||||
|
) T2 ON T1.MARKET = T2.MARKET AND T1.PACK_CODE = T2.PACK_COD
|
||||||
|
LEFT JOIN ( --UNIT--PACK
|
||||||
|
SELECT
|
||||||
|
DISTINCT
|
||||||
|
MARKET,
|
||||||
|
CMPS_COD,
|
||||||
|
CMPS_DES,
|
||||||
|
PACK_COD,
|
||||||
|
PACK_DES,
|
||||||
|
LEVEL,
|
||||||
|
TYPE,
|
||||||
|
CAST(RATIO AS FLOAT) AS RATIO,
|
||||||
|
CAST(STARTTIME AS INT) STARTTIME,
|
||||||
|
CAST(ENDTIME AS INT) ENDTIME
|
||||||
|
FROM DWD.DWD_GND_IMS_TBLBRANDRATIO_BYMONTH
|
||||||
|
WHERE UPPER(TYPE) = 'UNIT' AND UPPER(LEVEL) = 'PACK'
|
||||||
|
) T3 ON T1.MARKET = T3.MARKET AND T1.PACK_CODE = T3.PACK_COD
|
||||||
|
LEFT JOIN ( --COUNTINGUNIT--PACK
|
||||||
|
SELECT
|
||||||
|
DISTINCT
|
||||||
|
MARKET,
|
||||||
|
CMPS_COD,
|
||||||
|
CMPS_DES,
|
||||||
|
PACK_COD,
|
||||||
|
PACK_DES,
|
||||||
|
LEVEL,
|
||||||
|
TYPE,
|
||||||
|
CAST(RATIO AS FLOAT) AS RATIO,
|
||||||
|
CAST(STARTTIME AS INT) STARTTIME,
|
||||||
|
CAST(ENDTIME AS INT) ENDTIME
|
||||||
|
FROM DWD.DWD_GND_IMS_TBLBRANDRATIO_BYMONTH
|
||||||
|
WHERE UPPER(TYPE) = 'COUNTINGUNIT' AND UPPER(LEVEL) = 'PACK'
|
||||||
|
) T4 ON T1.MARKET = T4.MARKET AND T1.PACK_CODE = T4.PACK_COD
|
||||||
|
LEFT JOIN ( --VALUE--MOLECULE
|
||||||
|
SELECT
|
||||||
|
DISTINCT
|
||||||
|
MARKET,
|
||||||
|
CMPS_COD,
|
||||||
|
CMPS_DES,
|
||||||
|
PACK_COD,
|
||||||
|
PACK_DES,
|
||||||
|
LEVEL,
|
||||||
|
TYPE,
|
||||||
|
CAST(RATIO AS FLOAT) AS RATIO,
|
||||||
|
CAST(STARTTIME AS INT) STARTTIME,
|
||||||
|
CAST(ENDTIME AS INT) ENDTIME
|
||||||
|
FROM DWD.DWD_GND_IMS_TBLBRANDRATIO_BYMONTH
|
||||||
|
WHERE UPPER(TYPE) = 'VALUE' AND UPPER(LEVEL) = 'MOLECULE'
|
||||||
|
) T5 ON T1.MARKET = T5.MARKET AND T1.CMPS_CODE = T5.CMPS_COD
|
||||||
|
LEFT JOIN ( --UNIT--MOLECULE
|
||||||
|
SELECT
|
||||||
|
DISTINCT
|
||||||
|
MARKET,
|
||||||
|
CMPS_COD,
|
||||||
|
CMPS_DES,
|
||||||
|
PACK_COD,
|
||||||
|
PACK_DES,
|
||||||
|
LEVEL,
|
||||||
|
TYPE,
|
||||||
|
CAST(RATIO AS FLOAT) AS RATIO,
|
||||||
|
CAST(STARTTIME AS INT) STARTTIME,
|
||||||
|
CAST(ENDTIME AS INT) ENDTIME
|
||||||
|
FROM DWD.DWD_GND_IMS_TBLBRANDRATIO_BYMONTH
|
||||||
|
WHERE UPPER(TYPE) = 'UNIT' AND UPPER(LEVEL) = 'MOLECULE'
|
||||||
|
) T6 ON T1.MARKET = T6.MARKET AND T1.CMPS_CODE = T6.CMPS_COD
|
||||||
|
LEFT JOIN ( --COUNTINGUNIT--MOLECULE
|
||||||
|
SELECT
|
||||||
|
DISTINCT
|
||||||
|
MARKET,
|
||||||
|
CMPS_COD,
|
||||||
|
CMPS_DES,
|
||||||
|
PACK_COD,
|
||||||
|
PACK_DES,
|
||||||
|
LEVEL,
|
||||||
|
TYPE,
|
||||||
|
CAST(RATIO AS FLOAT) AS RATIO,
|
||||||
|
CAST(STARTTIME AS INT) STARTTIME,
|
||||||
|
CAST(ENDTIME AS INT) ENDTIME
|
||||||
|
FROM DWD.DWD_GND_IMS_TBLBRANDRATIO_BYMONTH
|
||||||
|
WHERE UPPER(TYPE) = 'COUNTINGUNIT' AND UPPER(LEVEL) = 'MOLECULE'
|
||||||
|
) T7 ON T1.MARKET = T7.MARKET
|
||||||
|
AND T1.CMPS_CODE = T7.CMPS_COD
|
||||||
Binary file not shown.
71
EXTERNAL/CHPA/03 DM_TD_EXT_CHPA_MARKET_PACK_MAPPING.sql
Normal file
71
EXTERNAL/CHPA/03 DM_TD_EXT_CHPA_MARKET_PACK_MAPPING.sql
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
-- Databricks notebook source
|
||||||
|
-- CREATE OR REPLACE TABLE DM.DM_TD_EXT_CHPA_MARKET_PACK_MAPPING
|
||||||
|
-- (
|
||||||
|
-- PACK_CODE STRING,
|
||||||
|
-- MARKET STRING,
|
||||||
|
-- CLASS STRING,
|
||||||
|
-- KEY_COMPETITOR STRING,
|
||||||
|
-- MARKET_RATIO STRING,
|
||||||
|
-- STARTTIME STRING,
|
||||||
|
-- ENDTIME STRING,
|
||||||
|
-- VALUE_BRAND_RATIO STRING,
|
||||||
|
-- VALUE_BRAND_RATIO_START STRING,
|
||||||
|
-- VALUE_BRAND_RATIO_END STRING,
|
||||||
|
-- UNIT_BRAND_RATIO STRING,
|
||||||
|
-- UNIT_BRAND_START STRING,
|
||||||
|
-- UNIT_BRAND_END STRING,
|
||||||
|
-- COUNTINGUNIT_BRAND_RATIO STRING,
|
||||||
|
-- COUNTINGUNIT_BRAND_START STRING,
|
||||||
|
-- COUNTINGUNIT_BRAND_END STRING,
|
||||||
|
-- PDOT_BRAND_RATIO STRING,
|
||||||
|
-- PDOT_BRAND_START STRING,
|
||||||
|
-- PDOT_BRAND_END STRING,
|
||||||
|
-- ETL_INSERT_DT TIMESTAMP,
|
||||||
|
-- ETL_UPDATE_DT TIMESTAMP
|
||||||
|
-- )
|
||||||
|
-- USING delta
|
||||||
|
-- -- LOCATION 'abfss://master@azcdatalakeprd.dfs.core.chinacloudapi.cn/DM/dm_td_ext_chpa_market_pack_mapping';
|
||||||
|
-- -- 上面是生产环境location,下面是测试环境location
|
||||||
|
-- LOCATION 'abfss://master@retaildlstoragetest.dfs.core.chinacloudapi.cn/DM/dm_td_ext_chpa_market_pack_mapping';
|
||||||
|
|
||||||
|
-- COMMAND ----------
|
||||||
|
|
||||||
|
INSERT OVERWRITE TABLE DM.DM_TD_EXT_CHPA_MARKET_PACK_MAPPING
|
||||||
|
SELECT DISTINCT
|
||||||
|
T1.PACK_CODE,
|
||||||
|
T1.MARKET,
|
||||||
|
T4.CLASS,
|
||||||
|
T5.KEY_COMPETITOR,
|
||||||
|
T2.MARKET_RATIO,
|
||||||
|
T2.STARTTIME,
|
||||||
|
T2.ENDTIME,
|
||||||
|
T3.VALUE_BRAND_RATIO,
|
||||||
|
T3.VALUE_BRAND_RATIO_START,
|
||||||
|
T3.VALUE_BRAND_RATIO_END,
|
||||||
|
T3.UNIT_BRAND_RATIO,
|
||||||
|
T3.UNIT_BRAND_START,
|
||||||
|
T3.UNIT_BRAND_END,
|
||||||
|
T3.COUNTINGUNIT_BRAND_RATIO,
|
||||||
|
T3.COUNTINGUNIT_BRAND_START,
|
||||||
|
T3.COUNTINGUNIT_BRAND_END,
|
||||||
|
T3.PDOT_BRAND_RATIO,
|
||||||
|
T3.PDOT_BRAND_START,
|
||||||
|
T3.PDOT_BRAND_END,
|
||||||
|
FROM_UTC_TIMESTAMP(CURRENT_TIMESTAMP(), 'UTC+8') AS ETL_INSERT_DT,
|
||||||
|
FROM_UTC_TIMESTAMP(CURRENT_TIMESTAMP(), 'UTC+8') AS ETL_UPDATE_DT
|
||||||
|
FROM
|
||||||
|
DM.DM_TD_EXT_CHPA_PACK2MARKET T1
|
||||||
|
LEFT JOIN DM.DM_TD_EXT_CHPA_MARKET_RATIO T2
|
||||||
|
ON T1.MARKET = T2.MARKET
|
||||||
|
AND T1.PACK_CODE = T2.PACK_CODE
|
||||||
|
LEFT JOIN DM.DM_TD_EXT_CHPA_MARKET_BRAND_RATIO T3
|
||||||
|
ON T1.MARKET = T3.MARKET
|
||||||
|
AND T1.PACK_CODE = T3.PACK_CODE
|
||||||
|
LEFT JOIN DM.DM_TD_EXT_CHPA_PACK2MARKET_CLASS T4
|
||||||
|
ON T1.PACK_CODE = T4.PACK_CODE
|
||||||
|
AND T1.MARKET = T4.MARKET
|
||||||
|
LEFT JOIN DM.DM_TD_EXT_CHPA_PACK2MARKET_KEYCOMPETITOR T5
|
||||||
|
ON T1.PACK_CODE = T5.PACK_CODE
|
||||||
|
AND T1.MARKET = T5.MARKET
|
||||||
|
WHERE
|
||||||
|
UPPER(T1.MARKET) NOT LIKE "%ALL%"
|
||||||
Binary file not shown.
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user