diff --git a/cms_ssd_extract_sql/systemc/liquidlogic_sqlserver_v1.2.0_1_20240731.sql b/cms_ssd_extract_sql/systemc/liquidlogic_sqlserver_v1.2.0_1_20240731.sql index 093ffcb..a02c802 100644 --- a/cms_ssd_extract_sql/systemc/liquidlogic_sqlserver_v1.2.0_1_20240731.sql +++ b/cms_ssd_extract_sql/systemc/liquidlogic_sqlserver_v1.2.0_1_20240731.sql @@ -303,13 +303,14 @@ WITH f903_data_CTE AS ( ROW_NUMBER() OVER (PARTITION BY dim_person_id ORDER BY no_upn_code DESC) AS rn FROM HDM.Child_Social.fact_903_data + WHERE + no_upn_code IS NOT NULL -- sparse data in this field, filter for performance ) --- Insert data INSERT INTO ssd_development.ssd_person ( pers_legacy_id, pers_person_id, pers_sex, - pers_gender, + pers_gender, -- Ensure this is mapped to GENDER_MAIN_CODE pers_ethnicity, pers_dob, pers_common_child_id, @@ -323,35 +324,84 @@ INSERT INTO ssd_development.ssd_person ( ) SELECT p.LEGACY_ID, - CAST(p.DIM_PERSON_ID AS NVARCHAR(48)), -- Ensure DIM_PERSON_ID is cast to NVARCHAR(48) - p.GENDER_MAIN_CODE, - p.NHS_NUMBER, + CAST(p.DIM_PERSON_ID AS NVARCHAR(48)), -- Ensure DIM_PERSON_ID is cast to NVARCHAR(48) + p.GENDER_MAIN_CODE AS pers_sex, -- Assuming pers_sex is meant to map to GENDER_MAIN_CODE + p.GENDER_MAIN_CODE, -- Ensure this maps correctly to pers_gender p.ETHNICITY_MAIN_CODE, CASE WHEN (p.DOB_ESTIMATED) = 'N' - THEN p.BIRTH_DTTM -- Set to BIRTH_DTTM when DOB_ESTIMATED = 'N' + THEN p.BIRTH_DTTM -- Set to BIRTH_DTTM when DOB_ESTIMATED = 'N' ELSE NULL - END, -- or NULL - NULL AS pers_common_child_id, -- Set to NULL as default(dev) / or set to NHS num + END, -- or NULL + NULL AS pers_common_child_id, -- Set to NULL as default(dev) / or set to NHS num COALESCE(f903.NO_UPN_CODE, 'SSD_PH') AS NO_UPN_CODE, -- Use NO_UPN_CODE from f903 or 'SSD_PH' as placeholder p.EHM_SEN_FLAG, CASE WHEN (p.DOB_ESTIMATED) = 'Y' - THEN p.BIRTH_DTTM -- Set to BIRTH_DTTM when DOB_ESTIMATED = 'Y' + THEN p.BIRTH_DTTM -- Set to BIRTH_DTTM when DOB_ESTIMATED = 'Y' ELSE NULL - END, -- or NULL + END, -- or NULL p.DEATH_DTTM, CASE - WHEN p.GENDER_MAIN_CODE <> 'M' AND -- Assumption that if male is not mother + WHEN p.GENDER_MAIN_CODE <> 'M' AND -- Assumption that if male is not mother EXISTS (SELECT 1 FROM HDM.Child_Social.FACT_PERSON_RELATION fpr WHERE fpr.DIM_PERSON_ID = p.DIM_PERSON_ID AND - fpr.DIM_LOOKUP_RELTN_TYPE_CODE = 'CHI') -- check for child relation only + fpr.DIM_LOOKUP_RELTN_TYPE_CODE = 'CHI') -- check for child relation only THEN 'Y' ELSE NULL -- No child relation found END, p.NATNL_CODE, 1 - FROM HDM.Child_Social.DIM_PERSON AS p + +-- -- Insert data +-- INSERT INTO ssd_development.ssd_person ( +-- pers_legacy_id, +-- pers_person_id, +-- pers_sex, +-- pers_gender, +-- pers_ethnicity, +-- pers_dob, +-- pers_common_child_id, +-- pers_upn_unknown, +-- pers_send_flag, +-- pers_expected_dob, +-- pers_death_date, +-- pers_is_mother, +-- pers_nationality, +-- ssd_flag +-- ) +-- SELECT +-- p.LEGACY_ID, +-- CAST(p.DIM_PERSON_ID AS NVARCHAR(48)), -- Ensure DIM_PERSON_ID is cast to NVARCHAR(48) +-- 'SSD_PH' AS pers_sex, -- +-- p.GENDER_MAIN_CODE, +-- p.NHS_NUMBER, +-- p.ETHNICITY_MAIN_CODE, +-- CASE WHEN (p.DOB_ESTIMATED) = 'N' +-- THEN p.BIRTH_DTTM -- Set to BIRTH_DTTM when DOB_ESTIMATED = 'N' +-- ELSE NULL +-- END, -- or NULL +-- NULL AS pers_common_child_id, -- Set to NULL as default(dev) / or set to NHS num +-- COALESCE(f903.NO_UPN_CODE, 'SSD_PH') AS NO_UPN_CODE, -- Use NO_UPN_CODE from f903 or 'SSD_PH' as placeholder +-- p.EHM_SEN_FLAG, +-- CASE WHEN (p.DOB_ESTIMATED) = 'Y' +-- THEN p.BIRTH_DTTM -- Set to BIRTH_DTTM when DOB_ESTIMATED = 'Y' +-- ELSE NULL +-- END, -- or NULL +-- p.DEATH_DTTM, +-- CASE +-- WHEN p.GENDER_MAIN_CODE <> 'M' AND -- Assumption that if male is not mother +-- EXISTS (SELECT 1 FROM HDM.Child_Social.FACT_PERSON_RELATION fpr +-- WHERE fpr.DIM_PERSON_ID = p.DIM_PERSON_ID AND +-- fpr.DIM_LOOKUP_RELTN_TYPE_CODE = 'CHI') -- check for child relation only +-- THEN 'Y' +-- ELSE NULL -- No child relation found +-- END, +-- p.NATNL_CODE, +-- 1 + +-- FROM +-- HDM.Child_Social.DIM_PERSON AS p -- [TESTING][PLACEHOLDER] 903 table refresh only in reporting period? LEFT JOIN ( diff --git a/cms_ssd_extract_sql/systemc/liquidlogic_sqlserver_v1.2.0_1_20240731_temp.sql b/cms_ssd_extract_sql/systemc/liquidlogic_sqlserver_v1.2.0_1_20240731_temp.sql index 1b72987..c6b8062 100644 --- a/cms_ssd_extract_sql/systemc/liquidlogic_sqlserver_v1.2.0_1_20240731_temp.sql +++ b/cms_ssd_extract_sql/systemc/liquidlogic_sqlserver_v1.2.0_1_20240731_temp.sql @@ -299,6 +299,8 @@ WITH f903_data_CTE AS ( ROW_NUMBER() OVER (PARTITION BY dim_person_id ORDER BY no_upn_code DESC) AS rn FROM HDM.Child_Social.fact_903_data + WHERE + no_upn_code IS NOT NULL -- sparse data in this field, filter for performance ) -- Insert data INSERT INTO #ssd_person ( diff --git a/cms_ssd_extract_sql/systemc/liquidlogic_sqlserver_v1.2.0_1_non_core_updates.sql b/cms_ssd_extract_sql/systemc/liquidlogic_sqlserver_v1.2.0_1_non_core_updates.sql index b9e4ed5..caf88ba 100644 --- a/cms_ssd_extract_sql/systemc/liquidlogic_sqlserver_v1.2.0_1_non_core_updates.sql +++ b/cms_ssd_extract_sql/systemc/liquidlogic_sqlserver_v1.2.0_1_non_core_updates.sql @@ -73,16 +73,18 @@ PRINT 'Updating table: ' + @TableName; -- Insert new records into ssd_person with ssd_flag set to 0 +-- CTE to get a no_upn_code WITH f903_data_CTE AS ( SELECT - -- Get the most recent no_upn_code if exists + -- get the most recent no_upn_code if exists dim_person_id, no_upn_code, ROW_NUMBER() OVER (PARTITION BY dim_person_id ORDER BY no_upn_code DESC) AS rn FROM - Child_Social.fact_903_data + HDM.Child_Social.fact_903_data + WHERE + no_upn_code IS NOT NULL -- sparse data in this field, filter for performance ) - INSERT INTO ssd_development.ssd_person ( pers_legacy_id, pers_person_id, diff --git a/tools-ssd_reductive_views/cla_snapshot-DtoI-1647.sql b/tools-ssd_reductive_views/cla_snapshot-DtoI-1647.sql new file mode 100644 index 0000000..d32715c --- /dev/null +++ b/tools-ssd_reductive_views/cla_snapshot-DtoI-1647.sql @@ -0,0 +1,148 @@ +/* +Reductive views extract SQL - CLA snapshot +#DtoI-1647 +*/ + +-- CTE most recent cla_episode +WITH RecentCLA AS ( + SELECT + clae_cla_episode_id, + clae_person_id, + clae_cla_placement_id, + clae_cla_episode_start_date, + clae_cla_episode_ceased, + clae_cla_episode_ceased_reason, + clae_cla_id, + clae_referral_id, + ROW_NUMBER() OVER (PARTITION BY clae_person_id ORDER BY clae_cla_episode_start_date DESC) AS rn + FROM + ssd_development.ssd_cla_episodes +), +-- CTE most recent placement +RecentPlacement AS ( + SELECT + clap_cla_placement_id, + clap_cla_id, + clap_person_id, + clap_cla_placement_start_date, + clap_cla_placement_type, + ROW_NUMBER() OVER (PARTITION BY clap_person_id ORDER BY clap_cla_placement_start_date DESC) AS rn + FROM + ssd_development.ssd_cla_placement +), +-- CTE most recent legal status +RecentLegalStatus AS ( + SELECT + lega_legal_status_id, + lega_person_id, + lega_legal_status, + lega_legal_status_start_date, + lega_legal_status_end_date, + ROW_NUMBER() OVER (PARTITION BY lega_person_id ORDER BY lega_legal_status_start_date DESC) AS rn + FROM + ssd_development.ssd_legal_status +), +-- CTE most recent health check +RecentHealthCheck AS ( + SELECT + clah_health_check_id, + clah_person_id, + clah_health_check_type, + clah_health_check_date, + clah_health_check_status, + ROW_NUMBER() OVER (PARTITION BY clah_person_id ORDER BY clah_health_check_date DESC) AS rn + FROM + ssd_development.ssd_cla_health +), +-- CTE most recent visit +RecentVisit AS ( + SELECT + clav_cla_visit_id, + clav_cla_id, + clav_person_id, + clav_cla_visit_date, + clav_cla_visit_seen, + clav_cla_visit_seen_alone, + ROW_NUMBER() OVER (PARTITION BY clav_person_id ORDER BY clav_cla_visit_date DESC) AS rn + FROM + ssd_development.ssd_cla_visits +), +-- CTE most recent review +RecentReview AS ( + SELECT + clar_cla_review_id, + clar_cla_id, + clar_cla_review_due_date, + clar_cla_review_date, + clar_cla_review_cancelled, + clar_cla_review_participation, + ROW_NUMBER() OVER (PARTITION BY clar_cla_id ORDER BY clar_cla_review_date DESC) AS rn + FROM + ssd_development.ssd_cla_reviews +) + +SELECT + -- base ssd_person details + p.pers_legacy_id AS legacy_id, + p.pers_person_id AS person_id, + p.pers_sex AS sex, + p.pers_gender AS gender, + p.pers_ethnicity AS ethnicity, + FORMAT(p.pers_dob, 'dd/MM/yyyy') AS pers_dob, + p.pers_send_flag AS send_flag, + FORMAT(p.pers_expected_dob, 'dd/MM/yyyy') AS pers_expected_dob, + FORMAT(p.pers_death_date, 'dd/MM/yyyy') AS pers_death_date, + p.pers_is_mother AS is_mother, + p.pers_nationality AS nationality, + p.ssd_flag AS is_core_ssd_record, + + -- most recent ssd_cla_episodes + cla.clae_cla_episode_id, -- [TESTING] + cla.clae_cla_placement_id, -- [TESTING] + FORMAT(cla.clae_cla_episode_start_date, 'dd/MM/yyyy') AS cla_episode_start_date, + cla.clae_cla_id, -- [TESTING] + cla.clae_referral_id, -- [TESTING] + + -- most recent ssd_cla_placement + pl.clap_cla_placement_id, -- [TESTING] + FORMAT(pl.clap_cla_placement_start_date, 'dd/MM/yyyy') AS cla_placement_start_date, + pl.clap_cla_placement_type, + + -- most recent ssd_legal_status + ls.lega_legal_status, + FORMAT(ls.lega_legal_status_start_date, 'dd/MM/yyyy') AS legal_status_start_date, + FORMAT(ls.lega_legal_status_end_date, 'dd/MM/yyyy') AS legal_status_end_date, + + -- most recent ssd_cla_health + hc.clah_health_check_type, + FORMAT(hc.clah_health_check_date, 'dd/MM/yyyy') AS health_check_date, + hc.clah_health_check_status, + + -- most recent ssd_cla_visit + FORMAT(v.clav_cla_visit_date, 'dd/MM/yyyy') AS clav_cla_visit_date, + v.clav_cla_visit_seen, + v.clav_cla_visit_seen_alone, + + -- ssd_cla_reviews + FORMAT(r.clar_cla_review_date, 'dd/MM/yyyy') AS cla_review_date, + FORMAT(r.clar_cla_review_due_date, 'dd/MM/yyyy') AS cla_review_due_date, + r.clar_cla_review_cancelled, + r.clar_cla_review_participation + + +FROM + ssd_development.ssd_person AS p + +LEFT JOIN + RecentCLA AS cla ON p.pers_person_id = cla.clae_person_id AND cla.rn = 1 +LEFT JOIN + RecentPlacement AS pl ON p.pers_person_id = pl.clap_person_id AND pl.rn = 1 +LEFT JOIN + RecentLegalStatus AS ls ON p.pers_person_id = ls.lega_person_id AND ls.rn = 1 +LEFT JOIN + RecentHealthCheck AS hc ON p.pers_person_id = hc.clah_person_id AND hc.rn = 1 +LEFT JOIN + RecentVisit AS v ON p.pers_person_id = v.clav_person_id AND v.rn = 1 + +LEFT JOIN + RecentReview AS r ON cla.clae_cla_id = r.clar_cla_id AND r.rn = 1; \ No newline at end of file