Files
REFACTOR-MA/sql/chpa/01_dwd/02_dwd_ims_nfc_hierarchy.sql
T
2026-08-20 18:22:16 +08:00

71 lines
2.0 KiB
SQL

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