refactor CHPA hierarchy SQL

This commit is contained in:
2026-08-20 18:22:16 +08:00
commit 3f3dc443da
5 changed files with 592 additions and 0 deletions
@@ -0,0 +1,86 @@
-- 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)
;
@@ -0,0 +1,70 @@
-- Databricks notebook source
-- =============================================================================
-- Purpose : Build the flattened IMS NFC level 1-3 hierarchy.
-- Source : dwd.dwd_ims_td_new_form_class
-- Target : dwd.dwd_ims_nfc_hierarchy
-- Grain : One row per hierarchy path rooted at NFC1_CODE; lower levels may be null.
-- Write mode : Full refresh (INSERT OVERWRITE).
-- Downstream : dwd.dwd_ims_td_pack_property, dws.dws_ims_td_nfc_cn
-- Notes : Parent-code matching is preserved from the legacy script.
-- =============================================================================
INSERT OVERWRITE TABLE dwd.dwd_ims_nfc_hierarchy (
NFC1_ID,
NFC1_CODE,
NFC1_DES,
NFC2_ID,
NFC2_CODE,
NFC2_DES,
NFC3_ID,
NFC3_CODE,
NFC3_DES
)
WITH new_form_class AS (
SELECT
newformclass_id,
newformclass_code,
newformclass_name,
newformclass_level
FROM dwd.dwd_ims_td_new_form_class
),
nfc_level_1 AS (
SELECT
newformclass_id AS nfc1_id,
newformclass_code AS nfc1_code,
newformclass_name AS nfc1_des
FROM new_form_class
WHERE newformclass_level = '1'
),
nfc_level_2 AS (
SELECT
newformclass_id AS nfc2_id,
newformclass_code AS nfc2_code,
newformclass_name AS nfc2_des
FROM new_form_class
WHERE newformclass_level = '2'
),
nfc_level_3 AS (
SELECT
newformclass_id AS nfc3_id,
newformclass_code AS nfc3_code,
newformclass_name AS nfc3_des
FROM new_form_class
WHERE newformclass_level = '3'
)
SELECT
nfc_level_1.nfc1_id,
nfc_level_1.nfc1_code,
nfc_level_1.nfc1_des,
nfc_level_2.nfc2_id,
nfc_level_2.nfc2_code,
nfc_level_2.nfc2_des,
nfc_level_3.nfc3_id,
nfc_level_3.nfc3_code,
nfc_level_3.nfc3_des
FROM nfc_level_1
LEFT JOIN nfc_level_2
ON nfc_level_1.nfc1_code = LEFT(nfc_level_2.nfc2_code, 1)
LEFT JOIN nfc_level_3
ON nfc_level_2.nfc2_code = LEFT(nfc_level_3.nfc3_code, 2)
;