From ecf2a8244eeb3d413c4ce9ea61f1899d26e122cc Mon Sep 17 00:00:00 2001 From: Rob Harrison <48765695+robjharrison@users.noreply.github.com> Date: Wed, 20 Dec 2023 09:25:22 +0000 Subject: [PATCH 1/2] 201223 cla_visits pk fix --- tools/cms_extract/create_ssd.sql | 194 ++++++++++---------- tools/cms_extract/create_ssd_tmp_tables.sql | 34 ++-- 2 files changed, 120 insertions(+), 108 deletions(-) diff --git a/tools/cms_extract/create_ssd.sql b/tools/cms_extract/create_ssd.sql index c85a1291..8a1d1e42 100644 --- a/tools/cms_extract/create_ssd.sql +++ b/tools/cms_extract/create_ssd.sql @@ -826,87 +826,6 @@ PRINT 'Test Progress Counter: ' + CAST(@TestProgress AS NVARCHAR(10)); -/* -============================================================================= -Object Name: ssd_early_help_episodes -Description: -Author: D2I -Last Modified Date: 22/11/23 -DB Compatibility: SQL Server 2014+|... -Version: 0.9 -Status: [*Dev, Testing, Release, Blocked, AwaitingReview, Backlog] -Remarks: -Dependencies: -- ssd_person -- FACT_CAF_EPISODE -============================================================================= -*/ --- [TESTING] Create marker -SET @TableName = N'ssd_early_help_episodes'; -PRINT 'Creating table: ' + @TableName; - - --- Check if exists & drop -IF OBJECT_ID('ssd_early_help_episodes') IS NOT NULL DROP TABLE ssd_early_help_episodes; - --- Create structure -CREATE TABLE ssd_early_help_episodes ( - earl_episode_id NVARCHAR(48) PRIMARY KEY, - earl_person_id NVARCHAR(48), - earl_episode_start_date DATETIME, - earl_episode_end_date DATETIME, - earl_episode_reason NVARCHAR(MAX), - earl_episode_end_reason NVARCHAR(MAX), - earl_episode_organisation NVARCHAR(MAX), - earl_episode_worker_id NVARCHAR(48) -); - --- Insert data -INSERT INTO ssd_early_help_episodes ( - earl_episode_id, - earl_person_id, - earl_episode_start_date, - earl_episode_end_date, - earl_episode_reason, - earl_episode_end_reason, - earl_episode_organisation, - earl_episode_worker_id -) -SELECT - cafe.FACT_CAF_EPISODE_ID, - cafe.DIM_PERSON_ID, - cafe.EPISODE_START_DTTM, - cafe.EPISODE_END_DTTM, - cafe.START_REASON, - cafe.DIM_LOOKUP_CAF_EP_ENDRSN_ID_CODE, - cafe.DIM_LOOKUP_ORIGINATING_ORGANISATION_CODE, - 'PLACEHOLDER_DATA' -- [PLACEHOLDER_DATA] [TESTING] -FROM - Child_Social.FACT_CAF_EPISODE AS cafe - -WHERE EXISTS - ( -- only ssd relevant records - SELECT 1 - FROM ssd_person p - WHERE p.pers_person_id = cafe.DIM_PERSON_ID - ); - --- Create index(es) -CREATE NONCLUSTERED INDEX IDX_ssd_early_help_episodes_person_id ON ssd_early_help_episodes(earl_person_id); - --- Create constraint(s) -ALTER TABLE ssd_early_help_episodes ADD CONSTRAINT FK_earl_to_person -FOREIGN KEY (earl_person_id) REFERENCES ssd_person(pers_person_id); - - - - --- [TESTING] Increment /print progress -SET @TestProgress = @TestProgress + 1; -PRINT 'Table created: ' + @TableName; -PRINT 'Test Progress Counter: ' + CAST(@TestProgress AS NVARCHAR(10)); - - /* ============================================================================= @@ -2633,6 +2552,7 @@ IF OBJECT_ID('ssd_cla_visits', 'U') IS NOT NULL DROP TABLE ssd_cla_visits; -- Create structure CREATE TABLE ssd_cla_visits ( + clav_table_id UNIQUEIDENTIFIER PRIMARY KEY, clav_casenote_id NVARCHAR(48) PRIMARY KEY, clav_cla_id NVARCHAR(48), clav_cla_visit_id NVARCHAR(48), @@ -2644,6 +2564,7 @@ CREATE TABLE ssd_cla_visits ( -- Insert data INSERT INTO ssd_cla_visits ( + clav_table_id, clav_casenote_id, clav_cla_id, clav_cla_visit_id, @@ -2653,7 +2574,8 @@ INSERT INTO ssd_cla_visits ( clav_cla_visit_seen_alone ) SELECT - clav.FACT_CASENOTE_ID AS clav_casenote_id, + DEFAULT NEWID() AS clav_table_id, + clav.FACT_CASENOTE_ID AS clav_casenote_id, clav.FACT_CLA_ID AS clav_cla_id, clav.FACT_CLA_VISIT_ID AS clav_cla_visit_id, ceps.FACT_CARE_EPISODES_ID AS clav_cla_episode_id, @@ -3172,12 +3094,12 @@ SELECT fc.START_DTTM AS perm_entered_care_date, fa.DECISION_DTTM AS perm_ffa_cp_decision_date, fa.PLACEMENT_ORDER_DTTM AS perm_placement_order_date, - fcpAdoption.START_DTTM AS perm_placed_for_adoption_date, + fcpl.START_DTTM AS perm_placed_for_adoption_date, fa.MATCHING_DTTM AS perm_matched_date, fa.ADOPTED_BY_CARER_FLAG AS perm_adopted_by_carer_flag, fa.FOSTER_TO_ADOPT_DTTM AS perm_placed_ffa_cp_date, fa.NO_LONGER_PLACED_DTTM AS perm_decision_reversed_date, - fcpAdopted.START_DTTM AS perm_placed_foster_carer_date, + fcpl.START_DTTM AS perm_placed_foster_carer_date, fa.SIBLING_GROUP AS perm_part_of_sibling_group, fa.NUMBER_TOGETHER AS perm_siblings_placed_together, fa.NUMBER_APART AS perm_siblings_placed_apart, @@ -3198,22 +3120,24 @@ SELECT fa.ADOPTION_SOCIAL_WORKER_ID AS perm_adoption_worker, -- Note that duplicate -1 seen in raw data fa.ALLOCATED_CASE_WORKER_ID AS perm_allocated_worker -- Note that duplicate -1 seen in raw data FROM - FACT_ADOPTION AS fa + Child_Social.FACT_ADOPTION AS fa -LEFT JOIN FACT_CLA AS fc +LEFT JOIN Child_Social.FACT_CLA AS fc ON fa.FACT_CLA_ID = fc.FACT_CLA_ID -- towards perm_adm_decision_date -LEFT JOIN FACT_CLA_PLACEMENT AS fcpAdoption - ON fa.FACT_CLA_ID = fcpAdoption.FACT_CLA_ID -- towards perm_ffa_cp_decision_date - AND fcpAdoption.DIM_LOOKUP_PLACEMENT_TYPE_DESC LIKE '%placed for adoption%' -- towards perm_placed_for_adoption_date - AND fcpAdoption.ADOPTED_BY_CARER_FLAG = 'Y' -- towards perm_placed_foster_carer_date -LEFT JOIN FACT_CARE_EPISODES AS fce +LEFT JOIN Child_Social.FACT_CLA_PLACEMENT AS fcpl + ON fa.FACT_CLA_ID = fcpl .FACT_CLA_ID -- towards perm_ffa_cp_decision_date + AND fcpl .DIM_LOOKUP_PLACEMENT_TYPE_DESC LIKE '%placed for adoption%' -- towards perm_placed_for_adoption_date + AND fcpl .ADOPTED_BY_CARER_FLAG = 'Y' -- towards perm_placed_foster_carer_date +LEFT JOIN Child_Social.FACT_CARE_EPISODES AS fce ON fa.FACT_CLA_ID = fce.FACT_CLA_ID -- towards perm_placement_provider_urn -LEFT JOIN FACT_LEGAL_STATUS AS fls +LEFT JOIN Child_Social.FACT_LEGAL_STATUS AS fls ON fa.FACT_CLA_ID = fls.FACT_CLA_ID AND fls.DIM_LOOKUP_LGL_STATUS_CODE IN ('0154', '0156', 'SGO', '0512') -- towards perm_permanence_order_type - AND fa.ADOPTION_DTTM IS NOT NULL; -- so only if there is a permanence order + AND fa.ADOPTION_DTTM IS NOT NULL; -- and only if there is a permanence order -WHERE EXISTS +WHERE +fa.FACT_ADOPTION_ID <> -1 -- Filter out -1 values +AND EXISTS ( -- only ssd relevant records SELECT 1 FROM ssd_person p @@ -3764,6 +3688,88 @@ PRINT 'Test Progress Counter: ' + CAST(@TestProgress AS NVARCHAR(10)); +/* +============================================================================= +Object Name: ssd_early_help_episodes +Description: +Author: D2I +Last Modified Date: 22/11/23 +DB Compatibility: SQL Server 2014+|... +Version: 0.9 +Status: [*Dev, Testing, Release, Blocked, AwaitingReview, Backlog] +Remarks: +Dependencies: +- ssd_person +- FACT_CAF_EPISODE +============================================================================= +*/ +-- [TESTING] Create marker +SET @TableName = N'ssd_early_help_episodes'; +PRINT 'Creating table: ' + @TableName; + + +-- Check if exists & drop +IF OBJECT_ID('ssd_early_help_episodes') IS NOT NULL DROP TABLE ssd_early_help_episodes; + +-- Create structure +CREATE TABLE ssd_early_help_episodes ( + earl_episode_id NVARCHAR(48) PRIMARY KEY, + earl_person_id NVARCHAR(48), + earl_episode_start_date DATETIME, + earl_episode_end_date DATETIME, + earl_episode_reason NVARCHAR(MAX), + earl_episode_end_reason NVARCHAR(MAX), + earl_episode_organisation NVARCHAR(MAX), + earl_episode_worker_id NVARCHAR(48) +); + +-- Insert data +INSERT INTO ssd_early_help_episodes ( + earl_episode_id, + earl_person_id, + earl_episode_start_date, + earl_episode_end_date, + earl_episode_reason, + earl_episode_end_reason, + earl_episode_organisation, + earl_episode_worker_id +) +SELECT + cafe.FACT_CAF_EPISODE_ID, + cafe.DIM_PERSON_ID, + cafe.EPISODE_START_DTTM, + cafe.EPISODE_END_DTTM, + cafe.START_REASON, + cafe.DIM_LOOKUP_CAF_EP_ENDRSN_ID_CODE, + cafe.DIM_LOOKUP_ORIGINATING_ORGANISATION_CODE, + 'PLACEHOLDER_DATA' -- [PLACEHOLDER_DATA] [TESTING] +FROM + Child_Social.FACT_CAF_EPISODE AS cafe + +WHERE EXISTS + ( -- only ssd relevant records + SELECT 1 + FROM ssd_person p + WHERE p.pers_person_id = cafe.DIM_PERSON_ID + ); + +-- Create index(es) +CREATE NONCLUSTERED INDEX IDX_ssd_early_help_episodes_person_id ON ssd_early_help_episodes(earl_person_id); + +-- Create constraint(s) +ALTER TABLE ssd_early_help_episodes ADD CONSTRAINT FK_earl_to_person +FOREIGN KEY (earl_person_id) REFERENCES ssd_person(pers_person_id); + + + + +-- [TESTING] Increment /print progress +SET @TestProgress = @TestProgress + 1; +PRINT 'Table created: ' + @TableName; +PRINT 'Test Progress Counter: ' + CAST(@TestProgress AS NVARCHAR(10)); + + + /* ============================================================================= Object Name: ssd_ehcp_assessment diff --git a/tools/cms_extract/create_ssd_tmp_tables.sql b/tools/cms_extract/create_ssd_tmp_tables.sql index da62076e..110fa1eb 100644 --- a/tools/cms_extract/create_ssd_tmp_tables.sql +++ b/tools/cms_extract/create_ssd_tmp_tables.sql @@ -2486,6 +2486,7 @@ IF OBJECT_ID('tempdb..#ssd_cla_visits', 'U') IS NOT NULL DROP TABLE #ssd_cla_vis -- Create structure CREATE TABLE #ssd_cla_visits ( + clav_table_id UNIQUEIDENTIFIER PRIMARY KEY, clav_casenote_id NVARCHAR(48) PRIMARY KEY, clav_cla_id NVARCHAR(48), clav_cla_visit_id NVARCHAR(48), @@ -2497,6 +2498,7 @@ CREATE TABLE #ssd_cla_visits ( -- Insert data INSERT INTO #ssd_cla_visits ( + clav_table_id, clav_casenote_id, clav_cla_id, clav_cla_visit_id, @@ -2506,7 +2508,8 @@ INSERT INTO #ssd_cla_visits ( clav_cla_visit_seen_alone ) SELECT - clav.FACT_CASENOTE_ID AS clav_casenote_id, + DEFAULT NEWID() AS clav_table_id, + clav.FACT_CASENOTE_ID AS clav_casenote_id, clav.FACT_CLA_ID AS clav_cla_id, clav.FACT_CLA_VISIT_ID AS clav_cla_visit_id, ceps.FACT_CARE_EPISODES_ID AS clav_cla_episode_id, @@ -2806,12 +2809,12 @@ SELECT fc.START_DTTM AS perm_entered_care_date, fa.DECISION_DTTM AS perm_ffa_cp_decision_date, fa.PLACEMENT_ORDER_DTTM AS perm_placement_order_date, - fcpAdoption.START_DTTM AS perm_placed_for_adoption_date, + fcpl.START_DTTM AS perm_placed_for_adoption_date, fa.MATCHING_DTTM AS perm_matched_date, fa.ADOPTED_BY_CARER_FLAG AS perm_adopted_by_carer_flag, fa.FOSTER_TO_ADOPT_DTTM AS perm_placed_ffa_cp_date, fa.NO_LONGER_PLACED_DTTM AS perm_decision_reversed_date, - fcpAdopted.START_DTTM AS perm_placed_foster_carer_date, + fcpl.START_DTTM AS perm_placed_foster_carer_date, fa.SIBLING_GROUP AS perm_part_of_sibling_group, fa.NUMBER_TOGETHER AS perm_siblings_placed_together, fa.NUMBER_APART AS perm_siblings_placed_apart, @@ -2832,29 +2835,32 @@ SELECT fa.ADOPTION_SOCIAL_WORKER_ID AS perm_adoption_worker, -- Note that duplicate -1 seen in raw data fa.ALLOCATED_CASE_WORKER_ID AS perm_allocated_worker -- Note that duplicate -1 seen in raw data FROM - FACT_ADOPTION AS fa + Child_Social.FACT_ADOPTION AS fa -LEFT JOIN FACT_CLA AS fc +LEFT JOIN Child_Social.FACT_CLA AS fc ON fa.FACT_CLA_ID = fc.FACT_CLA_ID -- towards perm_adm_decision_date -LEFT JOIN FACT_CLA_PLACEMENT AS fcpAdoption - ON fa.FACT_CLA_ID = fcpAdoption.FACT_CLA_ID -- towards perm_ffa_cp_decision_date - AND fcpAdoption.DIM_LOOKUP_PLACEMENT_TYPE_DESC LIKE '%placed for adoption%' -- towards perm_placed_for_adoption_date - AND fcpAdoption.ADOPTED_BY_CARER_FLAG = 'Y' -- towards perm_placed_foster_carer_date -LEFT JOIN FACT_CARE_EPISODES AS fce +LEFT JOIN Child_Social.FACT_CLA_PLACEMENT AS fcpl + ON fa.FACT_CLA_ID = fcpl.FACT_CLA_ID -- towards perm_ffa_cp_decision_date + AND fcpl.DIM_LOOKUP_PLACEMENT_TYPE_DESC LIKE '%placed for adoption%' -- towards perm_placed_for_adoption_date + AND fa.ADOPTED_BY_CARER_FLAG = 'Y' -- towards perm_placed_foster_carer_date [TESTING... should this be on fa. ?] +LEFT JOIN Child_Social.FACT_CARE_EPISODES AS fce ON fa.FACT_CLA_ID = fce.FACT_CLA_ID -- towards perm_placement_provider_urn -LEFT JOIN FACT_LEGAL_STATUS AS fls +LEFT JOIN Child_Social.FACT_LEGAL_STATUS AS fls ON fa.FACT_CLA_ID = fls.FACT_CLA_ID AND fls.DIM_LOOKUP_LGL_STATUS_CODE IN ('0154', '0156', 'SGO', '0512') -- towards perm_permanence_order_type - AND fa.ADOPTION_DTTM IS NOT NULL; -- so only if there is a permanence order + AND fa.ADOPTION_DTTM IS NOT NULL; -- and only if there is a permanence order -WHERE EXISTS +WHERE +fa.FACT_ADOPTION_ID <> -1 -- Filter out -1 values +AND EXISTS ( -- only ssd relevant records SELECT 1 - FROM #ssd_person p + FROM ssd_person p WHERE p.pers_person_id = fa.DIM_PERSON_ID ); + -- -- Add constraint(s) -- ALTER TABLE #ssd_permanence ADD CONSTRAINT FK_perm_person_id -- FOREIGN KEY (perm_person_id) REFERENCES #ssd_cla_episodes(clae_person_id); From 737e4cc5d486f215538faec6d358cd4ece82a111 Mon Sep 17 00:00:00 2001 From: Rob Harrison <48765695+robjharrison@users.noreply.github.com> Date: Mon, 25 Dec 2023 22:31:33 +0000 Subject: [PATCH 2/2] update ssd_person --- tools/cms_extract/create_ssd.sql | 133 ++++++-------------- tools/cms_extract/create_ssd_tmp_tables.sql | 33 +++-- 2 files changed, 53 insertions(+), 113 deletions(-) diff --git a/tools/cms_extract/create_ssd.sql b/tools/cms_extract/create_ssd.sql index 8a1d1e42..2fce018a 100644 --- a/tools/cms_extract/create_ssd.sql +++ b/tools/cms_extract/create_ssd.sql @@ -54,12 +54,12 @@ DECLARE @LastSept30th DATE; -- Most recent past September 30th date towards case Object Name: ssd_person Description: person/child details Author: D2I -Last Modified Date: 14/12/23 +Last Modified Date: 24/12/23 DB Compatibility: SQL Server 2014+|... Version: 1.4 Status: [Dev, *Testing, Release, Blocked, *AwaitingReview, Backlog] -Remarks: Need to confirm FACT_903_DATA as source of mother related data +Remarks: Removed non-required join to Child_Social.FACT_PERSON_RELATION Dependencies: - Child_Social.DIM_PERSON - Child_Social.FACT_REFERRALS @@ -77,67 +77,6 @@ PRINT 'Creating table: ' + @TableName; IF OBJECT_ID('ssd_person') IS NOT NULL DROP TABLE ssd_person; --- -- Create structure --- CREATE TABLE ssd_person ( --- pers_person_id NVARCHAR(48) PRIMARY KEY, --- pers_sex NVARCHAR(48), --- pers_ethnicity NVARCHAR(38), --- pers_dob DATETIME, --- pers_common_child_id NVARCHAR(10), --- pers_send NVARCHAR(1), --- pers_expected_dob DATETIME, -- Date or NULL --- pers_death_date DATETIME, --- pers_nationality NVARCHAR(48) --- ); - --- -- Insert data --- INSERT INTO ssd_person ( --- pers_person_id, --- pers_sex, --- pers_ethnicity, --- pers_dob, --- pers_common_child_id, --- pers_send, --- pers_expected_dob, --- pers_death_date, --- pers_nationality --- ) --- SELECT --- p.DIM_PERSON_ID, --- p.DIM_LOOKUP_VARIATION_OF_SEX_CODE, --- p.ETHNICITY_MAIN_CODE, --- p.BIRTH_DTTM, --- NULL AS pers_common_child_id, -- Set to NULL as default(dev) / or set to NHS num --- p.EHM_SEN_FLAG, --- CASE WHEN ISDATE(p.DOB_ESTIMATED) = 1 --- THEN CONVERT(DATETIME, p.DOB_ESTIMATED, 121) -- Coerce to either valid Date --- ELSE NULL END, -- or NULL --- p.DEATH_DTTM, --- p.NATNL_CODE --- FROM --- Child_Social.DIM_PERSON AS p - --- WHERE -- Filter invalid rows --- p.DIM_PERSON_ID IS NOT NULL -- Unlikely, but in case - --- AND p.DIM_PERSON_ID >= 1 -- Erronous rows with -1 seen - --- AND ( -- Filter irrelevant rows by timeframe --- EXISTS ( --- -- contact in last x@yrs --- SELECT 1 FROM Child_Social.FACT_CONTACTS fc --- WHERE fc.DIM_PERSON_ID = p.DIM_PERSON_ID --- AND fc.CONTACT_DTTM >= DATEADD(YEAR, -@ssd_timeframe_years, GETDATE()) --- ) --- OR EXISTS ( --- -- new or ongoing/active/unclosed referral in last x@yrs --- SELECT 1 FROM Child_Social.FACT_REFERRALS fr --- WHERE fr.DIM_PERSON_ID = p.DIM_PERSON_ID --- AND fr.REFRL_START_DTTM >= DATEADD(YEAR, -@ssd_timeframe_years, GETDATE()) --- ) --- ); - - -- Create structure CREATE TABLE ssd_person ( pers_person_id NVARCHAR(48) PRIMARY KEY, @@ -184,9 +123,14 @@ SELECT 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 fpr.DIM_LOOKUP_RELTN_TYPE_CODE = 'CHI' - THEN 'Y' - ELSE NULL END, + CASE + WHEN p.GENDER_MAIN_CODE <> 'M' AND -- Assumption that if male is not mother + EXISTS (SELECT 1 FROM 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 FROM @@ -195,9 +139,7 @@ FROM LEFT JOIN Child_Social.FACT_903_DATA f903 ON p.DIM_PERSON_ID = f903.DIM_PERSON_ID -JOIN - Child_Social.FACT_PERSON_RELATION AS fpr ON fpr.DIM_PERSON_ID = p.DIM_PERSON_ID - + WHERE -- Filter invalid rows p.DIM_PERSON_ID IS NOT NULL -- Unlikely, but in case AND p.DIM_PERSON_ID >= 1 -- Erronous rows with -1 seen @@ -1315,7 +1257,7 @@ INSERT INTO ssd_cin_visits ) SELECT cn.FACT_CASENOTE_ID, -- This needs checking!! [TESTING] - cn.FACT_FORM_ID, -- This needs checking!! [TESTING] + cn.FACT_FORM_ID, -- This needs checking!! [TESTING] cn.FACT_FORM_ID, cn.EVENT_DTTM, cn.SEEN_FLAG, @@ -1756,7 +1698,7 @@ ANSWER_NO = 'WasConf' AND DIM_ASSESSMENT_TEMPLATE_ID_DESC LIKE 'REVIEW%' Object Name: ssd_cla_episodes Description: Author: D2I -Last Modified Date: 08/12/23 +Last Modified Date: 24/12/23 DB Compatibility: SQL Server 2014+|... Version: 1.4 Status: [Dev, *Testing, Release, Blocked, *AwaitingReview, Backlog] @@ -1779,7 +1721,7 @@ IF OBJECT_ID('ssd_cla_episodes') IS NOT NULL DROP TABLE ssd_cla_episodes; -- Create structure CREATE TABLE ssd_cla_episodes ( - clae_cla_episode_id NVARCHAR(48) PRIMARY KEY, + clae_cla_episode_id NVARCHAR(48), clae_person_id NVARCHAR(48), clae_cla_episode_start DATETIME, clae_cla_episode_start_reason NVARCHAR(100), @@ -1788,7 +1730,7 @@ CREATE TABLE ssd_cla_episodes ( clae_cla_episode_cease_reason NVARCHAR(255), clae_cla_team NVARCHAR(48), clae_cla_worker_id NVARCHAR(48), - clae_cla_id NVARCHAR(48), + clae_cla_id NVARCHAR(48) PRIMARY KEY, clae_referral_id NVARCHAR(48) ); @@ -1814,21 +1756,19 @@ SELECT fce.CIN_903_CODE AS clae_cla_primary_need, fce.CARE_END_DATE AS clae_cla_episode_ceased, fce.CARE_REASON_END_DESC AS clae_cla_episode_cease_reason, - fi.DIM_DEPARTMENT_ID AS clae_cla_team, - fi.DIM_WORKER_NAME AS clae_cla_worker_id, + 'PLACEHOLDER DATA' AS clae_cla_team, -- + 'PLACEHOLDER DATA' AS clae_cla_worker_id, -- [PLACEHOLDER] [TESTING] fc.FACT_CLA_ID AS clae_cla_id, fc.FACT_REFERRAL_ID AS clae_referral_id - FROM Child_Social.FACT_CARE_EPISODES AS fce - JOIN Child_Social.FACT_CLA AS fc ON fce.FACT_CARE_EPISODES_ID = fc.fact_cla_id -JOIN - Child_Social.FACT_INVOLVEMENTS AS fi ON fc.fact_referral_id = fi.fact_referral_id -WHERE fi.IS_ALLOCATED_CW_FLAG = 'Y'; - +-- Removed 24/12/23 +--JOIN + -- Child_Social.FACT_INVOLVEMENTS AS fi ON fc.fact_referral_id = fi.fact_referral_id +--WHERE fi.IS_ALLOCATED_CW_FLAG = 'Y'; -- Create index(es) @@ -2222,10 +2162,10 @@ PRINT 'Test Progress Counter: ' + CAST(@TestProgress AS NVARCHAR(10)); Object Name: ssd_cla_reviews Description: Author: D2I -Last Modified Date: +Last Modified Date: 24/12/23 DB Compatibility: SQL Server 2014+|... -Version: 1.3 -Status: [*Dev, Testing, Release, Blocked, *AwaitingReview, Backlog] +Version: 1.4 +Status: [*Dev, *Testing, Release, Blocked, AwaitingReview, Backlog] Remarks: Still needs work on review_participation: 'FACT_FORM_ANSWERS.ANSWER Link using FACT_CLA_REVIEW.FACT_CASE_PATHWAY_STEP_ID to FACT_CASE_PATHWAY_STEP Link using FACT_CASE_PATHWAY_STEP.FACT_FORMS_ID to FACT_FORM_ANSWERS.ANSWER @@ -2241,12 +2181,11 @@ SET @TableName = N'ssd_cla_reviews'; PRINT 'Creating table: ' + @TableName; - -- Check if exists & drop IF OBJECT_ID('ssd_cla_review', 'U') IS NOT NULL DROP TABLE ssd_cla_review; -- Create structure -CREATE TABLE ssd_cla_review ( +CREATE TABLE #ssd_cla_review ( clar_cla_review_id NVARCHAR(48) PRIMARY KEY, clar_cla_episode_id NVARCHAR(48), clar_cla_review_due_date DATETIME, @@ -2254,26 +2193,27 @@ CREATE TABLE ssd_cla_review ( clar_cla_review_participation NVARCHAR(100), clar_cla_review_last_iro_contact_date DATETIME ); - + -- Insert data -INSERT INTO ssd_cla_review ( - clar_cla_review_id, - clar_cla_episode_id, +INSERT INTO #ssd_cla_review ( + clar_cla_review_id, + clar_cla_episode_id, clar_cla_review_due_date, clar_cla_review_date, clar_cla_review_participation, clar_cla_review_last_iro_contact_date ) -SELECT +SELECT fcr.FACT_CLA_REVIEW_ID AS clar_cla_review_id, - 'PLACEHOLDER_EPISODE_ID' AS clar_cla_episode_id, -- [PLACEHOLDER_DATA] [TESTING] + fcla.FACT_CLA_ID AS clar_cla_episode_id, fcr.DUE_DTTM AS clar_cla_review_due_date, fcr.MEETING_DTTM AS clar_cla_review_date, 'PLACEHOLDER_DATA' AS clar_cla_review_participation, -- [PLACEHOLDER_DATA] [TESTING] - '20010101' AS clar_cla_review_last_iro_contact_date -- [PLACEHOLDER_DATA] [TESTING] in YYYYMMDD format - -FROM - Child_Social.FACT_CLA_REVIEW AS fcr; + '01/01/2001' AS clar_cla_review_last_iro_contact_date -- [PLACEHOLDER_DATA] [TESTING] +FROM + Child_Social.FACT_CLA_REVIEW AS fcr + +LEFT JOIN Child_Social.FACT_CLA fcla ON fcr.FACT_CLA_ID = fcla.FACT_CLA_ID -- Add constraint(s) ALTER TABLE ssd_cla_review ADD CONSTRAINT FK_clar_to_clae @@ -3895,6 +3835,7 @@ FROM LEFT JOIN Education.DIM_PERSON AS p ON f903.DIM_PERSON_ID = p.DIM_PERSON_ID; + -- Add constraint(s) ALTER TABLE ssd_send ADD CONSTRAINT FK_send_to_person FOREIGN KEY (send_person_id) REFERENCES ssd_person(pers_person_id); diff --git a/tools/cms_extract/create_ssd_tmp_tables.sql b/tools/cms_extract/create_ssd_tmp_tables.sql index 110fa1eb..a1f94f09 100644 --- a/tools/cms_extract/create_ssd_tmp_tables.sql +++ b/tools/cms_extract/create_ssd_tmp_tables.sql @@ -78,17 +78,18 @@ PRINT 'Creating table: ' + @TableName; IF OBJECT_ID('tempdb..#ssd_person') IS NOT NULL DROP TABLE #ssd_person; + -- Create structure CREATE TABLE #ssd_person ( pers_person_id NVARCHAR(48) PRIMARY KEY, pers_sex NVARCHAR(48), - pers_gender NVARCHAR(48), + pers_gender NVARCHAR(48), -- [PLACEHOLDER_DATA] [TESTING] pers_ethnicity NVARCHAR(38), pers_dob DATETIME, pers_common_child_id NVARCHAR(10), pers_upn_unknown NVARCHAR(10), pers_send NVARCHAR(1), - pers_expected_dob DATETIME, -- Date or NULL + pers_expected_dob DATETIME, -- Date or NULL pers_death_date DATETIME, pers_is_mother NVARCHAR(48), pers_nationality NVARCHAR(48) @@ -112,10 +113,10 @@ INSERT INTO #ssd_person ( SELECT p.DIM_PERSON_ID, p.GENDER_MAIN_CODE, - 'PLACEHOLDER DATA', -- [TESTING] [PLACEHOLDER_DATA] + 'PLACEHOLDER DATA', -- [PLACEHOLDER_DATA] [TESTING] 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 f903.NO_UPN_CODE, @@ -124,9 +125,14 @@ SELECT 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 fpr.DIM_LOOKUP_RELTN_TYPE_CODE = 'CHI' - THEN 'Y' - ELSE NULL END, + CASE + WHEN p.GENDER_MAIN_CODE <> 'M' AND -- Assumption that if male is not mother + EXISTS (SELECT 1 FROM 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 FROM @@ -135,8 +141,6 @@ FROM LEFT JOIN Child_Social.FACT_903_DATA f903 ON p.DIM_PERSON_ID = f903.DIM_PERSON_ID -JOIN - Child_Social.FACT_PERSON_RELATION AS fpr ON fpr.DIM_PERSON_ID = p.DIM_PERSON_ID WHERE -- Filter invalid rows p.DIM_PERSON_ID IS NOT NULL -- Unlikely, but in case @@ -1756,7 +1760,7 @@ PRINT 'Test Progress Counter: ' + CAST(@TestProgress AS NVARCHAR(10)); Object Name: ssd_cla_episodes Description: Author: D2I -Last Modified Date: 08/12/23 +Last Modified Date: 24/12/23 DB Compatibility: SQL Server 2014+|... Version: 1.4 Status: [Dev, *Testing, Release, Blocked, *AwaitingReview, Backlog] @@ -1814,19 +1818,14 @@ SELECT fce.CIN_903_CODE AS clae_cla_primary_need, fce.CARE_END_DATE AS clae_cla_episode_ceased, fce.CARE_REASON_END_DESC AS clae_cla_episode_cease_reason, - fi.DIM_DEPARTMENT_ID AS clae_cla_team, - fi.DIM_WORKER_NAME AS clae_cla_worker_id, + 'PLACEHOLDER DATA' AS clae_cla_team, -- [PLACEHOLDER] [TESTING] + 'PLACEHOLDER DATA' AS clae_cla_worker_id, -- [PLACEHOLDER] [TESTING] fc.FACT_CLA_ID AS clae_cla_id, fc.FACT_REFERRAL_ID AS clae_referral_id - FROM Child_Social.FACT_CARE_EPISODES AS fce JOIN Child_Social.FACT_CLA AS fc ON fce.FACT_CARE_EPISODES_ID = fc.fact_cla_id -JOIN - Child_Social.FACT_INVOLVEMENTS AS fi ON fc.fact_referral_id = fi.fact_referral_id - -WHERE fi.IS_ALLOCATED_CW_FLAG = 'Y'; -- Create index(es)