-- Databricks notebook source -- ============================================================================= -- Purpose : Build the flattened IMS ATC level 1-4 hierarchy. -- Source : dwd.dwd_ims_td_therapeutic_class -- Target : dwd.dwd_ims_atc_hierarchy -- Grain : One row per hierarchy path rooted at ATC1_CODE; lower levels may be null. -- Write mode : Full refresh (INSERT OVERWRITE). -- Downstream : dwd.dwd_ims_td_pack_property, dws.dws_ims_td_atc_cn -- Notes : Parent-code matching is preserved from the legacy script. -- ============================================================================= INSERT OVERWRITE TABLE dwd.dwd_ims_atc_hierarchy ( ATC1_ID, ATC1_CODE, ATC1_DES, ATC2_ID, ATC2_CODE, ATC2_DES, ATC3_ID, ATC3_CODE, ATC3_DES, ATC4_ID, ATC4_CODE, ATC4_DES ) WITH therapeutic_class AS ( SELECT therapeutic_id, therapeutic_code, therapeutic_name, therapeutic_level FROM dwd.dwd_ims_td_therapeutic_class ), atc_level_1 AS ( SELECT therapeutic_id AS atc1_id, therapeutic_code AS atc1_code, therapeutic_name AS atc1_des FROM therapeutic_class WHERE therapeutic_level = '1' ), atc_level_2 AS ( SELECT therapeutic_id AS atc2_id, therapeutic_code AS atc2_code, therapeutic_name AS atc2_des FROM therapeutic_class WHERE therapeutic_level = '2' ), atc_level_3 AS ( SELECT therapeutic_id AS atc3_id, therapeutic_code AS atc3_code, therapeutic_name AS atc3_des FROM therapeutic_class WHERE therapeutic_level = '3' ), atc_level_4 AS ( SELECT therapeutic_id AS atc4_id, therapeutic_code AS atc4_code, therapeutic_name AS atc4_des FROM therapeutic_class WHERE therapeutic_level = '4' ) SELECT atc_level_1.atc1_id, atc_level_1.atc1_code, atc_level_1.atc1_des, atc_level_2.atc2_id, atc_level_2.atc2_code, atc_level_2.atc2_des, atc_level_3.atc3_id, atc_level_3.atc3_code, atc_level_3.atc3_des, atc_level_4.atc4_id, atc_level_4.atc4_code, atc_level_4.atc4_des FROM atc_level_1 LEFT JOIN atc_level_2 ON atc_level_1.atc1_code = LEFT(atc_level_2.atc2_code, 1) LEFT JOIN atc_level_3 ON atc_level_2.atc2_code = LEFT(atc_level_3.atc3_code, 3) LEFT JOIN atc_level_4 ON atc_level_3.atc3_code = LEFT(atc_level_4.atc4_code, 4) ;