-- Databricks notebook source
-- CREATE OR REPLACE TABLE DM.DM_TF_EXT_CHPA_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_chpa_sales';
-- -- 上面是生产环境location，下面是测试环境location
-- LOCATION 'abfss://master@retaildlstoragetest.dfs.core.chinacloudapi.cn/DM/dm_tf_ext_chpa_sales';

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

CREATE OR REPLACE TEMPORARY VIEW CHPA_SALES AS
  SELECT
    A.YYYYMM,
    A.PACK_COD AS PACK_CODE,
    A.AUDIT_COD AS AUDIT_CODE,
    'IQVIA-CHPA(Monthly)' DATA_SOURCE,
    -------------------------------------
    SUM(SALES_UNIT_CAL) SALES_UNIT_CAL,
    SUM(SALES_UNIT_CAL_LY) as SALES_UNIT_CAL_LY,
    SUM(SALES_VALUE_CAL) SALES_VALUE_CAL,
    SUM(SALES_VALUE_CAL_LY) as SALES_VALUE_CAL_LY,
    SUM(CONUTING_UNIT) CONUTING_UNIT,
    SUM(CONUTING_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数据标签-------------------------
    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 --可以移除，报告没有使用
  -------------------------------------
  FROM
    DM.DM_IMS_TF_SALES A
  where
    A.YYYYMM >= '202201'
    -- AND A.MARKET = 'IMS ALL Market'
  GROUP BY
    A.YYYYMM,
    A.PACK_COD,
    A.AUDIT_COD

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

INSERT OVERWRITE TABLE DM.DM_TF_EXT_CHPA_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
  CHPA_SALES;