113 lines
3.5 KiB
SQL
113 lines
3.5 KiB
SQL
-- 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
|