diff --git a/liquibase-service/src/main/resources/stlt/data_validaiton/001-job_validation_config.sql b/liquibase-service/src/main/resources/stlt/data_validaiton/001-job_validation_config.sql new file mode 100644 index 000000000..44ab419ad --- /dev/null +++ b/liquibase-service/src/main/resources/stlt/data_validaiton/001-job_validation_config.sql @@ -0,0 +1,2126 @@ + +IF OBJECT_ID('dbo.job_validation_config', 'U') IS NULL +begin + create table job_validation_config ( + rdb_entity varchar(500), dataflows varchar(500), validation_query nvarchar(max), dependencies nvarchar(max) default NULL + ) +end; + +IF OBJECT_ID('dbo.job_validation_config', 'U') IS NOT NULL +begin + truncate table job_validation_config; + + insert into job_validation_config values ('D_PROVIDER', 'Provider PRE-Processing Event,Provider POST-Processing', + 'SELECT + src.person_uid as uid, src.local_id, src.update_time, src.record_status_cd + , case + when nrt.provider_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_table + , case + when nrtk.provider_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_key_table + , nb.record_uid_list as retry_list + , nb.batch_id as retry_job_batch_id + , nb.retry_count as retry_count + , nb.err_description as retry_error_desc + FROM ( + SELECT person_uid, local_id, ISNULL(p.last_chg_time,p.add_time) as update_time, p.record_status_cd + FROM nbs_odse.dbo.Person p with (nolock) + WHERE p.cd = ''PRV'' + ) src + LEFT JOIN dbo.NRT_PROVIDER nrt with (nolock) ON nrt.PROVIDER_UID = src.person_uid + LEFT JOIN dbo.NRT_PROVIDER_KEY nrtk with (nolock) ON nrtk.PROVIDER_UID = nrt.provider_uid + LEFT JOIN dbo.D_PROVIDER dp with (nolock) ON dp.PROVIDER_UID = src.person_uid + LEFT JOIN dbo.NRT_BACKFILL nb with (nolock) + ON nb.status_cd <> ''COMPLETE'' AND nb.entity = ''PROVIDER'' + AND EXISTS ( + SELECT 1 + FROM STRING_SPLIT(nb.record_uid_list, '','') s + WHERE TRY_CAST(s.value AS BIGINT) = src.person_uid + ) + WHERE dp.PROVIDER_UID IS NULL', NULL); + + + insert into job_validation_config values ('D_PATIENT', 'Patient PRE-Processing Event,Patient POST-Processing', + 'SELECT + src.person_uid as uid + , src.local_id + , src.update_time + , src.record_status_cd + , case + when nrt.patient_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_table + , case + when nrtk.patient_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_key_table + , nb.record_uid_list as retry_list + , nb.batch_id as retry_job_batch_id + , nb.retry_count as retry_count + , nb.err_description as retry_error_desc + FROM ( + SELECT person_uid, local_id, ISNULL(p.last_chg_time,p.add_time) as update_time, p.record_status_cd + FROM nbs_odse.dbo.Person p with (nolock) + WHERE p.cd = ''PAT'' + ) src + LEFT JOIN dbo.D_PATIENT dp with (nolock) ON dp.patient_uid = src.person_uid + LEFT JOIN dbo.NRT_PATIENT nrt with (nolock) ON nrt.patient_uid = src.person_uid + LEFT JOIN dbo.NRT_PATIENT_KEY nrtk with (nolock) ON nrtk.patient_uid = nrt.patient_uid + LEFT JOIN dbo.NRT_BACKFILL nb with (nolock) + ON nb.status_cd <> ''COMPLETE'' AND nb.entity = ''PATIENT'' + AND EXISTS ( + SELECT 1 + FROM STRING_SPLIT(nb.record_uid_list, '','') s + WHERE TRY_CAST(s.value AS BIGINT) = src.person_uid + ) + WHERE dp.patient_uid IS NULL', NULL); + + + + insert into job_validation_config values ('D_ORGANIZATION', 'Organization PRE-Processing Event,Organization POST-Processing', + 'SELECT + src.organization_uid as uid + , src.local_id + , src.update_time + , src.record_status_cd + , case + when nrt.organization_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_table + , case + when nrtk.organization_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_key_table + , nb.record_uid_list as retry_list + , nb.batch_id as retry_job_batch_id + , nb.retry_count as retry_count + , nb.err_description as retry_error_desc + FROM ( + SELECT organization_uid, local_id, ISNULL(o.last_chg_time,o.add_time) as update_time, o.record_status_cd + FROM nbs_odse.dbo.Organization o with (nolock) + ) src + LEFT JOIN dbo.D_ORGANIZATION do with (nolock) ON do.organization_uid = src.organization_uid + LEFT JOIN dbo.NRT_ORGANIZATION nrt with (nolock) ON nrt.organization_uid = src.organization_uid + LEFT JOIN dbo.NRT_ORGANIZATION_KEY nrtk with (nolock) ON nrtk.organization_uid = nrt.organization_uid + LEFT JOIN NRT_BACKFILL nb with (nolock) + ON nb.status_cd <> ''COMPLETE'' AND nb.entity = ''ORGANIZATION'' + AND EXISTS ( + SELECT 1 + FROM STRING_SPLIT(nb.record_uid_list, '','') s + WHERE TRY_CAST(s.value AS BIGINT) = src.organization_uid + ) + WHERE do.organization_uid IS NULL', NULL); + + + insert into job_validation_config values ('INVESTIGATION', 'Investigation PRE-Processing Event,Investigation POST-Processing', + 'SELECT + src.public_health_case_uid as uid + , src.local_id + , src.update_time + , src.record_status_cd + , case + when nrt.public_health_case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_table + , case + when nrtk.case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_key_table + , nb.record_uid_list as retry_list + , nb.batch_id as retry_job_batch_id + , nb.retry_count as retry_count + , nb.err_description as retry_error_desc + FROM ( + SELECT public_health_case_uid, + local_id, + cd, + investigation_status_cd, + ISNULL(last_chg_time, add_time) as update_time, + record_status_cd + FROM nbs_odse.dbo.Public_health_case phc with (nolock) + ) src + LEFT JOIN dbo.INVESTIGATION inv with (nolock) ON inv.case_uid = src.public_health_case_uid + LEFT JOIN dbo.NRT_INVESTIGATION nrt with (nolock) ON nrt.public_health_case_uid = src.public_health_case_uid + LEFT JOIN dbo.NRT_INVESTIGATION_KEY nrtk with (nolock) ON nrtk.case_uid = nrt.public_health_case_uid + LEFT JOIN dbo.NRT_BACKFILL nb with (nolock) + ON nb.status_cd <> ''COMPLETE'' AND nb.entity = ''INVESTIGATION'' + AND EXISTS ( + SELECT 1 + FROM STRING_SPLIT(nb.record_uid_list, '','') s + WHERE TRY_CAST(s.value AS BIGINT) = src.public_health_case_uid + ) + WHERE inv.case_uid IS NULL', NULL); + + + insert into job_validation_config values ('TREATMENT', 'Treatment PRE-Processing Event,Treatment POST-Processing', + 'SELECT + src.treatment_uid as uid + , src.local_id + , src.update_time + , src.record_status_cd + , case + when nrt.treatment_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_table + , case + when nrtk.treatment_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_key_table + , nb.record_uid_list as retry_list + , nb.batch_id as retry_job_batch_id + , nb.retry_count as retry_count + , nb.err_description as retry_error_desc + FROM ( + SELECT treatment_uid, + local_id, + record_status_cd, + ISNULL(last_chg_time, add_time) as update_time + FROM nbs_odse.dbo.Treatment t + ) src + LEFT JOIN dbo.TREATMENT dt ON dt.treatment_uid = src.treatment_uid + LEFT JOIN dbo.NRT_TREATMENT nrt ON nrt.treatment_uid = src.treatment_uid + LEFT JOIN dbo.NRT_TREATMENT_KEY nrtk ON nrtk.treatment_uid = nrt.treatment_uid + LEFT JOIN dbo.NRT_BACKFILL nb + ON nb.status_cd <> ''COMPLETE'' AND nb.entity = ''TREATMENT'' + AND EXISTS ( + SELECT 1 + FROM STRING_SPLIT(nb.record_uid_list, '','') s + WHERE TRY_CAST(s.value AS BIGINT) = src.treatment_uid + ) + WHERE dt.treatment_uid IS NULL', NULL); + + + insert into job_validation_config values ('D_CASE_MANAGEMENT', 'Case Management POST-Processing', + 'SELECT + src.public_health_case_uid as uid + , src.local_id + , src.update_time + , src.record_status_cd + , case + when nrt.public_health_case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_table + , case + when nrtk.public_health_case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_key_table + , nb.record_uid_list as retry_list + , nb.batch_id as retry_job_batch_id + , nb.retry_count as retry_count + , nb.err_description as retry_error_desc + FROM ( + SELECT cm.public_health_case_uid, + local_id, + investigation_status_cd, + ISNULL(last_chg_time, add_time) as update_time, + record_status_cd + FROM nbs_odse.dbo.case_management cm with (nolock) + INNER JOIN nbs_odse.dbo.Public_health_case phc with (nolock) on cm.public_health_case_uid = phc.public_health_case_uid + ) src + LEFT JOIN ( + SELECT case_uid + FROM dbo.D_CASE_MANAGEMENT dcm with (nolock) + INNER JOIN dbo.INVESTIGATION i with (nolock) on i.investigation_key = dcm.investigation_key + ) tgt + ON tgt.case_uid = src.public_health_case_uid + LEFT JOIN dbo.NRT_INVESTIGATION nrt with (nolock) ON nrt.public_health_case_uid = src.public_health_case_uid + LEFT JOIN dbo.NRT_CASE_MANAGEMENT_KEY nrtk with (nolock) ON nrtk.public_health_case_uid = nrt.public_health_case_uid + LEFT JOIN dbo.NRT_BACKFILL nb with (nolock) + ON nb.status_cd <> ''COMPLETE'' AND nb.entity = ''CASE_MANAGEMENT'' + AND EXISTS ( + SELECT 1 + FROM STRING_SPLIT(nb.record_uid_list, '','') s + WHERE TRY_CAST(s.value AS BIGINT) = src.public_health_case_uid + ) + WHERE tgt.case_uid IS NULL', 'INVESTIGATION'); + + + + insert into job_validation_config values ('D_VACCINATION', 'Vaccination PRE-Processing Event,D_VACCINATION Post-Processing Event', + 'SELECT + src.vaccination_uid as uid + , src.local_id + , src.update_time + , src.record_status_cd + , case + when nrt.vaccination_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_table + , case + when nrtk.vaccination_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_key_table + , nb.record_uid_list as retry_list + , nb.batch_id as retry_job_batch_id + , nb.retry_count as retry_count + , nb.err_description as retry_error_desc + FROM ( + SELECT intervention_uid AS vaccination_uid, + local_id, + record_status_cd, + ISNULL(last_chg_time, add_time) as update_time + FROM nbs_odse.dbo.Intervention i with (nolock) + ) src + LEFT JOIN dbo.D_VACCINATION dv with (nolock) ON dv.vaccination_uid = src.vaccination_uid + LEFT JOIN dbo.NRT_VACCINATION nrt with (nolock) ON nrt.vaccination_uid = src.vaccination_uid + LEFT JOIN dbo.NRT_VACCINATION_KEY nrtk with (nolock) ON nrtk.vaccination_uid = nrt.vaccination_uid + LEFT JOIN dbo.NRT_BACKFILL nb with (nolock) + ON nb.status_cd <> ''COMPLETE'' AND nb.entity = ''VACCINATION'' + AND EXISTS ( + SELECT 1 + FROM STRING_SPLIT(nb.record_uid_list, '','') s + WHERE TRY_CAST(s.value AS BIGINT) = src.vaccination_uid + ) + WHERE dv.vaccination_uid IS NULL', NULL); + + + + insert into job_validation_config values ('D_INTERVIEW', 'Interview PRE-Processing Event,D_INTERVIEW', + 'SELECT + src.interview_uid as uid + , src.local_id + , src.update_time + , src.record_status_cd + , case + when nrt.interview_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_table + , case + when nrtk.interview_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_key_table + , nb.record_uid_list as retry_list + , nb.batch_id as retry_job_batch_id + , nb.retry_count as retry_count + , nb.err_description as retry_error_desc + FROM ( + SELECT interview_uid, + local_id, + record_status_cd, + ISNULL(last_chg_time,add_time) as update_time + FROM nbs_odse.dbo.Interview i with (nolock) + ) src + LEFT JOIN ( + SELECT n.interview_uid, d.local_id + FROM dbo.NRT_INTERVIEW_KEY n with (nolock) + INNER JOIN dbo.D_INTERVIEW d with (nolock) + ON d.d_interview_key = n.d_interview_key + ) di + ON di.interview_uid = src.interview_uid + LEFT JOIN dbo.NRT_INTERVIEW nrt with (nolock) ON nrt.interview_uid = src.interview_uid + LEFT JOIN dbo.NRT_INTERVIEW_KEY nrtk with (nolock) ON nrtk.interview_uid = nrt.interview_uid + LEFT JOIN dbo.NRT_BACKFILL nb with (nolock) + ON nb.status_cd <> ''COMPLETE'' AND nb.entity = ''INTERVIEW'' + AND EXISTS ( + SELECT 1 + FROM STRING_SPLIT(nb.record_uid_list, '','') s + WHERE TRY_CAST(s.value AS BIGINT) = src.interview_uid + ) + WHERE di.LOCAL_ID IS NULL', NULL); + + + insert into job_validation_config values ('LAB_TEST', 'Observation PRE-Processing Event,D_LAB_TEST Post-Processing Event', + 'SELECT + src.observation_uid as uid + , src.local_id + , src.update_time + , src.record_status_cd + , case + when nrt.observation_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_table + , case + when nrtk.lab_test_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_key_table + , nb.record_uid_list as retry_list + , nb.batch_id as retry_job_batch_id + , nb.retry_count as retry_count + , nb.err_description as retry_error_desc + FROM ( + SELECT + observation_uid, + local_id, + record_status_cd, + ISNULL(last_chg_time, add_time) as update_time, + obs_domain_cd_st_1 + FROM nbs_odse.dbo.Observation obs with (nolock) + WHERE obs.record_status_cd <> ''LOG_DEL'' + AND obs.obs_domain_cd_st_1 IN (''Order'', ''Result'', ''R_Order'', ''R_Result'', ''I_Order'', ''I_Result'', ''Order_rslt'') + AND (obs.CTRL_CD_DISPLAY_FORM IN (''LabReport'', ''LabReportMorb'') OR obs.CTRL_CD_DISPLAY_FORM IS NULL) + ) src + LEFT JOIN dbo.LAB_TEST lt with (nolock) ON lt.LAB_TEST_UID = src.observation_uid + LEFT JOIN dbo.NRT_OBSERVATION nrt with (nolock) ON nrt.observation_uid = src.observation_uid + LEFT JOIN dbo.NRT_LAB_TEST_KEY nrtk with (nolock) ON nrtk.lab_test_uid = nrt.observation_uid + LEFT JOIN dbo.NRT_BACKFILL nb with (nolock) + ON nb.status_cd <> ''COMPLETE'' AND (nb.entity = ''OBSERVATION'' or nb.entity like ''OBS%'' ) + AND EXISTS ( + SELECT 1 + FROM STRING_SPLIT(nb.record_uid_list, '','') s + WHERE TRY_CAST(s.value AS BIGINT) = src.observation_uid + ) + WHERE lt.LAB_TEST_UID IS NULL', NULL); + + + + insert into job_validation_config values ('LAB_TEST_RESULT', 'Observation PRE-Processing Event,D_LABTEST_RESULTS Post-Processing Event', + 'SELECT + src.observation_uid as uid + , src.local_id + , src.update_time + , src.record_status_cd + , case + when nrt.observation_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_table + , case + when nrtk.lab_test_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_key_table + , nb.record_uid_list as retry_list + , nb.batch_id as retry_job_batch_id + , nb.retry_count as retry_count + , nb.err_description as retry_error_desc + FROM ( + SELECT + observation_uid, + local_id, + record_status_cd, + ISNULL(last_chg_time, add_time) as update_time, + obs_domain_cd_st_1 + FROM nbs_odse.dbo.Observation obs with (nolock) + WHERE obs.record_status_cd <> ''LOG_DEL'' + AND obs.obs_domain_cd_st_1 IN ( ''Result'', ''R_Result'', ''I_Result'', ''Order_rslt'') + AND (obs.CTRL_CD_DISPLAY_FORM IN (''LabReport'', ''LabReportMorb'') OR obs.CTRL_CD_DISPLAY_FORM IS NULL) + ) src + LEFT JOIN dbo.LAB_TEST_RESULT lt with (nolock) ON lt.LAB_TEST_UID = src.observation_uid + LEFT JOIN dbo.NRT_OBSERVATION nrt with (nolock) ON nrt.observation_uid = src.observation_uid + LEFT JOIN dbo.NRT_LAB_TEST_KEY nrtk with (nolock) ON nrtk.lab_test_uid = nrt.observation_uid + LEFT JOIN dbo.NRT_BACKFILL nb with (nolock) + ON nb.status_cd <> ''COMPLETE'' AND (nb.entity = ''OBSERVATION'' or nb.entity like ''OBS%'') + AND EXISTS ( + SELECT 1 + FROM STRING_SPLIT(nb.record_uid_list, '','') s + WHERE TRY_CAST(s.value AS BIGINT) = src.observation_uid + ) + WHERE lt.LAB_TEST_UID IS NULL', 'LAB_TEST'); + + + + insert into job_validation_config values ('F_PAGE_CASE', 'F_PAGE_CASE', + 'SELECT + src.public_health_case_uid as uid + , src.local_id + , src.update_time + , src.record_status_cd + , case + when nrt.public_health_case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_table + , case + when nrtk.case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_key_table + , nb.record_uid_list as retry_list + , nb.batch_id as retry_job_batch_id + , nb.retry_count as retry_count + , nb.err_description as retry_error_desc + FROM ( + SELECT public_health_case_uid, + local_id, + cd, + investigation_status_cd, + ISNULL(last_chg_time, add_time) as update_time, + record_status_cd + FROM nbs_odse.dbo.Public_health_case phc with (nolock) + WHERE cd NOT IN ( + SELECT distinct condition_cd FROM dbo.NRT_SRTE_CONDITION_CODE + WHERE investigation_form_cd + NOT IN ( ''bo.'',''INV_FORM_BMDGBS'',''INV_FORM_BMDGEN'',''INV_FORM_BMDNM'',''INV_FORM_BMDSP'',''INV_FORM_GEN'',''INV_FORM_HEPA'',''INV_FORM_HEPBV'',''INV_FORM_HEPCV'',''INV_FORM_HEPGEN'',''INV_FORM_MEA'',''INV_FORM_PER'',''INV_FORM_RUB'',''INV_FORM_RVCT'',''INV_FORM_VAR'') + ) + ) src + LEFT JOIN dbo.INVESTIGATION inv with (nolock) ON inv.case_uid = src.public_health_case_uid + LEFT JOIN dbo.F_PAGE_CASE fact with (nolock) ON inv.investigation_key = fact.investigation_key + LEFT JOIN dbo.NRT_INVESTIGATION nrt with (nolock) ON nrt.public_health_case_uid = src.public_health_case_uid + LEFT JOIN dbo.NRT_INVESTIGATION_KEY nrtk with (nolock) ON nrtk.d_investigation_key = fact.investigation_key + LEFT JOIN dbo.NRT_BACKFILL nb with (nolock) + ON nb.status_cd <> ''COMPLETE'' AND nb.entity = ''F_PAGE_CASE'' + AND EXISTS ( + SELECT 1 + FROM STRING_SPLIT(nb.record_uid_list, '','') s + WHERE TRY_CAST(s.value AS BIGINT) = src.public_health_case_uid + ) + WHERE fact.investigation_key IS NULL', 'INVESTIGATION'); + + + insert into job_validation_config values ('F_STD_PAGE_CASE', 'F_STD_PAGE_CASE', + 'SELECT + src.public_health_case_uid as uid + , src.local_id + , src.update_time + , src.record_status_cd + , case + when nrt.public_health_case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_table + , case + when nrtk.case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_key_table + , nb.record_uid_list as retry_list + , nb.batch_id as retry_job_batch_id + , nb.retry_count as retry_count + , nb.err_description as retry_error_desc + FROM ( + SELECT public_health_case_uid, + local_id, + cd, + investigation_status_cd, + ISNULL(last_chg_time, add_time) as update_time, + record_status_cd + FROM nbs_odse.dbo.Public_health_case phc with (nolock) + WHERE cd IN ( + SELECT distinct condition_cd FROM dbo.NRT_SRTE_CONDITION_CODE + WHERE investigation_form_cd + NOT IN ( ''bo.'',''INV_FORM_BMDGBS'',''INV_FORM_BMDGEN'',''INV_FORM_BMDNM'',''INV_FORM_BMDSP'',''INV_FORM_GEN'',''INV_FORM_HEPA'',''INV_FORM_HEPBV'',''INV_FORM_HEPCV'',''INV_FORM_HEPGEN'',''INV_FORM_MEA'',''INV_FORM_PER'',''INV_FORM_RUB'',''INV_FORM_RVCT'',''INV_FORM_VAR'') + ) + ) src + LEFT JOIN dbo.INVESTIGATION inv with (nolock) ON inv.case_uid = src.public_health_case_uid + LEFT JOIN dbo.F_STD_PAGE_CASE fact with (nolock) ON inv.investigation_key = fact.investigation_key + LEFT JOIN dbo.NRT_INVESTIGATION nrt with (nolock) ON nrt.public_health_case_uid = src.public_health_case_uid + LEFT JOIN dbo.NRT_INVESTIGATION_KEY nrtk with (nolock) ON nrtk.d_investigation_key = fact.investigation_key + LEFT JOIN dbo.NRT_BACKFILL nb with (nolock) + ON nb.status_cd <> ''COMPLETE'' AND nb.entity = ''F_STD_PAGE_CASE'' + AND EXISTS ( + SELECT 1 + FROM STRING_SPLIT(nb.record_uid_list, '','') s + WHERE TRY_CAST(s.value AS BIGINT) = src.public_health_case_uid + ) + WHERE fact.investigation_key IS NULL', 'INVESTIGATION'); + + + insert into job_validation_config values ('F_VACCINATION', 'Vaccination PRE-Processing Event,F_VACCINATION Post-Processing Event', + 'SELECT + src.vaccination_uid as uid + , src.local_id + , src.update_time + , src.record_status_cd + , case + when nrt.vaccination_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_table + , case + when nrtk.vaccination_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_key_table + , nb.record_uid_list as retry_list + , nb.batch_id as retry_job_batch_id + , nb.retry_count as retry_count + , nb.err_description as retry_error_desc + FROM ( + SELECT intervention_uid AS vaccination_uid, + local_id, + record_status_cd, + ISNULL(last_chg_time, add_time) as update_time + FROM nbs_odse.dbo.Intervention i with (nolock) + ) src + LEFT JOIN dbo.D_VACCINATION dv with (nolock) ON dv.vaccination_uid = src.vaccination_uid + LEFT JOIN dbo.F_VACCINATION fact with (nolock) ON dv.d_vaccination_key = fact.d_vaccination_key + LEFT JOIN dbo.NRT_VACCINATION nrt with (nolock) ON nrt.vaccination_uid = src.vaccination_uid + LEFT JOIN dbo.NRT_VACCINATION_KEY nrtk with (nolock) ON nrtk.vaccination_uid = nrt.vaccination_uid + LEFT JOIN dbo.NRT_BACKFILL nb with (nolock) + ON nb.status_cd <> ''COMPLETE'' AND nb.entity = ''VACCINATION'' + AND EXISTS ( + SELECT 1 + FROM STRING_SPLIT(nb.record_uid_list, '','') s + WHERE TRY_CAST(s.value AS BIGINT) = src.vaccination_uid + ) + WHERE fact.d_vaccination_key IS NULL', 'D_VACCINATION'); + + + insert into job_validation_config values ('F_INTERVIEW_CASE', 'Interview PRE-Processing Event,F_INTERVIEW_CASE', + 'SELECT + src.interview_uid as uid + , src.local_id + , src.update_time + , src.record_status_cd + , case + when nrt.interview_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_table + , case + when nrtk.interview_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_key_table + , nb.record_uid_list as retry_list + , nb.batch_id as retry_job_batch_id + , nb.retry_count as retry_count + , nb.err_description as retry_error_desc + FROM ( + SELECT interview_uid, + local_id, + record_status_cd, + ISNULL(last_chg_time,add_time) as update_time + FROM nbs_odse.dbo.Interview i with (nolock) + ) src + LEFT JOIN ( + SELECT n.interview_uid, d.local_id + FROM dbo.NRT_INTERVIEW_KEY n with (nolock) + INNER JOIN dbo.D_INTERVIEW d with (nolock) + ON d.d_interview_key = n.d_interview_key + ) di + ON di.interview_uid = src.interview_uid + LEFT JOIN dbo.NRT_INTERVIEW nrt with (nolock) ON nrt.interview_uid = src.interview_uid + LEFT JOIN dbo.NRT_INTERVIEW_KEY nrtk with (nolock) ON nrtk.interview_uid = nrt.interview_uid + LEFT JOIN dbo.F_INTERVIEW_CASE fact with (nolock) ON fact.d_interview_key = nrtk.d_interview_key + LEFT JOIN dbo.NRT_BACKFILL nb with (nolock) + ON nb.status_cd <> ''COMPLETE'' AND nb.entity = ''INTERVIEW'' + AND EXISTS ( + SELECT 1 + FROM STRING_SPLIT(nb.record_uid_list, '','') s + WHERE TRY_CAST(s.value AS BIGINT) = src.interview_uid + ) + WHERE fact.d_interview_key IS NULL', 'D_INTERVIEW'); + + + insert into job_validation_config values ('D_CONTACT_RECORD', 'Contact_Record PRE-Processing Event,D_CONTACT_RECORD Post-Processing Event', + 'SELECT + src.CT_CONTACT_UID as uid + , src.local_id + , src.update_time + , src.record_status_cd + , case + when nrt.contact_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_table + , case + when nrtk.contact_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_key_table + , nb.record_uid_list as retry_list + , nb.batch_id as retry_job_batch_id + , nb.retry_count as retry_count + , nb.err_description as retry_error_desc + FROM ( + SELECT CT_CONTACT_UID, + local_id, + record_status_cd, + ISNULL(last_chg_time,add_time) as update_time + FROM nbs_odse.dbo.CT_CONTACT with (nolock) + ) src + LEFT JOIN ( + SELECT n.contact_uid, d.local_id + FROM dbo.NRT_CONTACT_KEY n with (nolock) + INNER JOIN dbo.D_CONTACT_RECORD d with (nolock) + ON d.d_contact_record_key = n.d_contact_record_key + ) di + ON di.contact_uid = src.CT_CONTACT_UID + LEFT JOIN dbo.NRT_CONTACT nrt with (nolock) ON nrt.contact_uid = src.CT_CONTACT_UID + LEFT JOIN dbo.NRT_CONTACT_KEY nrtk with (nolock) ON nrtk.contact_uid = nrt.contact_uid + LEFT JOIN dbo.NRT_BACKFILL nb with (nolock) + ON nb.status_cd <> ''COMPLETE'' AND nb.entity = ''CONTACT'' + AND EXISTS ( + SELECT 1 + FROM STRING_SPLIT(nb.record_uid_list, '','') s + WHERE TRY_CAST(s.value AS BIGINT) = src.CT_CONTACT_UID + ) + WHERE di.contact_uid IS NULL', NULL); + + + insert into job_validation_config values ('F_CONTACT_RECORD_CASE', 'Contact_Record PRE-Processing Event,F_CONTACT_RECORD_CASE Post-Processing Event', + 'SELECT + src.CT_CONTACT_UID as uid + , src.local_id + , src.update_time + , src.record_status_cd + , case + when nrt.contact_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_table + , case + when nrtk.contact_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_key_table + , nb.record_uid_list as retry_list + , nb.batch_id as retry_job_batch_id + , nb.retry_count as retry_count + , nb.err_description as retry_error_desc + FROM ( + SELECT CT_CONTACT_UID, + local_id, + record_status_cd, + ISNULL(last_chg_time,add_time) as update_time + FROM nbs_odse.dbo.CT_CONTACT with (nolock) + ) src + LEFT JOIN ( + SELECT n.contact_uid, d.local_id + FROM dbo.NRT_CONTACT_KEY n with (nolock) + INNER JOIN dbo.D_CONTACT_RECORD d with (nolock) + ON d.d_contact_record_key = n.d_contact_record_key + ) di + ON di.contact_uid = src.CT_CONTACT_UID + LEFT JOIN dbo.NRT_CONTACT nrt with (nolock) ON nrt.contact_uid = src.CT_CONTACT_UID + LEFT JOIN dbo.NRT_CONTACT_KEY nrtk with (nolock) ON nrtk.contact_uid = nrt.contact_uid + LEFT JOIN dbo.F_CONTACT_RECORD_CASE fact with (nolock) ON fact.d_contact_record_key = nrtk.d_contact_record_key + LEFT JOIN dbo.NRT_BACKFILL nb with (nolock) + ON nb.status_cd <> ''COMPLETE'' AND nb.entity = ''CONTACT'' + AND EXISTS ( + SELECT 1 + FROM STRING_SPLIT(nb.record_uid_list, '','') s + WHERE TRY_CAST(s.value AS BIGINT) = src.CT_CONTACT_UID + ) + WHERE fact.d_contact_record_key IS NULL', 'D_CONTACT_RECORD'); + + + insert into job_validation_config values ('HEPATITIS_DATAMART', 'HEPATITIS_DATAMART', + 'SELECT + src.public_health_case_uid as uid + , src.local_id + , src.update_time + , src.record_status_cd + , case + when nrt.public_health_case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_table + , case + when nrtk.case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_key_table + , nb.record_uid_list as retry_list + , nb.batch_id as retry_job_batch_id + , nb.retry_count as retry_count + , nb.err_description as retry_error_desc + FROM ( + SELECT public_health_case_uid, + local_id, + cd, + investigation_status_cd, + ISNULL(last_chg_time, add_time) as update_time, + record_status_cd + FROM nbs_odse.dbo.Public_health_case phc with (nolock) + WHERE record_status_cd <> ''LOG_DEL'' AND cd in (SELECT condition_cd FROM dbo.nrt_datamart_metadata WHERE Datamart=''Hepatitis_Datamart'' ) + ) src + LEFT JOIN dbo.NRT_INVESTIGATION nrt with (nolock) ON nrt.public_health_case_uid = src.public_health_case_uid + LEFT JOIN dbo.NRT_INVESTIGATION_KEY nrtk with (nolock) ON nrtk.case_uid = src.public_health_case_uid + LEFT JOIN dbo.HEPATITIS_DATAMART dm with (nolock) ON dm.investigation_key = nrtk.d_investigation_key + LEFT JOIN dbo.NRT_BACKFILL nb with (nolock) + ON nb.status_cd <> ''COMPLETE'' AND nb.entity like ''%Hepatitis_Datamart%'' + AND EXISTS ( + SELECT 1 + FROM STRING_SPLIT(nb.record_uid_list, '','') s + WHERE TRY_CAST(s.value AS BIGINT) = src.public_health_case_uid + ) + WHERE dm.investigation_key IS NULL', 'INVESTIGATION'); + + insert into job_validation_config values ('STD_HIV_DATAMART', 'STD_HIV_DATAMART', + 'SELECT + src.public_health_case_uid as uid + , src.local_id + , src.update_time + , src.record_status_cd + , case + when nrt.public_health_case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_table + , case + when nrtk.case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_key_table + , nb.record_uid_list as retry_list + , nb.batch_id as retry_job_batch_id + , nb.retry_count as retry_count + , nb.err_description as retry_error_desc + FROM ( + SELECT public_health_case_uid, + local_id, + cd, + investigation_status_cd, + ISNULL(last_chg_time, add_time) as update_time, + record_status_cd + FROM nbs_odse.dbo.Public_health_case phc with (nolock) + WHERE cd in (SELECT condition_cd FROM dbo.nrt_datamart_metadata WHERE Datamart=''Std_Hiv_Datamart'' ) + ) src + LEFT JOIN dbo.NRT_INVESTIGATION nrt with (nolock) ON nrt.public_health_case_uid = src.public_health_case_uid + LEFT JOIN dbo.NRT_INVESTIGATION_KEY nrtk with (nolock) ON nrtk.case_uid = src.public_health_case_uid + LEFT JOIN dbo.STD_HIV_DATAMART dm with (nolock) ON dm.investigation_key = nrtk.d_investigation_key + LEFT JOIN dbo.NRT_BACKFILL nb with (nolock) + ON nb.status_cd <> ''COMPLETE'' AND nb.entity = ''%Std_Hiv_Datamart%'' + AND EXISTS ( + SELECT 1 + FROM STRING_SPLIT(nb.record_uid_list, '','') s + WHERE TRY_CAST(s.value AS BIGINT) = src.public_health_case_uid + ) + WHERE dm.investigation_key IS NULL', 'F_STD_PAGE_CASE,INVESTIGATION'); + + insert into job_validation_config values ('D_TB_HIV', 'D_TB_HIV POST-Processing', + 'SELECT + src.public_health_case_uid as uid + , src.local_id + , src.update_time + , src.record_status_cd + , case + when nrt.public_health_case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_table + , case + when nrtk.case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_key_table + , nb.record_uid_list as retry_list + , nb.batch_id as retry_job_batch_id + , nb.retry_count as retry_count + , nb.err_description as retry_error_desc + FROM ( + SELECT public_health_case_uid, + local_id, + cd, + investigation_status_cd, + ISNULL(last_chg_time, add_time) as update_time, + record_status_cd + FROM nbs_odse.dbo.Public_health_case phc with (nolock) + WHERE cd IN ( + SELECT distinct condition_cd FROM dbo.NRT_SRTE_CONDITION_CODE + WHERE investigation_form_cd=''INV_FORM_RVCT'' + ) + ) src + LEFT JOIN dbo.NRT_INVESTIGATION nrt with (nolock) ON nrt.public_health_case_uid = src.public_health_case_uid + LEFT JOIN dbo.NRT_INVESTIGATION_KEY nrtk with (nolock) ON nrtk.case_uid = src.public_health_case_uid + LEFT JOIN dbo.D_TB_HIV dm with (nolock) ON dm.tb_pam_uid = nrtk.case_uid + LEFT JOIN dbo.NRT_BACKFILL nb with (nolock) + ON nb.status_cd <> ''COMPLETE'' AND nb.entity = ''%d_tb_hiv%'' + AND EXISTS ( + SELECT 1 + FROM STRING_SPLIT(nb.record_uid_list, '','') s + WHERE TRY_CAST(s.value AS BIGINT) = src.public_health_case_uid + ) + WHERE dm.tb_pam_uid IS NULL', 'INVESTIGATION'); + + + insert into job_validation_config values ('D_TB_PAM', 'D_TB_PAM POST-Processing', + 'SELECT + src.public_health_case_uid as uid + , src.local_id + , src.update_time + , src.record_status_cd + , case + when nrt.public_health_case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_table + , case + when nrtk.case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_key_table + , nb.record_uid_list as retry_list + , nb.batch_id as retry_job_batch_id + , nb.retry_count as retry_count + , nb.err_description as retry_error_desc + FROM ( + SELECT public_health_case_uid, + local_id, + cd, + investigation_status_cd, + ISNULL(last_chg_time, add_time) as update_time, + record_status_cd + FROM nbs_odse.dbo.Public_health_case phc with (nolock) + WHERE cd IN ( + SELECT distinct condition_cd FROM dbo.NRT_SRTE_CONDITION_CODE + WHERE investigation_form_cd=''INV_FORM_RVCT'' + ) + ) src + LEFT JOIN dbo.NRT_INVESTIGATION nrt with (nolock) ON nrt.public_health_case_uid = src.public_health_case_uid + LEFT JOIN dbo.NRT_INVESTIGATION_KEY nrtk with (nolock) ON nrtk.case_uid = src.public_health_case_uid + LEFT JOIN dbo.D_TB_PAM dim with (nolock) ON dim.tb_pam_uid = nrtk.case_uid + LEFT JOIN dbo.NRT_BACKFILL nb with (nolock) + ON nb.status_cd <> ''COMPLETE'' AND nb.entity = ''D_TB_PAM'' + AND EXISTS ( + SELECT 1 + FROM STRING_SPLIT(nb.record_uid_list, '','') s + WHERE TRY_CAST(s.value AS BIGINT) = src.public_health_case_uid + ) + WHERE dim.tb_pam_uid IS NULL', 'INVESTIGATION'); + + + insert into job_validation_config values ('F_TB_PAM', 'F_TB_PAM POST-Processing', + 'SELECT + src.public_health_case_uid as uid + , src.local_id + , src.update_time + , src.record_status_cd + , case + when nrt.public_health_case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_table + , case + when nrtk.case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_key_table + , nb.record_uid_list as retry_list + , nb.batch_id as retry_job_batch_id + , nb.retry_count as retry_count + , nb.err_description as retry_error_desc + FROM ( + SELECT public_health_case_uid, + local_id, + cd, + investigation_status_cd, + ISNULL(last_chg_time, add_time) as update_time, + record_status_cd + FROM nbs_odse.dbo.Public_health_case phc with (nolock) + WHERE cd IN ( + SELECT distinct condition_cd FROM dbo.NRT_SRTE_CONDITION_CODE + WHERE investigation_form_cd=''INV_FORM_RVCT'' + ) + ) src + LEFT JOIN dbo.NRT_INVESTIGATION nrt with (nolock) ON nrt.public_health_case_uid = src.public_health_case_uid + LEFT JOIN dbo.NRT_INVESTIGATION_KEY nrtk with (nolock) ON nrtk.case_uid = src.public_health_case_uid + LEFT JOIN dbo.F_TB_PAM fact with (nolock) ON fact.investigation_key = nrtk.d_investigation_key + LEFT JOIN dbo.NRT_BACKFILL nb with (nolock) + ON nb.status_cd <> ''COMPLETE'' AND nb.entity = ''F_TB_PAM'' + AND EXISTS ( + SELECT 1 + FROM STRING_SPLIT(nb.record_uid_list, '','') s + WHERE TRY_CAST(s.value AS BIGINT) = src.public_health_case_uid + ) + WHERE fact.investigation_key IS NULL', 'INVESTIGATION'); + + + insert into job_validation_config values ('TB_DATAMART', 'TB_DATAMART POST-Processing', + 'SELECT + src.public_health_case_uid as uid + , src.local_id + , src.update_time + , src.record_status_cd + , case + when nrt.public_health_case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_table + , case + when nrtk.case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_key_table + , nb.record_uid_list as retry_list + , nb.batch_id as retry_job_batch_id + , nb.retry_count as retry_count + , nb.err_description as retry_error_desc + FROM ( + SELECT public_health_case_uid, + local_id, + cd, + investigation_status_cd, + ISNULL(last_chg_time, add_time) as update_time, + record_status_cd + FROM nbs_odse.dbo.Public_health_case phc with (nolock) + WHERE record_status_cd <> ''LOG_DEL'' AND cd in (SELECT condition_cd FROM dbo.nrt_datamart_metadata WHERE Datamart=''TB_Datamart'' ) + ) src + LEFT JOIN dbo.NRT_INVESTIGATION nrt with (nolock) ON nrt.public_health_case_uid = src.public_health_case_uid + LEFT JOIN dbo.NRT_INVESTIGATION_KEY nrtk with (nolock) ON nrtk.case_uid = src.public_health_case_uid + LEFT JOIN dbo.TB_DATAMART dm with (nolock) ON dm.investigation_key = nrtk.d_investigation_key + LEFT JOIN dbo.NRT_BACKFILL nb with (nolock) + ON nb.status_cd <> ''COMPLETE'' AND nb.entity like ''%TB_Datamart%'' + AND EXISTS ( + SELECT 1 + FROM STRING_SPLIT(nb.record_uid_list, '','') s + WHERE TRY_CAST(s.value AS BIGINT) = src.public_health_case_uid + ) + WHERE dm.investigation_key IS NULL', 'F_TB_PAM'); + + insert into job_validation_config values ('D_VAR_PAM', 'd_var_pam POST-Processing', + 'SELECT + src.public_health_case_uid as uid + , src.local_id + , src.update_time + , src.record_status_cd + , case + when nrt.public_health_case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_table + , case + when nrtk.case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_key_table + , nb.record_uid_list as retry_list + , nb.batch_id as retry_job_batch_id + , nb.retry_count as retry_count + , nb.err_description as retry_error_desc + FROM ( + SELECT public_health_case_uid, + local_id, + cd, + investigation_status_cd, + ISNULL(last_chg_time, add_time) as update_time, + record_status_cd + FROM nbs_odse.dbo.Public_health_case phc with (nolock) + WHERE cd IN ( + SELECT distinct condition_cd FROM dbo.NRT_SRTE_CONDITION_CODE + WHERE investigation_form_cd=''INV_FORM_VAR'' + ) + ) src + LEFT JOIN dbo.NRT_INVESTIGATION nrt with (nolock) ON nrt.public_health_case_uid = src.public_health_case_uid + LEFT JOIN dbo.NRT_INVESTIGATION_KEY nrtk with (nolock) ON nrtk.case_uid = src.public_health_case_uid + LEFT JOIN dbo.D_VAR_PAM dim with (nolock) ON dim.var_pam_uid = nrtk.case_uid + LEFT JOIN dbo.NRT_BACKFILL nb with (nolock) + ON nb.status_cd <> ''COMPLETE'' AND nb.entity = ''D_VAR_PAM'' + AND EXISTS ( + SELECT 1 + FROM STRING_SPLIT(nb.record_uid_list, '','') s + WHERE TRY_CAST(s.value AS BIGINT) = src.public_health_case_uid + ) + WHERE dim.VAR_PAM_UID IS NULL', 'INVESTIGATION'); + + insert into job_validation_config values ('F_VAR_PAM', 'F_VAR_PAM POST-Processing', + 'SELECT + src.public_health_case_uid as uid + , src.local_id + , src.update_time + , src.record_status_cd + , case + when nrt.public_health_case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_table + , case + when nrtk.case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_key_table + , nb.record_uid_list as retry_list + , nb.batch_id as retry_job_batch_id + , nb.retry_count as retry_count + , nb.err_description as retry_error_desc + FROM ( + SELECT public_health_case_uid, + local_id, + cd, + investigation_status_cd, + ISNULL(last_chg_time, add_time) as update_time, + record_status_cd + FROM nbs_odse.dbo.Public_health_case phc with (nolock) + WHERE cd IN ( + SELECT distinct condition_cd FROM dbo.NRT_SRTE_CONDITION_CODE + WHERE investigation_form_cd=''INV_FORM_VAR'' + ) + ) src + LEFT JOIN dbo.NRT_INVESTIGATION nrt with (nolock) ON nrt.public_health_case_uid = src.public_health_case_uid + LEFT JOIN dbo.NRT_INVESTIGATION_KEY nrtk with (nolock) ON nrtk.case_uid = src.public_health_case_uid + LEFT JOIN dbo.F_VAR_PAM fact with (nolock) ON fact.investigation_key = nrtk.d_investigation_key + LEFT JOIN dbo.NRT_BACKFILL nb with (nolock) + ON nb.status_cd <> ''COMPLETE'' AND nb.entity = ''F_VAR_PAM'' + AND EXISTS ( + SELECT 1 + FROM STRING_SPLIT(nb.record_uid_list, '','') s + WHERE TRY_CAST(s.value AS BIGINT) = src.public_health_case_uid + ) + WHERE fact.investigation_key IS NULL', 'D_VAR_PAM'); + + insert into job_validation_config values ('VAR_DATAMART', 'var_datamart POST-Processing', + 'SELECT + src.public_health_case_uid as uid + , src.local_id + , src.update_time + , src.record_status_cd + , case + when nrt.public_health_case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_table + , case + when nrtk.case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_key_table + , nb.record_uid_list as retry_list + , nb.batch_id as retry_job_batch_id + , nb.retry_count as retry_count + , nb.err_description as retry_error_desc + FROM ( + SELECT public_health_case_uid, + local_id, + cd, + investigation_status_cd, + ISNULL(last_chg_time, add_time) as update_time, + record_status_cd + FROM nbs_odse.dbo.Public_health_case phc with (nolock) + WHERE record_status_cd <> ''LOG_DEL'' AND cd in (SELECT condition_cd FROM dbo.nrt_datamart_metadata WHERE Datamart=''VAR_Datamart'' ) + ) src + LEFT JOIN dbo.NRT_INVESTIGATION nrt with (nolock) ON nrt.public_health_case_uid = src.public_health_case_uid + LEFT JOIN dbo.NRT_INVESTIGATION_KEY nrtk with (nolock) ON nrtk.case_uid = src.public_health_case_uid + LEFT JOIN dbo.VAR_DATAMART dm with (nolock) ON dm.investigation_key = nrtk.d_investigation_key + LEFT JOIN dbo.NRT_BACKFILL nb with (nolock) + ON nb.status_cd <> ''COMPLETE'' AND nb.entity like ''%VAR_Datamart%'' + AND EXISTS ( + SELECT 1 + FROM STRING_SPLIT(nb.record_uid_list, '','') s + WHERE TRY_CAST(s.value AS BIGINT) = src.public_health_case_uid + ) + WHERE dm.investigation_key IS NULL', 'F_VAR_PAM'); + + + insert into job_validation_config values ('CRS_CASE', 'CRS_CASE_DATAMART', + 'SELECT + src.public_health_case_uid as uid + , src.local_id + , src.update_time + , src.record_status_cd + , case + when nrt.public_health_case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_table + , case + when nrtk.case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_key_table + , nb.record_uid_list as retry_list + , nb.batch_id as retry_job_batch_id + , nb.retry_count as retry_count + , nb.err_description as retry_error_desc + FROM ( + SELECT public_health_case_uid, + local_id, + cd, + investigation_status_cd, + ISNULL(last_chg_time, add_time) as update_time, + record_status_cd + FROM nbs_odse.dbo.Public_health_case phc with (nolock) + WHERE record_status_cd <> ''LOG_DEL'' AND cd in (SELECT condition_cd FROM dbo.nrt_datamart_metadata WHERE Datamart=''CRS_Case'' ) + ) src + LEFT JOIN dbo.NRT_INVESTIGATION nrt with (nolock) ON nrt.public_health_case_uid = src.public_health_case_uid + LEFT JOIN dbo.NRT_INVESTIGATION_KEY nrtk with (nolock) ON nrtk.case_uid = src.public_health_case_uid + LEFT JOIN dbo.CRS_CASE dm with (nolock) ON dm.investigation_key = nrtk.d_investigation_key + LEFT JOIN dbo.NRT_BACKFILL nb with (nolock) + ON nb.status_cd <> ''COMPLETE'' AND nb.entity like ''%CRS_Case%'' + AND EXISTS ( + SELECT 1 + FROM STRING_SPLIT(nb.record_uid_list, '','') s + WHERE TRY_CAST(s.value AS BIGINT) = src.public_health_case_uid + ) + WHERE dm.investigation_key IS NULL', 'INVESTIGATION'); + + + insert into job_validation_config values ('RUBELLA_CASE', 'RUBELLA_CASE_DATAMART', + 'SELECT + src.public_health_case_uid as uid + , src.local_id + , src.update_time + , src.record_status_cd + , case + when nrt.public_health_case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_table + , case + when nrtk.case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_key_table + , nb.record_uid_list as retry_list + , nb.batch_id as retry_job_batch_id + , nb.retry_count as retry_count + , nb.err_description as retry_error_desc + FROM ( + SELECT public_health_case_uid, + local_id, + cd, + investigation_status_cd, + ISNULL(last_chg_time, add_time) as update_time, + record_status_cd + FROM nbs_odse.dbo.Public_health_case phc with (nolock) + WHERE record_status_cd <> ''LOG_DEL'' AND cd in (SELECT condition_cd FROM dbo.nrt_datamart_metadata WHERE Datamart=''Rubella_Case'' ) + ) src + LEFT JOIN dbo.NRT_INVESTIGATION nrt with (nolock) ON nrt.public_health_case_uid = src.public_health_case_uid + LEFT JOIN dbo.NRT_INVESTIGATION_KEY nrtk with (nolock) ON nrtk.case_uid = src.public_health_case_uid + LEFT JOIN dbo.RUBELLA_CASE dm with (nolock) ON dm.investigation_key = nrtk.d_investigation_key + LEFT JOIN dbo.NRT_BACKFILL nb with (nolock) + ON nb.status_cd <> ''COMPLETE'' AND nb.entity like ''%Rubella_Case%'' + AND EXISTS ( + SELECT 1 + FROM STRING_SPLIT(nb.record_uid_list, '','') s + WHERE TRY_CAST(s.value AS BIGINT) = src.public_health_case_uid + ) + WHERE dm.investigation_key IS NULL', 'INVESTIGATION'); + + + insert into job_validation_config values ('GENERIC_CASE', 'GENERIC_CASE_DATAMART', + 'SELECT + src.public_health_case_uid as uid + , src.local_id + , src.update_time + , src.record_status_cd + , case + when nrt.public_health_case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_table + , case + when nrtk.case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_key_table + , nb.record_uid_list as retry_list + , nb.batch_id as retry_job_batch_id + , nb.retry_count as retry_count + , nb.err_description as retry_error_desc + FROM ( + SELECT public_health_case_uid, + local_id, + cd, + investigation_status_cd, + ISNULL(last_chg_time, add_time) as update_time, + record_status_cd + FROM nbs_odse.dbo.Public_health_case phc with (nolock) + WHERE record_status_cd <> ''LOG_DEL'' AND cd in (SELECT condition_cd FROM dbo.nrt_datamart_metadata WHERE Datamart=''Generic_Case'' ) + ) src + LEFT JOIN dbo.NRT_INVESTIGATION nrt with (nolock) ON nrt.public_health_case_uid = src.public_health_case_uid + LEFT JOIN dbo.NRT_INVESTIGATION_KEY nrtk with (nolock) ON nrtk.case_uid = src.public_health_case_uid + LEFT JOIN dbo.GENERIC_CASE dm with (nolock) ON dm.investigation_key = nrtk.d_investigation_key + LEFT JOIN dbo.NRT_BACKFILL nb with (nolock) + ON nb.status_cd <> ''COMPLETE'' AND nb.entity like ''%Generic_Case%'' + AND EXISTS ( + SELECT 1 + FROM STRING_SPLIT(nb.record_uid_list, '','') s + WHERE TRY_CAST(s.value AS BIGINT) = src.public_health_case_uid + ) + WHERE dm.investigation_key IS NULL', 'INVESTIGATION'); + + insert into job_validation_config values ('MEASLES_CASE', 'MEASLES_CASE_DATAMART', + 'SELECT + src.public_health_case_uid as uid + , src.local_id + , src.update_time + , src.record_status_cd + , case + when nrt.public_health_case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_table + , case + when nrtk.case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_key_table + , nb.record_uid_list as retry_list + , nb.batch_id as retry_job_batch_id + , nb.retry_count as retry_count + , nb.err_description as retry_error_desc + FROM ( + SELECT public_health_case_uid, + local_id, + cd, + investigation_status_cd, + ISNULL(last_chg_time, add_time) as update_time, + record_status_cd + FROM nbs_odse.dbo.Public_health_case phc with (nolock) + WHERE record_status_cd <> ''LOG_DEL'' AND cd in (SELECT condition_cd FROM dbo.nrt_datamart_metadata WHERE Datamart=''Measles_Case'' ) + ) src + LEFT JOIN dbo.NRT_INVESTIGATION nrt with (nolock) ON nrt.public_health_case_uid = src.public_health_case_uid + LEFT JOIN dbo.NRT_INVESTIGATION_KEY nrtk with (nolock) ON nrtk.case_uid = src.public_health_case_uid + LEFT JOIN dbo.MEASLES_CASE dm with (nolock) ON dm.investigation_key = nrtk.d_investigation_key + LEFT JOIN dbo.NRT_BACKFILL nb with (nolock) + ON nb.status_cd <> ''COMPLETE'' AND nb.entity like ''%Measles_Case%'' + AND EXISTS ( + SELECT 1 + FROM STRING_SPLIT(nb.record_uid_list, '','') s + WHERE TRY_CAST(s.value AS BIGINT) = src.public_health_case_uid + ) + WHERE dm.investigation_key IS NULL', 'INVESTIGATION'); + + insert into job_validation_config values ('BMIRD_CASE', 'BMIRD_CASE_DATAMART', + 'SELECT + src.public_health_case_uid as uid + , src.local_id + , src.update_time + , src.record_status_cd + , case + when nrt.public_health_case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_table + , case + when nrtk.case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_key_table + , nb.record_uid_list as retry_list + , nb.batch_id as retry_job_batch_id + , nb.retry_count as retry_count + , nb.err_description as retry_error_desc + FROM ( + SELECT public_health_case_uid, + local_id, + cd, + investigation_status_cd, + ISNULL(last_chg_time, add_time) as update_time, + record_status_cd + FROM nbs_odse.dbo.Public_health_case phc with (nolock) + WHERE record_status_cd <> ''LOG_DEL'' AND cd in (SELECT condition_cd FROM dbo.nrt_datamart_metadata WHERE Datamart=''BMIRD_Case'' ) + ) src + LEFT JOIN dbo.NRT_INVESTIGATION nrt with (nolock) ON nrt.public_health_case_uid = src.public_health_case_uid + LEFT JOIN dbo.NRT_INVESTIGATION_KEY nrtk with (nolock) ON nrtk.case_uid = src.public_health_case_uid + LEFT JOIN dbo.BMIRD_CASE dm with (nolock) ON dm.investigation_key = nrtk.d_investigation_key + LEFT JOIN dbo.NRT_BACKFILL nb with (nolock) + ON nb.status_cd <> ''COMPLETE'' AND nb.entity like''%BMIRD_Case%'' + AND EXISTS ( + SELECT 1 + FROM STRING_SPLIT(nb.record_uid_list, '','') s + WHERE TRY_CAST(s.value AS BIGINT) = src.public_health_case_uid + ) + WHERE dm.investigation_key IS NULL', 'INVESTIGATION'); + + insert into job_validation_config values ('HEPATITIS_CASE', 'HEPATITIS_CASE_DATAMART', + 'SELECT + src.public_health_case_uid as uid + , src.local_id + , src.update_time + , src.record_status_cd + , case + when nrt.public_health_case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_table + , case + when nrtk.case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_key_table + , nb.record_uid_list as retry_list + , nb.batch_id as retry_job_batch_id + , nb.retry_count as retry_count + , nb.err_description as retry_error_desc + FROM ( + SELECT public_health_case_uid, + local_id, + cd, + investigation_status_cd, + ISNULL(last_chg_time, add_time) as update_time, + record_status_cd + FROM nbs_odse.dbo.Public_health_case phc with (nolock) + WHERE record_status_cd <> ''LOG_DEL'' AND cd in (SELECT condition_cd FROM dbo.nrt_datamart_metadata WHERE Datamart=''Hepatitis_Case'' ) + ) src + LEFT JOIN dbo.NRT_INVESTIGATION nrt with (nolock) ON nrt.public_health_case_uid = src.public_health_case_uid + LEFT JOIN dbo.NRT_INVESTIGATION_KEY nrtk with (nolock) ON nrtk.case_uid = src.public_health_case_uid + LEFT JOIN dbo.HEPATITIS_CASE dm with (nolock) ON dm.investigation_key = nrtk.d_investigation_key + LEFT JOIN dbo.NRT_BACKFILL nb with (nolock) + ON nb.status_cd <> ''COMPLETE'' AND nb.entity like ''%Hepatitis_Case%'' + AND EXISTS ( + SELECT 1 + FROM STRING_SPLIT(nb.record_uid_list, '','') s + WHERE TRY_CAST(s.value AS BIGINT) = src.public_health_case_uid + ) + WHERE dm.investigation_key IS NULL', 'INVESTIGATION'); + + + insert into job_validation_config values ('PERTUSSIS_CASE', 'PERTUSSIS_CASE_DATAMART', + 'SELECT + src.public_health_case_uid as uid + , src.local_id + , src.update_time + , src.record_status_cd + , case + when nrt.public_health_case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_table + , case + when nrtk.case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_key_table + , nb.record_uid_list as retry_list + , nb.batch_id as retry_job_batch_id + , nb.retry_count as retry_count + , nb.err_description as retry_error_desc + FROM ( + SELECT public_health_case_uid, + local_id, + cd, + investigation_status_cd, + ISNULL(last_chg_time, add_time) as update_time, + record_status_cd + FROM nbs_odse.dbo.Public_health_case phc with (nolock) + WHERE record_status_cd <> ''LOG_DEL'' AND cd in (SELECT condition_cd FROM dbo.nrt_datamart_metadata WHERE Datamart=''Pertussis_Case'' ) + ) src + LEFT JOIN dbo.NRT_INVESTIGATION nrt with (nolock) ON nrt.public_health_case_uid = src.public_health_case_uid + LEFT JOIN dbo.NRT_INVESTIGATION_KEY nrtk with (nolock) ON nrtk.case_uid = src.public_health_case_uid + LEFT JOIN dbo.PERTUSSIS_CASE dm with (nolock) ON dm.investigation_key = nrtk.d_investigation_key + LEFT JOIN dbo.NRT_BACKFILL nb with (nolock) + ON nb.status_cd <> ''COMPLETE'' AND nb.entity like ''%Pertussis_Case%'' + AND EXISTS ( + SELECT 1 + FROM STRING_SPLIT(nb.record_uid_list, '','') s + WHERE TRY_CAST(s.value AS BIGINT) = src.public_health_case_uid + ) + WHERE dm.investigation_key IS NULL', 'INVESTIGATION'); + + + insert into job_validation_config values ('COVID_CASE_DATAMART', 'COVID DATAMART Post-Processing Event', + 'SELECT + src.public_health_case_uid as uid + , src.local_id + , src.update_time + , src.record_status_cd + , case + when nrt.public_health_case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_table + , case + when nrtk.case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_key_table + , nb.record_uid_list as retry_list + , nb.batch_id as retry_job_batch_id + , nb.retry_count as retry_count + , nb.err_description as retry_error_desc + FROM ( + SELECT public_health_case_uid, + local_id, + cd, + investigation_status_cd, + ISNULL(last_chg_time, add_time) as update_time, + record_status_cd + FROM nbs_odse.dbo.Public_health_case phc with (nolock) + WHERE record_status_cd <> ''LOG_DEL'' AND cd in (SELECT condition_cd FROM dbo.nrt_datamart_metadata WHERE Datamart=''Covid_Case_Datamart'' ) + ) src + LEFT JOIN dbo.NRT_INVESTIGATION nrt with (nolock) ON nrt.public_health_case_uid = src.public_health_case_uid + LEFT JOIN dbo.NRT_INVESTIGATION_KEY nrtk with (nolock) ON nrtk.case_uid = src.public_health_case_uid + LEFT JOIN dbo.COVID_CASE_DATAMART dm with (nolock) ON dm.public_health_case_uid = nrtk.case_uid + LEFT JOIN dbo.NRT_BACKFILL nb with (nolock) + ON nb.status_cd <> ''COMPLETE'' AND nb.entity like ''%Covid_Case_Datamart%'' + AND EXISTS ( + SELECT 1 + FROM STRING_SPLIT(nb.record_uid_list, '','') s + WHERE TRY_CAST(s.value AS BIGINT) = src.public_health_case_uid + ) + WHERE dm.public_health_case_uid IS NULL', 'INVESTIGATION'); + + + insert into job_validation_config values ('TB_HIV_DATAMART', 'TB_HIV_DATAMART POST-Processing', + 'SELECT + src.public_health_case_uid as uid + , src.local_id + , src.update_time + , src.record_status_cd + , case + when nrt.public_health_case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_table + , case + when nrtk.case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_key_table + , nb.record_uid_list as retry_list + , nb.batch_id as retry_job_batch_id + , nb.retry_count as retry_count + , nb.err_description as retry_error_desc + FROM ( + SELECT public_health_case_uid, + local_id, + cd, + investigation_status_cd, + ISNULL(last_chg_time, add_time) as update_time, + record_status_cd + FROM nbs_odse.dbo.Public_health_case phc with (nolock) + WHERE record_status_cd <> ''LOG_DEL'' AND cd IN ( + SELECT distinct condition_cd FROM dbo.NRT_SRTE_CONDITION_CODE + WHERE investigation_form_cd=''INV_FORM_RVCT'' + ) + ) src + LEFT JOIN dbo.NRT_INVESTIGATION nrt with (nolock) ON nrt.public_health_case_uid = src.public_health_case_uid + LEFT JOIN dbo.NRT_INVESTIGATION_KEY nrtk with (nolock) ON nrtk.case_uid = src.public_health_case_uid + LEFT JOIN dbo.TB_HIV_DATAMART dm with (nolock) ON dm.investigation_key = nrtk.d_investigation_key + LEFT JOIN dbo.NRT_BACKFILL nb with (nolock) + ON nb.status_cd <> ''COMPLETE'' AND nb.entity like ''%tb_hiv_datamart%'' + AND EXISTS ( + SELECT 1 + FROM STRING_SPLIT(nb.record_uid_list, '','') s + WHERE TRY_CAST(s.value AS BIGINT) = src.public_health_case_uid + ) + WHERE dm.investigation_key IS NULL', 'INVESTIGATION'); + + insert into job_validation_config values ('NOTIFICATION', 'Notification PRE-Processing Event,Notification POST-Processing', + 'SELECT + src.notification_uid as uid + , src.local_id + , src.update_time + , src.record_status_cd + , case + when nrt.notification_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_table + , case + when nrtk.notification_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_key_table + , nb.record_uid_list as retry_list + , nb.batch_id as retry_job_batch_id + , nb.retry_count as retry_count + , nb.err_description as retry_error_desc + FROM ( + SELECT notification_uid, + local_id, + ISNULL(last_chg_time, add_time) as update_time, + record_status_cd + FROM nbs_odse.dbo.Notification n with (nolock) + ) src + LEFT JOIN dbo.NRT_INVESTIGATION_NOTIFICATION nrt with (nolock) ON nrt.notification_uid = src.notification_uid + LEFT JOIN dbo.NRT_NOTIFICATION_KEY nrtk with (nolock) ON nrtk.notification_uid = src.notification_uid + LEFT JOIN dbo.NOTIFICATION n with (nolock) ON n.notification_key = nrtk.d_notification_key + LEFT JOIN dbo.NRT_BACKFILL nb with (nolock) + ON nb.status_cd <> ''COMPLETE'' AND nb.entity = ''NOTIFICATION'' + AND EXISTS ( + SELECT 1 + FROM STRING_SPLIT(nb.record_uid_list, '','') s + WHERE TRY_CAST(s.value AS BIGINT) = src.notification_uid + ) + WHERE n.notification_key IS NULL', NULL); + + insert into job_validation_config values ('NOTIFICATION_EVENT', 'Notification PRE-Processing Event,Notification POST-Processing', + 'SELECT + src.notification_uid as uid + , src.local_id + , src.update_time + , src.record_status_cd + , case + when nrt.notification_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_table + , case + when nrtk.notification_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_key_table + , nb.record_uid_list as retry_list + , nb.batch_id as retry_job_batch_id + , nb.retry_count as retry_count + , nb.err_description as retry_error_desc + FROM ( + SELECT notification_uid, + local_id, + ISNULL(last_chg_time, add_time) as update_time, + record_status_cd + FROM nbs_odse.dbo.Notification n with (nolock) + ) src + LEFT JOIN dbo.NRT_INVESTIGATION_NOTIFICATION nrt with (nolock) ON nrt.notification_uid = src.notification_uid + LEFT JOIN dbo.NRT_NOTIFICATION_KEY nrtk with (nolock) ON nrtk.notification_uid = src.notification_uid + LEFT JOIN dbo.NOTIFICATION_EVENT n with (nolock) ON n.notification_key = nrtk.d_notification_key + LEFT JOIN dbo.NRT_BACKFILL nb with (nolock) + ON nb.status_cd <> ''COMPLETE'' AND nb.entity = ''NOTIFICATION'' + AND EXISTS ( + SELECT 1 + FROM STRING_SPLIT(nb.record_uid_list, '','') s + WHERE TRY_CAST(s.value AS BIGINT) = src.notification_uid + ) + WHERE n.notification_key IS NULL', NULL); + + + insert into job_validation_config values ('HEP100', 'HEP100_DATAMART', + 'SELECT + src.public_health_case_uid as uid + , src.local_id + , src.update_time + , src.record_status_cd + , case + when nrt.public_health_case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_table + , case + when nrtk.case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_key_table + , nb.record_uid_list as retry_list + , nb.batch_id as retry_job_batch_id + , nb.retry_count as retry_count + , nb.err_description as retry_error_desc + FROM ( + SELECT public_health_case_uid, + local_id, + cd, + investigation_status_cd, + ISNULL(last_chg_time, add_time) as update_time, + record_status_cd + FROM nbs_odse.dbo.Public_health_case phc with (nolock) + WHERE record_status_cd <> ''LOG_DEL'' AND cd in (SELECT condition_cd FROM dbo.nrt_datamart_metadata WHERE Datamart=''Hepatitis_Case'' ) + ) src + LEFT JOIN dbo.NRT_INVESTIGATION nrt with (nolock) ON nrt.public_health_case_uid = src.public_health_case_uid + LEFT JOIN dbo.NRT_INVESTIGATION_KEY nrtk with (nolock) ON nrtk.case_uid = src.public_health_case_uid + LEFT JOIN dbo.HEP100 dim with (nolock) ON dim.investigation_key = nrtk.d_investigation_key + LEFT JOIN dbo.NRT_BACKFILL nb with (nolock) + ON nb.status_cd <> ''COMPLETE'' AND nb.entity = ''HEP100'' + AND EXISTS ( + SELECT 1 + FROM STRING_SPLIT(nb.record_uid_list, '','') s + WHERE TRY_CAST(s.value AS BIGINT) = src.public_health_case_uid + ) + WHERE dim.investigation_key IS NULL', 'HEPATITIS_CASE,INVESTIGATION'); + + insert into job_validation_config values ('LDF_GENERIC', 'sp_ldf_generic_datamart_postprocessing', + 'SELECT + src.public_health_case_uid as uid + , src.local_id + , src.update_time + , src.record_status_cd + , case + when nrt.public_health_case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_table + , case + when nrtk.case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_key_table + , nb.record_uid_list as retry_list + , nb.batch_id as retry_job_batch_id + , nb.retry_count as retry_count + , nb.err_description as retry_error_desc + FROM ( + select distinct + public_health_case_uid, + local_id, + investigation_status_cd, + ISNULL(p.last_chg_time, p.add_time) as update_time, + p.record_status_cd + from + nbs_odse.dbo.STATE_DEFINED_FIELD_METADATA m + INNER JOIN nbs_odse.dbo.STATE_DEFINED_FIELD_DATA d with (nolock) + ON m.ldf_uid = d.ldf_uid + AND d.business_object_nm in (''PHC'', ''BMD'', ''HEP'', ''NIP'') + INNER JOIN nbs_odse.dbo.PUBLIC_HEALTH_CASE p with (nolock) + ON d.business_object_uid = p.public_health_case_uid + INNER JOIN dbo.LDF_DATAMART_COLUMN_REF b WITH(NOLOCK) + ON m.ldf_uid = b.ldf_uid + where + m.condition_cd in (SELECT condition_cd FROM dbo.LDF_DATAMART_TABLE_REF WHERE Datamart_Name=''LDF_GENERIC'' ) + and m.data_type IN (''ST'', ''CV'', ''LIST_ST'') + ) src + LEFT JOIN dbo.NRT_INVESTIGATION nrt with (nolock) ON nrt.public_health_case_uid = src.public_health_case_uid + LEFT JOIN dbo.NRT_INVESTIGATION_KEY nrtk with (nolock) ON nrtk.case_uid = src.public_health_case_uid + LEFT JOIN dbo.LDF_GENERIC dim with (nolock) ON dim.investigation_key = nrtk.d_investigation_key + LEFT JOIN dbo.NRT_BACKFILL nb with (nolock) + ON nb.status_cd <> ''COMPLETE'' AND nb.entity = ''LDF_GENERIC'' + AND EXISTS ( + SELECT 1 + FROM STRING_SPLIT(nb.record_uid_list, '','') s + WHERE TRY_CAST(s.value AS BIGINT) = src.public_health_case_uid + ) + WHERE dim.investigation_key IS NULL', 'GENERIC_CASE'); + + insert into job_validation_config values ('LDF_GENERIC1', 'sp_ldf_generic_datamart_postprocessing', + 'SELECT + src.public_health_case_uid as uid + , src.local_id + , src.update_time + , src.record_status_cd + , case + when nrt.public_health_case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_table + , case + when nrtk.case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_key_table + , nb.record_uid_list as retry_list + , nb.batch_id as retry_job_batch_id + , nb.retry_count as retry_count + , nb.err_description as retry_error_desc + FROM ( + select distinct + public_health_case_uid, + local_id, + investigation_status_cd, + ISNULL(p.last_chg_time, p.add_time) as update_time, + p.record_status_cd + from + nbs_odse.dbo.STATE_DEFINED_FIELD_METADATA m + INNER JOIN nbs_odse.dbo.STATE_DEFINED_FIELD_DATA d with (nolock) + ON m.ldf_uid = d.ldf_uid + AND d.business_object_nm in (''PHC'', ''BMD'', ''HEP'', ''NIP'') + INNER JOIN nbs_odse.dbo.PUBLIC_HEALTH_CASE p with (nolock) + ON d.business_object_uid = p.public_health_case_uid + INNER JOIN dbo.LDF_DATAMART_COLUMN_REF b WITH(NOLOCK) + ON m.ldf_uid = b.ldf_uid + where + m.condition_cd in (SELECT condition_cd FROM dbo.LDF_DATAMART_TABLE_REF WHERE Datamart_Name=''LDF_GENERIC1'' ) + and m.data_type IN (''ST'', ''CV'', ''LIST_ST'') + ) src + LEFT JOIN dbo.NRT_INVESTIGATION nrt with (nolock) ON nrt.public_health_case_uid = src.public_health_case_uid + LEFT JOIN dbo.NRT_INVESTIGATION_KEY nrtk with (nolock) ON nrtk.case_uid = src.public_health_case_uid + LEFT JOIN dbo.LDF_GENERIC1 dim with (nolock) ON dim.investigation_key = nrtk.d_investigation_key + LEFT JOIN dbo.NRT_BACKFILL nb with (nolock) + ON nb.status_cd <> ''COMPLETE'' AND nb.entity = ''LDF_GENERIC'' + AND EXISTS ( + SELECT 1 + FROM STRING_SPLIT(nb.record_uid_list, '','') s + WHERE TRY_CAST(s.value AS BIGINT) = src.public_health_case_uid + ) + WHERE dim.investigation_key IS NULL', 'GENERIC_CASE'); + + + insert into job_validation_config values ('LDF_GENERIC2', 'sp_ldf_generic_datamart_postprocessing', + 'SELECT + src.public_health_case_uid as uid + , src.local_id + , src.update_time + , src.record_status_cd + , case + when nrt.public_health_case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_table + , case + when nrtk.case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_key_table + , nb.record_uid_list as retry_list + , nb.batch_id as retry_job_batch_id + , nb.retry_count as retry_count + , nb.err_description as retry_error_desc + FROM ( + select distinct + public_health_case_uid, + local_id, + investigation_status_cd, + ISNULL(p.last_chg_time, p.add_time) as update_time, + p.record_status_cd + from + nbs_odse.dbo.STATE_DEFINED_FIELD_METADATA m + INNER JOIN nbs_odse.dbo.STATE_DEFINED_FIELD_DATA d with (nolock) + ON m.ldf_uid = d.ldf_uid + AND d.business_object_nm in (''PHC'', ''BMD'', ''HEP'', ''NIP'') + INNER JOIN nbs_odse.dbo.PUBLIC_HEALTH_CASE p with (nolock) + ON d.business_object_uid = p.public_health_case_uid + INNER JOIN dbo.LDF_DATAMART_COLUMN_REF b WITH(NOLOCK) + ON m.ldf_uid = b.ldf_uid + where + m.condition_cd in (SELECT condition_cd FROM dbo.LDF_DATAMART_TABLE_REF WHERE Datamart_Name=''LDF_GENERIC2'' ) + and m.data_type IN (''ST'', ''CV'', ''LIST_ST'') + ) src + LEFT JOIN dbo.NRT_INVESTIGATION nrt with (nolock) ON nrt.public_health_case_uid = src.public_health_case_uid + LEFT JOIN dbo.NRT_INVESTIGATION_KEY nrtk with (nolock) ON nrtk.case_uid = src.public_health_case_uid + LEFT JOIN dbo.LDF_GENERIC2 dim with (nolock) ON dim.investigation_key = nrtk.d_investigation_key + LEFT JOIN dbo.NRT_BACKFILL nb with (nolock) + ON nb.status_cd <> ''COMPLETE'' AND nb.entity = ''LDF_GENERIC'' + AND EXISTS ( + SELECT 1 + FROM STRING_SPLIT(nb.record_uid_list, '','') s + WHERE TRY_CAST(s.value AS BIGINT) = src.public_health_case_uid + ) + WHERE dim.investigation_key IS NULL', 'GENERIC_CASE'); + + insert into job_validation_config values ('LDF_BMIRD', 'sp_ldf_bmird_datamart_postprocessing', + 'SELECT + src.public_health_case_uid as uid + , src.local_id + , src.update_time + , src.record_status_cd + , case + when nrt.public_health_case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_table + , case + when nrtk.case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_key_table + , nb.record_uid_list as retry_list + , nb.batch_id as retry_job_batch_id + , nb.retry_count as retry_count + , nb.err_description as retry_error_desc + FROM ( + select distinct + public_health_case_uid, + local_id, + investigation_status_cd, + ISNULL(p.last_chg_time, p.add_time) as update_time, + p.record_status_cd + from + nbs_odse.dbo.STATE_DEFINED_FIELD_METADATA m + INNER JOIN nbs_odse.dbo.STATE_DEFINED_FIELD_DATA d with (nolock) + ON m.ldf_uid = d.ldf_uid + AND d.business_object_nm in (''PHC'', ''BMD'', ''HEP'', ''NIP'') + INNER JOIN nbs_odse.dbo.PUBLIC_HEALTH_CASE p with (nolock) + ON d.business_object_uid = p.public_health_case_uid + INNER JOIN dbo.LDF_DATAMART_COLUMN_REF b WITH(NOLOCK) + ON m.ldf_uid = b.ldf_uid + where + m.condition_cd in (SELECT condition_cd FROM dbo.LDF_DATAMART_TABLE_REF WHERE Datamart_Name=''LDF_BMIRD'' ) + and m.data_type IN (''ST'', ''CV'', ''LIST_ST'') + ) src + LEFT JOIN dbo.NRT_INVESTIGATION nrt with (nolock) ON nrt.public_health_case_uid = src.public_health_case_uid + LEFT JOIN dbo.NRT_INVESTIGATION_KEY nrtk with (nolock) ON nrtk.case_uid = src.public_health_case_uid + LEFT JOIN dbo.LDF_BMIRD dim with (nolock) ON dim.investigation_key = nrtk.d_investigation_key + LEFT JOIN dbo.NRT_BACKFILL nb with (nolock) + ON nb.status_cd <> ''COMPLETE'' AND nb.entity = ''LDF_BMIRD'' + AND EXISTS ( + SELECT 1 + FROM STRING_SPLIT(nb.record_uid_list, '','') s + WHERE TRY_CAST(s.value AS BIGINT) = src.public_health_case_uid + ) + WHERE dim.investigation_key IS NULL', 'BMIRD_CASE'); + + insert into job_validation_config values ('LDF_FOODBORNE', 'LDF_FOODBORNE POST-Processing', + 'SELECT + src.public_health_case_uid as uid + , src.local_id + , src.update_time + , src.record_status_cd + , case + when nrt.public_health_case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_table + , case + when nrtk.case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_key_table + , nb.record_uid_list as retry_list + , nb.batch_id as retry_job_batch_id + , nb.retry_count as retry_count + , nb.err_description as retry_error_desc + FROM ( + select distinct + public_health_case_uid, + local_id, + investigation_status_cd, + ISNULL(p.last_chg_time, p.add_time) as update_time, + p.record_status_cd + from + nbs_odse.dbo.STATE_DEFINED_FIELD_METADATA m + INNER JOIN nbs_odse.dbo.STATE_DEFINED_FIELD_DATA d with (nolock) + ON m.ldf_uid = d.ldf_uid + AND d.business_object_nm in (''PHC'', ''BMD'', ''HEP'', ''NIP'') + INNER JOIN nbs_odse.dbo.PUBLIC_HEALTH_CASE p with (nolock) + ON d.business_object_uid = p.public_health_case_uid + INNER JOIN dbo.LDF_DATAMART_COLUMN_REF b WITH(NOLOCK) + ON m.ldf_uid = b.ldf_uid + where + m.condition_cd in (SELECT condition_cd FROM dbo.LDF_DATAMART_TABLE_REF WHERE Datamart_Name=''LDF_FOODBORNE'' ) + and m.data_type IN (''ST'', ''CV'', ''LIST_ST'') + ) src + LEFT JOIN dbo.NRT_INVESTIGATION nrt with (nolock) ON nrt.public_health_case_uid = src.public_health_case_uid + LEFT JOIN dbo.NRT_INVESTIGATION_KEY nrtk with (nolock) ON nrtk.case_uid = src.public_health_case_uid + LEFT JOIN dbo.LDF_FOODBORNE dim with (nolock) ON dim.investigation_key = nrtk.d_investigation_key + LEFT JOIN dbo.NRT_BACKFILL nb with (nolock) + ON nb.status_cd <> ''COMPLETE'' AND nb.entity = ''LDF_FOODBORNE'' + AND EXISTS ( + SELECT 1 + FROM STRING_SPLIT(nb.record_uid_list, '','') s + WHERE TRY_CAST(s.value AS BIGINT) = src.public_health_case_uid + ) + WHERE dim.investigation_key IS NULL', 'GENERIC_CASE'); + + insert into job_validation_config values ('LDF_MUMPS', 'sp_ldf_mumps_datamart_postprocessing POST-Processing', + 'SELECT + src.public_health_case_uid as uid + , src.local_id + , src.update_time + , src.record_status_cd + , case + when nrt.public_health_case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_table + , case + when nrtk.case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_key_table + , nb.record_uid_list as retry_list + , nb.batch_id as retry_job_batch_id + , nb.retry_count as retry_count + , nb.err_description as retry_error_desc + FROM ( + select distinct + public_health_case_uid, + local_id, + investigation_status_cd, + ISNULL(p.last_chg_time, p.add_time) as update_time, + p.record_status_cd + from + nbs_odse.dbo.STATE_DEFINED_FIELD_METADATA m + INNER JOIN nbs_odse.dbo.STATE_DEFINED_FIELD_DATA d with (nolock) + ON m.ldf_uid = d.ldf_uid + AND d.business_object_nm in (''PHC'', ''BMD'', ''HEP'', ''NIP'') + INNER JOIN nbs_odse.dbo.PUBLIC_HEALTH_CASE p with (nolock) + ON d.business_object_uid = p.public_health_case_uid + INNER JOIN dbo.LDF_DATAMART_COLUMN_REF b WITH(NOLOCK) + ON m.ldf_uid = b.ldf_uid + where + m.condition_cd in (SELECT condition_cd FROM dbo.LDF_DATAMART_TABLE_REF WHERE Datamart_Name=''LDF_MUMPS'' ) + and m.data_type IN (''ST'', ''CV'', ''LIST_ST'') + ) src + LEFT JOIN dbo.NRT_INVESTIGATION nrt with (nolock) ON nrt.public_health_case_uid = src.public_health_case_uid + LEFT JOIN dbo.NRT_INVESTIGATION_KEY nrtk with (nolock) ON nrtk.case_uid = src.public_health_case_uid + LEFT JOIN dbo.LDF_MUMPS dim with (nolock) ON dim.investigation_key = nrtk.d_investigation_key + LEFT JOIN dbo.NRT_BACKFILL nb with (nolock) + ON nb.status_cd <> ''COMPLETE'' AND nb.entity = ''LDF_MUMPS'' + AND EXISTS ( + SELECT 1 + FROM STRING_SPLIT(nb.record_uid_list, '','') s + WHERE TRY_CAST(s.value AS BIGINT) = src.public_health_case_uid + ) + WHERE dim.investigation_key IS NULL', 'GENERIC_CASE'); + + insert into job_validation_config values ('LDF_TETANUS', 'LDF_TETANUS POST-Processing', + 'SELECT + src.public_health_case_uid as uid + , src.local_id + , src.update_time + , src.record_status_cd + , case + when nrt.public_health_case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_table + , case + when nrtk.case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_key_table + , nb.record_uid_list as retry_list + , nb.batch_id as retry_job_batch_id + , nb.retry_count as retry_count + , nb.err_description as retry_error_desc + FROM ( + select distinct + public_health_case_uid, + local_id, + investigation_status_cd, + ISNULL(p.last_chg_time, p.add_time) as update_time, + p.record_status_cd + from + nbs_odse.dbo.STATE_DEFINED_FIELD_METADATA m + INNER JOIN nbs_odse.dbo.STATE_DEFINED_FIELD_DATA d with (nolock) + ON m.ldf_uid = d.ldf_uid + AND d.business_object_nm in (''PHC'', ''BMD'', ''HEP'', ''NIP'') + INNER JOIN nbs_odse.dbo.PUBLIC_HEALTH_CASE p with (nolock) + ON d.business_object_uid = p.public_health_case_uid + INNER JOIN dbo.LDF_DATAMART_COLUMN_REF b WITH(NOLOCK) + ON m.ldf_uid = b.ldf_uid + where + m.condition_cd in (SELECT condition_cd FROM dbo.LDF_DATAMART_TABLE_REF WHERE Datamart_Name=''LDF_TETANUS'' ) + and m.data_type IN (''ST'', ''CV'', ''LIST_ST'') + ) src + LEFT JOIN dbo.NRT_INVESTIGATION nrt with (nolock) ON nrt.public_health_case_uid = src.public_health_case_uid + LEFT JOIN dbo.NRT_INVESTIGATION_KEY nrtk with (nolock) ON nrtk.case_uid = src.public_health_case_uid + LEFT JOIN dbo.LDF_TETANUS dim with (nolock) ON dim.investigation_key = nrtk.d_investigation_key + LEFT JOIN dbo.NRT_BACKFILL nb with (nolock) + ON nb.status_cd <> ''COMPLETE'' AND nb.entity = ''LDF_TETANUS'' + AND EXISTS ( + SELECT 1 + FROM STRING_SPLIT(nb.record_uid_list, '','') s + WHERE TRY_CAST(s.value AS BIGINT) = src.public_health_case_uid + ) + WHERE dim.investigation_key IS NULL', 'GENERIC_CASE'); + + + insert into job_validation_config values ('LDF_VACCINE_PREVENT_DISEASES', 'LDF_VACCINE_PREVENT_DISEASES POST-Processing', + 'SELECT + src.public_health_case_uid as uid + , src.local_id + , src.update_time + , src.record_status_cd + , case + when nrt.public_health_case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_table + , case + when nrtk.case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_key_table + , nb.record_uid_list as retry_list + , nb.batch_id as retry_job_batch_id + , nb.retry_count as retry_count + , nb.err_description as retry_error_desc + FROM ( + select distinct + public_health_case_uid, + local_id, + investigation_status_cd, + ISNULL(p.last_chg_time, p.add_time) as update_time, + p.record_status_cd + from + nbs_odse.dbo.STATE_DEFINED_FIELD_METADATA m + INNER JOIN nbs_odse.dbo.STATE_DEFINED_FIELD_DATA d with (nolock) + ON m.ldf_uid = d.ldf_uid + AND d.business_object_nm in (''PHC'', ''BMD'', ''HEP'', ''NIP'') + INNER JOIN nbs_odse.dbo.PUBLIC_HEALTH_CASE p with (nolock) + ON d.business_object_uid = p.public_health_case_uid + INNER JOIN dbo.LDF_DATAMART_COLUMN_REF b WITH(NOLOCK) + ON m.ldf_uid = b.ldf_uid + where + m.condition_cd in (SELECT condition_cd FROM dbo.LDF_DATAMART_TABLE_REF WHERE Datamart_Name=''LDF_VACCINE_PREVENT_DISEASES'' ) + and m.data_type IN (''ST'', ''CV'', ''LIST_ST'') + ) src + LEFT JOIN dbo.NRT_INVESTIGATION nrt with (nolock) ON nrt.public_health_case_uid = src.public_health_case_uid + LEFT JOIN dbo.NRT_INVESTIGATION_KEY nrtk with (nolock) ON nrtk.case_uid = src.public_health_case_uid + LEFT JOIN dbo.LDF_VACCINE_PREVENT_DISEASES dim with (nolock) ON dim.investigation_key = nrtk.d_investigation_key + LEFT JOIN dbo.NRT_BACKFILL nb with (nolock) + ON nb.status_cd <> ''COMPLETE'' AND nb.entity = ''LDF_VACCINE_PREVENT_DISEASES'' + AND EXISTS ( + SELECT 1 + FROM STRING_SPLIT(nb.record_uid_list, '','') s + WHERE TRY_CAST(s.value AS BIGINT) = src.public_health_case_uid + ) + WHERE dim.investigation_key IS NULL', 'MEASLES_CASE'); + + + insert into job_validation_config values ('LDF_HEPATITIS', 'sp_ldf_hepatitis_datamart_postprocessing POST-Processing', + 'SELECT + src.public_health_case_uid as uid + , src.local_id + , src.update_time + , src.record_status_cd + , case + when nrt.public_health_case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_table + , case + when nrtk.case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_key_table + , nb.record_uid_list as retry_list + , nb.batch_id as retry_job_batch_id + , nb.retry_count as retry_count + , nb.err_description as retry_error_desc + FROM ( + select distinct + public_health_case_uid, + local_id, + investigation_status_cd, + ISNULL(p.last_chg_time, p.add_time) as update_time, + p.record_status_cd + from + nbs_odse.dbo.STATE_DEFINED_FIELD_METADATA m + INNER JOIN nbs_odse.dbo.STATE_DEFINED_FIELD_DATA d with (nolock) + ON m.ldf_uid = d.ldf_uid + AND d.business_object_nm in (''PHC'', ''BMD'', ''HEP'', ''NIP'') + INNER JOIN nbs_odse.dbo.PUBLIC_HEALTH_CASE p with (nolock) + ON d.business_object_uid = p.public_health_case_uid + INNER JOIN dbo.LDF_DATAMART_COLUMN_REF b WITH(NOLOCK) + ON m.ldf_uid = b.ldf_uid + where + m.condition_cd in (SELECT condition_cd FROM dbo.LDF_DATAMART_TABLE_REF WHERE Datamart_Name=''LDF_HEPATITIS'' ) + and m.data_type IN (''ST'', ''CV'', ''LIST_ST'') + ) src + LEFT JOIN dbo.NRT_INVESTIGATION nrt with (nolock) ON nrt.public_health_case_uid = src.public_health_case_uid + LEFT JOIN dbo.NRT_INVESTIGATION_KEY nrtk with (nolock) ON nrtk.case_uid = src.public_health_case_uid + LEFT JOIN dbo.LDF_HEPATITIS dim with (nolock) ON dim.investigation_key = nrtk.d_investigation_key + LEFT JOIN dbo.NRT_BACKFILL nb with (nolock) + ON nb.status_cd <> ''COMPLETE'' AND nb.entity = ''LDF_HEPATITIS'' + AND EXISTS ( + SELECT 1 + FROM STRING_SPLIT(nb.record_uid_list, '','') s + WHERE TRY_CAST(s.value AS BIGINT) = src.public_health_case_uid + ) + WHERE dim.investigation_key IS NULL', 'HEPATITIS_CASE'); + + + insert into job_validation_config values ('BMIRD_STREP_PNEUMO_DATAMART', 'BMIRD_STREP_PNEUMO Post-Processing Event', + 'SELECT + src.public_health_case_uid as uid + , src.local_id + , src.update_time + , src.record_status_cd + , case + when nrt.public_health_case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_table + , case + when nrtk.case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_key_table + , nb.record_uid_list as retry_list + , nb.batch_id as retry_job_batch_id + , nb.retry_count as retry_count + , nb.err_description as retry_error_desc + FROM ( + SELECT + public_health_case_uid, + local_id, + cd, + investigation_status_cd, + ISNULL(last_chg_time, add_time) as update_time, + record_status_cd + FROM + nbs_odse.dbo.PUBLIC_HEALTH_CASE phc with (nolock) + WHERE phc.record_status_cd <> ''LOG_DEL'' + AND phc.cd in (''11723'',''11717'',''11720'') + ) src + LEFT JOIN dbo.NRT_INVESTIGATION nrt with (nolock) ON nrt.public_health_case_uid = src.public_health_case_uid + LEFT JOIN dbo.NRT_INVESTIGATION_KEY nrtk with (nolock) ON nrtk.case_uid = src.public_health_case_uid + LEFT JOIN dbo.BMIRD_STREP_PNEUMO_DATAMART dm with (nolock) ON dm.investigation_key = nrtk.d_investigation_key + LEFT JOIN dbo.NRT_BACKFILL nb with (nolock) + ON nb.status_cd <> ''COMPLETE'' AND nb.entity = ''Bmird_Strep_Pneumo_Datamart'' + AND EXISTS ( + SELECT 1 + FROM STRING_SPLIT(nb.record_uid_list, '','') s + WHERE TRY_CAST(s.value AS BIGINT) = src.public_health_case_uid + ) + WHERE dm.investigation_key IS NULL', 'BMIRD_CASE'); + + insert into job_validation_config values ('INV_SUMM_DATAMART', 'INV_SUMM_DATAMART Post-Processing Event', + 'SELECT + src.public_health_case_uid as uid + , src.local_id + , src.update_time + , src.record_status_cd + , case + when nrt.public_health_case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_table + , case + when nrtk.case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_key_table + , nb.record_uid_list as retry_list + , nb.batch_id as retry_job_batch_id + , nb.retry_count as retry_count + , nb.err_description as retry_error_desc + FROM ( + SELECT + public_health_case_uid, + local_id, + cd, + investigation_status_cd, + ISNULL(last_chg_time, add_time) as update_time, + record_status_cd + FROM + nbs_odse.dbo.PUBLIC_HEALTH_CASE with (nolock) + WHERE record_status_cd = ''ACTIVE'' AND case_type_cd = ''I'' + ) src + LEFT JOIN dbo.NRT_INVESTIGATION nrt with (nolock) ON nrt.public_health_case_uid = src.public_health_case_uid + LEFT JOIN dbo.NRT_INVESTIGATION_KEY nrtk with (nolock) ON nrtk.case_uid = src.public_health_case_uid + LEFT JOIN dbo.INV_SUMM_DATAMART dm with (nolock) ON dm.investigation_key = nrtk.d_investigation_key + LEFT JOIN dbo.NRT_BACKFILL nb with (nolock) + ON nb.status_cd <> ''COMPLETE'' AND nb.entity = ''DM^MultiId_Datamart'' + AND EXISTS ( + SELECT 1 + FROM STRING_SPLIT(nb.record_uid_list, '','') s + WHERE TRY_CAST(s.value AS BIGINT) = src.public_health_case_uid + ) + WHERE dm.investigation_key IS NULL', 'INVESTIGATION'); + + + insert into job_validation_config values ('DM_INV_', 'DYNAMIC_DATAMART POST-Processing', + 'SELECT + src.public_health_case_uid as uid + , src.local_id + , src.update_time + , src.record_status_cd + , case + when nrt.public_health_case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_table + , case + when nrtk.case_uid is null then ''FALSE'' + else ''TRUE'' + end as record_in_nrt_key_table + , nb.record_uid_list as retry_list + , nb.batch_id as retry_job_batch_id + , nb.retry_count as retry_count + , nb.err_description as retry_error_desc + FROM ( + SELECT public_health_case_uid, + local_id, + cd, + investigation_status_cd, + ISNULL(last_chg_time, add_time) as update_time, + record_status_cd + FROM nbs_odse.dbo.Public_health_case phc with (nolock) + WHERE record_status_cd <> ''LOG_DEL'' + AND cd in ( + SELECT condition_cd + FROM dbo.CONDITION c with ( nolock) + INNER JOIN dbo.V_NRT_NBS_INVESTIGATION_RDB_TABLE_METADATA inv_meta + ON c.DISEASE_GRP_CD = inv_meta.FORM_CD AND c.DISEASE_GRP_CD = '''' + ) + ) src + LEFT JOIN dbo.NRT_INVESTIGATION nrt with (nolock) ON nrt.public_health_case_uid = src.public_health_case_uid + LEFT JOIN dbo.NRT_INVESTIGATION_KEY nrtk with (nolock) ON nrtk.case_uid = src.public_health_case_uid + LEFT JOIN dbo.DM_INV_ dm with (nolock) ON dm.investigation_key = nrtk.d_investigation_key + LEFT JOIN dbo.NRT_BACKFILL nb with (nolock) + ON nb.status_cd <> ''COMPLETE'' AND nb.entity = ''DM^MultiId_Datamart'' + AND EXISTS ( + SELECT 1 + FROM STRING_SPLIT(nb.record_uid_list, '','') s + WHERE TRY_CAST(s.value AS BIGINT) = src.public_health_case_uid + ) + WHERE dm.investigation_key IS NULL', 'INV_SUMM_DATAMART'); + +END \ No newline at end of file diff --git a/liquibase-service/src/main/resources/stlt/data_validaiton/002-sp_run_validation.sql b/liquibase-service/src/main/resources/stlt/data_validaiton/002-sp_run_validation.sql new file mode 100644 index 000000000..a3dc6fe2a --- /dev/null +++ b/liquibase-service/src/main/resources/stlt/data_validaiton/002-sp_run_validation.sql @@ -0,0 +1,411 @@ +IF EXISTS (SELECT * FROM sysobjects WHERE id = object_id(N'[dbo].[sp_run_validation]') + AND OBJECTPROPERTY(id, N'IsProcedure') = 1 +) +BEGIN + DROP PROCEDURE [dbo].[sp_run_validation] +END +GO +/** + Stored Procedure: dbo.sp_run_validation + + Purpose: + Validates a given RDB table against its corresponding ODSE table, with optional inclusion of job flow log errors, + and can optionally update the ODSE table's last_chg_time for relevant records. + + Parameters: + @entity VARCHAR(100) - The RDB table or entity to validate against the ODSE table. + @check_log BIT - 1 = include job_flow_log error info, 0 = only run validation query. + @topn_errors SMALLINT - Optional; when provided, limits to TOP N latest errors; + when NULL, retrieves all errors from the last @lookback days (default 10). + @initiate_update BIT - 0 = update the ODSE table (last_chg_time) for rows corresponding to validated UIDs. + @from_date DATE - Optional; updates only rows where last_chg_time >= @from_date or if last_chg_time is NULL. Defaults to current date. + + Logic Overview: + 1. Handle for Dynamic Datamarts Dependencies: + - Expanded rows for each DM_INV_* datamart instead of just the placeholder one. + - Creates a temporary table that gets used downstream + + 2. Resolve Dependencies: + - Uses a recursive CTE to expand all dependencies for the given @entity. + - Produces an ordered list of entities to process (parents before children). + + 3. Iterate Entities: + - For each entity: + • Loads the validation query and associated dataflows. + • Executes the validation query, comparing ODSE table data against RDB, NRT, and Backfill/Retry tables. + • If @check_log = 1, joins validation results to recent job_flow_log errors filtered by dataflows, Status_Type = 'ERROR', + and either TOP N or last X days. + • Aggregates errors per UID into a JSON array (error_log_json). + + 4. Collate Results: + - Inserts results into a temporary table #results with a unified schema including error_log_json. + - Maintains #root_uids for top-level entity UIDs. + + 5. Conditional Update (if @initiate_update = 1): + - Parses the ODSE table name and primary key from the validation query. + - Updates only rows in the ODSE table where the PK matches UIDs in #results. + - Sets last_chg_time to last_chg_time+2 mins for these rows. + +*/ +CREATE PROCEDURE dbo.sp_run_validation +( + @entity VARCHAR(100), + @check_log BIT = 0, + @topn_errors SMALLINT = NULL, + @initiate_update BIT = 0, + @from_date DATE = NULL +) +AS +BEGIN +SET NOCOUNT ON; + +DECLARE @validation_query NVARCHAR(MAX); +DECLARE @sql NVARCHAR(MAX); +DECLARE @dataflows VARCHAR(500); +DECLARE @lookback INT = 10; +DECLARE @odse_table SYSNAME; + +BEGIN TRY + + ---------------------------------------------------------------------- + -- 1. Resolve dependencies (recursive CTE) + ---------------------------------------------------------------------- + + -- 1. Create the temp table with same structure + CREATE TABLE #JOB_VALIDATION_CONFIG ( + rdb_entity varchar(500), + dataflows varchar(500), + validation_query nvarchar(max), + dependencies nvarchar(max) default NULL + ); + + -- 2. Insert all rows except the placeholder DM_INV_ + INSERT INTO #JOB_VALIDATION_CONFIG (rdb_entity, dataflows, validation_query, dependencies) + SELECT rdb_entity, dataflows, validation_query, dependencies + FROM dbo.JOB_VALIDATION_CONFIG + WHERE rdb_entity <> 'DM_INV_'; + + -- 3. Insert dynamic DM_INV_* rows + INSERT INTO #JOB_VALIDATION_CONFIG (rdb_entity, dataflows, validation_query, dependencies) + SELECT + 'DM_INV_' + DATAMART_NM AS rdb_entity, + 'DYNAMIC_DATAMART POST-Processing' AS dataflows, + REPLACE( + REPLACE(jvc.validation_query, '', DATAMART_NM), + '', DISEASE_GRP_CD + ) AS validation_query, + jvc.dependencies + FROM + (select distinct DATAMART_NM, DISEASE_GRP_CD FROM + dbo.INV_SUMM_DATAMART isd WITH (nolock) + INNER JOIN + dbo.CONDITION c WITH (nolock) + ON isd.DISEASE_CD = c.CONDITION_CD + INNER JOIN + dbo.V_NRT_NBS_INVESTIGATION_RDB_TABLE_METADATA inv_meta + ON c.DISEASE_GRP_CD = inv_meta.FORM_CD + ) dminfo + CROSS JOIN (select * from dbo.JOB_VALIDATION_CONFIG where rdb_entity = 'DM_INV_')jvc + ; + + ---------------------------------------------------------------------- + -- 2. Resolve dependencies (recursive CTE) + ---------------------------------------------------------------------- + ;WITH dep_cte AS ( + SELECT r.rdb_entity, r.dependencies, 0 AS level + FROM #JOB_VALIDATION_CONFIG r + WHERE r.rdb_entity = @entity + + UNION ALL + + SELECT c.rdb_entity, c.dependencies, p.level + 1 + FROM dep_cte p + JOIN #JOB_VALIDATION_CONFIG c + ON EXISTS ( + SELECT 1 FROM STRING_SPLIT(p.dependencies, ',') d + WHERE d.value = c.rdb_entity + ) + ) + SELECT rdb_entity, MAX(level) AS level + INTO #entities_to_run + FROM dep_cte + GROUP BY rdb_entity; + + select * from #entities_to_run; + + ---------------------------------------------------------------------- + -- 3. Cursor/loop through entities in dependency order + ---------------------------------------------------------------------- + DECLARE @curr_entity VARCHAR(500), @level INT; + + DECLARE entity_cursor CURSOR LOCAL FAST_FORWARD FOR + SELECT rdb_entity, level + FROM #entities_to_run + ORDER BY level; + + CREATE TABLE #results ( + rdb_entity VARCHAR(500), + uid BIGINT, + local_id VARCHAR(100), + update_time DATETIME, + record_status_cd VARCHAR(20), + record_in_nrt_table VARCHAR(5), + record_in_nrt_key_table VARCHAR(5), + retry_list NVARCHAR(MAX), + retry_job_batch_id BIGINT, + retry_count INT, + retry_error_desc NVARCHAR(MAX), + error_log_json NVARCHAR(MAX) + ); + + CREATE TABLE #root_uids (uid BIGINT PRIMARY KEY); + + + + OPEN entity_cursor; + FETCH NEXT FROM entity_cursor INTO @curr_entity, @level; + + WHILE @@FETCH_STATUS = 0 + BEGIN + -- 1. Get validation query and dataflows for the entity + SELECT + @validation_query = validation_query, + @dataflows = dataflows + FROM #JOB_VALIDATION_CONFIG + WHERE rdb_entity = @curr_entity; + + IF @validation_query IS NULL + BEGIN + RAISERROR('No validation query found for entity %s', 16, 1, @curr_entity); + CLOSE entity_cursor; + DEALLOCATE entity_cursor; + RETURN; + END + + PRINT '--- Loop Iteration ---'; + PRINT 'Current Entity: ' + ISNULL(@curr_entity, ''); + PRINT 'Level: ' + CAST(@level AS VARCHAR(10)); + --PRINT 'Validation Query: ' + ISNULL(@validation_query, ''); + PRINT 'Dataflows: ' + ISNULL(@dataflows, ''); + + + + IF @check_log = 0 + BEGIN + -- Only use validation query + SET @sql = 'select v.*, error_log_json as NULL from ('+@validation_query+') v'; + END + ELSE + BEGIN + -- 2. Build dynamic SQL that: + -- - Runs validation query + -- - Appends latest N errors flattened as JSON array + SET @sql = ' + ;WITH validation_cte AS ( + SELECT * FROM (' + @validation_query + ' ) base '+ + CASE + WHEN EXISTS (SELECT 1 FROM #root_uids) THEN ' WHERE uid IN (SELECT uid FROM #root_uids)' + ELSE '' + END + + ' + ), + error_cte AS ( + SELECT + jfl.batch_id, + jfl.Dataflow_Name, + jfl.package_Name, + jfl.Error_Description, + jfl.Step_Number, + jfl.Step_Name, + jfl.create_dttm, + vstr.uid + FROM dbo.JOB_FLOW_LOG jfl WITH (NOLOCK) + INNER JOIN STRING_SPLIT(@dataflows, '','') df ON jfl.Dataflow_Name = df.value + INNER JOIN ( + SELECT DISTINCT CAST(uid AS VARCHAR(50)) AS uid_str, uid + FROM validation_cte + ) vstr + ON jfl.Msg_Description1 LIKE ''%'' + vstr.uid_str + ''%'' + WHERE jfl.Status_Type = ''ERROR'' + ' + CASE + WHEN @topn_errors IS NULL + THEN 'AND jfl.create_dttm >= DATEADD(DAY, -@lookback, SYSDATETIME())' + ELSE '' + END + ' + ) + SELECT + ''' + @curr_entity + ''' AS rdb_entity, + v.*, + ( + SELECT ' + + CASE + WHEN @topn_errors IS NOT NULL THEN 'TOP (@topn_errors)' + ELSE '' + END + ' + e.batch_id, + e.Dataflow_Name, + e.package_Name, + e.Error_Description AS Latest_Error_Info, + e.Step_Number AS Latest_Step_Number, + e.Step_Name AS Latest_Step, + e.create_dttm AS Latest_Step_Execution_Time + FROM error_cte e + WHERE e.uid = v.uid + ORDER BY e.batch_id DESC, e.create_dttm DESC + FOR JSON PATH + ) AS error_log_json + FROM validation_cte v; + '; + END + + PRINT 'SQL statement: ' + @sql; + PRINT 'Parameter @dataflows: ' + ISNULL(@dataflows, ''); + + -- 3. Execute dynamic SQL with parameters + IF @check_log = 0 + BEGIN + INSERT INTO #results + EXEC sp_executesql + @sql, + N'@dataflows VARCHAR(500)', + @dataflows = @dataflows; + END + ELSE + BEGIN + IF @topn_errors IS NOT NULL + BEGIN + INSERT INTO #results + EXEC sp_executesql + @sql, + N'@dataflows VARCHAR(500), @topn_errors SMALLINT, @lookback INT', + @dataflows = @dataflows, + @topn_errors = @topn_errors, + @lookback = @lookback; + END + ELSE + BEGIN + INSERT INTO #results + EXEC sp_executesql + @sql, + N'@dataflows VARCHAR(500), @lookback INT', + @dataflows = @dataflows, + @lookback = @lookback; + END + END + + IF @level= 0 + BEGIN + INSERT INTO #root_uids (uid) + SELECT DISTINCT uid FROM #results; + END + + FETCH NEXT FROM entity_cursor INTO @curr_entity, @level; + END + + CLOSE entity_cursor; + DEALLOCATE entity_cursor; + + ---------------------------------------------------------------------- + -- 4. Collated result + ---------------------------------------------------------------------- + SELECT TOP 1000 * FROM #results; + + ---------------------------------------------------------------------- + -- 5. If initiate_update = 1, parse odse table name and run UPDATE + ---------------------------------------------------------------------- + + IF @initiate_update = 1 + BEGIN + SET NOCOUNT OFF; + DECLARE @RowCount_no INT; + DECLARE @odse_pk_column NVARCHAR(MAX); + + -- Default from_date to today if not provided + IF @from_date IS NULL + SET @from_date = CAST(SYSDATETIME() AS DATE); + + --special case since Case_Mgmt is not tracked under cdc and LDF data can be + --tracked via the investigation + IF @entity = 'D_CASE_MANAGEMENT' or LEFT(@entity, 4) = 'LDF_' + SET @entity = 'INVESTIGATION' + + ;WITH cte AS ( + SELECT + CHARINDEX('nbs_odse.dbo.', validation_query) AS startpos, + validation_query, + rdb_entity + FROM #JOB_VALIDATION_CONFIG + WHERE rdb_entity = @entity + ) + SELECT TOP 1 @odse_table = SUBSTRING( + validation_query, + startpos + LEN('nbs_odse.dbo.'), + CHARINDEX(' ', validation_query, startpos + LEN('nbs_odse.dbo.')) - (startpos + LEN('nbs_odse.dbo.')) + ) + FROM cte; + IF @odse_table IS NOT NULL + BEGIN + ;WITH PKCols AS ( + SELECT + c.name AS column_name, + ic.key_ordinal + FROM nbs_odse.sys.indexes i + JOIN nbs_odse.sys.index_columns ic + ON i.object_id = ic.object_id AND i.index_id = ic.index_id + JOIN nbs_odse.sys.columns c + ON c.object_id = ic.object_id AND c.column_id = ic.column_id + WHERE i.is_primary_key = 1 AND ic.key_ordinal = 1 + AND i.object_id = OBJECT_ID(N'nbs_odse.dbo.' + @odse_table) + ) + SELECT @odse_pk_column = + STRING_AGG(QUOTENAME(column_name), ',') + FROM PKCols; + + PRINT 'ODSE table: nbs_odse.dbo.' + ISNULL(@odse_table,'(null)') + + ' | PK: ' + ISNULL(@odse_pk_column,'(none)'); + + DECLARE @update_sql NVARCHAR(MAX) = N' + UPDATE tgt + SET last_chg_time = ISNULL( + DATEADD(millisecond, 2, tgt.last_chg_time), + GETDATE() + ) + FROM nbs_odse.dbo.' + QUOTENAME(@odse_table) + ' AS tgt + INNER JOIN #results r + ON tgt.' + @odse_pk_column + ' = r.uid + WHERE tgt.last_chg_time >= @from_date + OR tgt.last_chg_time IS NULL'; + + PRINT 'SQL statement: ' + @update_sql; + PRINT 'Parameter @from_date: ' + ISNULL(cast(@from_date as varchar) , ''); + + EXEC sp_executesql @update_sql, N'@from_date DATE', @from_date=@from_date; + SELECT @RowCount_no = @@ROWCOUNT; + PRINT 'Updated '+CAST(@RowCount_no AS VARCHAR(20)) +' rows in table: nbs_odse.dbo.' + @odse_table; + END + ELSE + BEGIN + RAISERROR('Could not find nbs_odse.dbo. in validation query for entity %s', 16, 1, @entity); + RETURN; + END + + END +END TRY +BEGIN CATCH + -- Close cursor only if it exists and is open + IF CURSOR_STATUS('local', 'entity_cursor') >= -1 + BEGIN + IF CURSOR_STATUS('local', 'entity_cursor') > -1 + CLOSE entity_cursor; + + DEALLOCATE entity_cursor; + END; + DECLARE @FullErrorMessage VARCHAR(8000) = + 'Error Number: ' + CAST(ERROR_NUMBER() AS VARCHAR(10)) + CHAR(13) + CHAR(10) + -- Carriage return and line feed for new lines + 'Error Severity: ' + CAST(ERROR_SEVERITY() AS VARCHAR(10)) + CHAR(13) + CHAR(10) + + 'Error State: ' + CAST(ERROR_STATE() AS VARCHAR(10)) + CHAR(13) + CHAR(10) + + 'Error Line: ' + CAST(ERROR_LINE() AS VARCHAR(10)) + CHAR(13) + CHAR(10) + + 'Error Message: ' + ERROR_MESSAGE(); + select @FullErrorMessage; +END CATCH +END; \ No newline at end of file