-- 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)
;