Skip to content

Commit

Permalink
minor typos
Browse files Browse the repository at this point in the history
  • Loading branch information
robjharrison committed Jul 12, 2024
1 parent 0298681 commit e484395
Showing 1 changed file with 120 additions and 118 deletions.
238 changes: 120 additions & 118 deletions cms_ssd_extract_sql/systemc/liquidlogic_sqlserver_v1.1.7_1_20240710.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5684,127 +5684,129 @@ select * from ssd_table_creation_log;



/*
=============================================================================
MOD Name: involvements history, involvements type history
Description:
Author: D2I
Version: 0.2
0.1: involvement_history_json size change from 4000 to max fix trunc err 040724 RH
Status: [DT]ataTesting
Remarks:
Dependencies:
- FACT_INVOLVEMENTS
- ssd_person
=============================================================================
*/
-- [TESTING] Create marker
SET @TableName = N' Involvement History';
PRINT 'Adding MOD: ' + @TableName;

ALTER TABLE ssd_development.ssd_person
ADD pers_involvement_history_json NVARCHAR(max), -- Adjust data type as needed
pers_involvement_type_story NVARCHAR(1000); -- Adjust data type as needed

Go -- Need to ensure the above is completed prior to onward processing
-- All variables now reset, ensure re-definition if testing below in isolation

-- CTE for involvement history incl. worker data
WITH InvolvementHistoryCTE AS (
SELECT
fi.DIM_PERSON_ID,
MAX(CASE WHEN fi.RecentInvolvement = 'CW' THEN fi.DIM_WORKER_ID END) AS CurrentWorkerID,
MAX(CASE WHEN fi.RecentInvolvement = 'CW' THEN fi.FACT_WORKER_HISTORY_DEPARTMENT_DESC END) AS AllocatedTeam,
MAX(CASE WHEN fi.RecentInvolvement = '16PLUS' THEN fi.DIM_WORKER_ID END) AS PersonalAdvisorID,

JSON_QUERY((
-- structure of the main|complete invovements history json
SELECT
ISNULL(fi2.FACT_INVOLVEMENTS_ID, '') AS INVOLVEMENT_ID,
ISNULL(fi2.DIM_LOOKUP_INVOLVEMENT_TYPE_CODE, '') AS INVOLVEMENT_TYPE_CODE,
ISNULL(fi2.START_DTTM, '') AS START_DATE,
ISNULL(fi2.END_DTTM, '') AS END_DATE,
ISNULL(fi2.DIM_WORKER_ID, '') AS WORKER_ID,
ISNULL(fi2.DIM_DEPARTMENT_ID, '') AS DEPARTMENT_ID
FROM
Child_Social.FACT_INVOLVEMENTS fi2
WHERE
fi2.DIM_PERSON_ID = fi.DIM_PERSON_ID

FOR JSON PATH, WITHOUT_ARRAY_WRAPPER
-- rem WITHOUT_ARRAY_WRAPPER if restricting FULL contact history in _json (involvement_history_json)
)) AS involvement_history
FROM (

-- commented out to enable FULL contact history in _json (involvement_history_json). Re-enable if wanting only most recent/1
SELECT *,
-- ROW_NUMBER() OVER (
-- PARTITION BY DIM_PERSON_ID, DIM_LOOKUP_INVOLVEMENT_TYPE_CODE
-- ORDER BY FACT_INVOLVEMENTS_ID DESC
-- ) AS rn,
-- only applied if the following fi.rn = 1 is uncommented

DIM_LOOKUP_INVOLVEMENT_TYPE_CODE AS RecentInvolvement
FROM Child_Social.FACT_INVOLVEMENTS
WHERE
DIM_LOOKUP_INVOLVEMENT_TYPE_CODE IN ('CW', '16PLUS')
-- AND END_DTTM IS NULL -- Switch on if certainty exists that we will always find a 'current' 'open' record for both types
AND DIM_WORKER_ID IS NOT NULL -- Suggests missing data|other non-caseworker record / cannot be associated CW or +16 CW
AND DIM_WORKER_ID <> -1 -- Suggests missing data|other non-caseworker record / cannot be associated CW or +16 CW
AND (DIM_LOOKUP_INVOLVEMENT_TYPE_CODE <> 'CW' OR (DIM_LOOKUP_INVOLVEMENT_TYPE_CODE = 'CW' AND IS_ALLOCATED_CW_FLAG = 'Y'))
-- Leaving only involvement records <with> worker data that are CW+Allocated and/or 16PLUS
) fi
-- /*
-- =============================================================================
-- MOD Name: involvements history, involvements type history
-- Description:
-- Author: D2I
-- Version: 0.2
-- 0.1: involvement_history_json size change from 4000 to max fix trunc err 040724 RH
-- Status: [DT]ataTesting
-- Remarks: The addition of these MOD columns is overhead heavy. This is <especially> noticable
-- on larger dimension versions of ssd_person (i.e. > 40k).
-- Recommend that this MOD is switched off during any test runs|peak-time extract runs
-- Dependencies:
-- - FACT_INVOLVEMENTS
-- - ssd_person
-- =============================================================================
-- */
-- -- [TESTING] Create marker
-- SET @TableName = N' Involvement History';
-- PRINT 'Adding MOD: ' + @TableName;

-- ALTER TABLE ssd_development.ssd_person
-- ADD pers_involvement_history_json NVARCHAR(max), -- Adjust data type as needed
-- pers_involvement_type_story NVARCHAR(1000); -- Adjust data type as needed

-- Go -- ensure new cols ALTER TABLE completed prior to onward processing
-- -- All variables now reset, will require redeclaring if testing below in isolation

-- -- CTE for involvement history incl. worker data
-- WITH InvolvementHistoryCTE AS (
-- SELECT
-- fi.DIM_PERSON_ID,
-- MAX(CASE WHEN fi.RecentInvolvement = 'CW' THEN fi.DIM_WORKER_ID END) AS CurrentWorkerID,
-- MAX(CASE WHEN fi.RecentInvolvement = 'CW' THEN fi.FACT_WORKER_HISTORY_DEPARTMENT_DESC END) AS AllocatedTeam,
-- MAX(CASE WHEN fi.RecentInvolvement = '16PLUS' THEN fi.DIM_WORKER_ID END) AS PersonalAdvisorID,

-- JSON_QUERY((
-- -- structure of the main|complete invovements history json
-- SELECT
-- ISNULL(fi2.FACT_INVOLVEMENTS_ID, '') AS INVOLVEMENT_ID,
-- ISNULL(fi2.DIM_LOOKUP_INVOLVEMENT_TYPE_CODE, '') AS INVOLVEMENT_TYPE_CODE,
-- ISNULL(fi2.START_DTTM, '') AS START_DATE,
-- ISNULL(fi2.END_DTTM, '') AS END_DATE,
-- ISNULL(fi2.DIM_WORKER_ID, '') AS WORKER_ID,
-- ISNULL(fi2.DIM_DEPARTMENT_ID, '') AS DEPARTMENT_ID
-- FROM
-- Child_Social.FACT_INVOLVEMENTS fi2
-- WHERE
-- fi2.DIM_PERSON_ID = fi.DIM_PERSON_ID

-- FOR JSON PATH, WITHOUT_ARRAY_WRAPPER
-- -- rem WITHOUT_ARRAY_WRAPPER if restricting FULL contact history in _json (involvement_history_json)
-- )) AS involvement_history
-- FROM (

-- -- commented out to enable FULL contact history in _json (involvement_history_json). Re-enable if wanting only most recent/1
-- SELECT *,
-- -- ROW_NUMBER() OVER (
-- -- PARTITION BY DIM_PERSON_ID, DIM_LOOKUP_INVOLVEMENT_TYPE_CODE
-- -- ORDER BY FACT_INVOLVEMENTS_ID DESC
-- -- ) AS rn,
-- -- only applied if the following fi.rn = 1 is uncommented

-- DIM_LOOKUP_INVOLVEMENT_TYPE_CODE AS RecentInvolvement
-- FROM Child_Social.FACT_INVOLVEMENTS
-- WHERE
-- DIM_LOOKUP_INVOLVEMENT_TYPE_CODE IN ('CW', '16PLUS')
-- -- AND END_DTTM IS NULL -- Switch on if certainty exists that we will always find a 'current' 'open' record for both types
-- AND DIM_WORKER_ID IS NOT NULL -- Suggests missing data|other non-caseworker record / cannot be associated CW or +16 CW
-- AND DIM_WORKER_ID <> -1 -- Suggests missing data|other non-caseworker record / cannot be associated CW or +16 CW
-- AND (DIM_LOOKUP_INVOLVEMENT_TYPE_CODE <> 'CW' OR (DIM_LOOKUP_INVOLVEMENT_TYPE_CODE = 'CW' AND IS_ALLOCATED_CW_FLAG = 'Y'))
-- -- Leaving only involvement records <with> worker data that are CW+Allocated and/or 16PLUS
-- ) fi

WHERE
-- -- Commented out to enable FULL contact history in _json (involvement_history_json). Re-enable if wanting only most recent/1
-- fi.rn = 1
-- AND
-- WHERE
-- -- -- Commented out to enable FULL contact history in _json (involvement_history_json). Re-enable if wanting only most recent/1
-- -- fi.rn = 1
-- -- AND

EXISTS ( -- Remove filter IF wishing to extract records beyond scope of SSD timeframe
SELECT 1 FROM ssd_development.ssd_person p
WHERE p.pers_person_id = fi.DIM_PERSON_ID
)
-- EXISTS ( -- Remove filter IF wishing to extract records beyond scope of SSD timeframe
-- SELECT 1 FROM ssd_development.ssd_person p
-- WHERE p.pers_person_id = fi.DIM_PERSON_ID
-- )

GROUP BY
fi.DIM_PERSON_ID
),
-- CTE for involvement type story
InvolvementTypeStoryCTE AS (
SELECT
fi.DIM_PERSON_ID,
STUFF((
-- Concat involvement type codes into string
-- cannot use STRING AGG as appears to not work (Needs v2017+)
SELECT CONCAT(',', '"', fi3.DIM_LOOKUP_INVOLVEMENT_TYPE_CODE, '"')
FROM Child_Social.FACT_INVOLVEMENTS fi3
WHERE fi3.DIM_PERSON_ID = fi.DIM_PERSON_ID

AND EXISTS ( -- Remove this filter IF wishing to extract records beyond scope of SSD timeframe
SELECT 1 FROM ssd_development.ssd_person p
WHERE p.pers_person_id = fi3.DIM_PERSON_ID
)

ORDER BY fi3.FACT_INVOLVEMENTS_ID DESC
FOR XML PATH('')
), 1, 1, '') AS InvolvementTypeStory
FROM
Child_Social.FACT_INVOLVEMENTS fi
-- GROUP BY
-- fi.DIM_PERSON_ID
-- ),
-- -- CTE for involvement type story
-- InvolvementTypeStoryCTE AS (
-- SELECT
-- fi.DIM_PERSON_ID,
-- STUFF((
-- -- Concat involvement type codes into string
-- -- cannot use STRING AGG as appears to not work (Needs v2017+)
-- SELECT CONCAT(',', '"', fi3.DIM_LOOKUP_INVOLVEMENT_TYPE_CODE, '"')
-- FROM Child_Social.FACT_INVOLVEMENTS fi3
-- WHERE fi3.DIM_PERSON_ID = fi.DIM_PERSON_ID

-- AND EXISTS ( -- Remove this filter IF wishing to extract records beyond scope of SSD timeframe
-- SELECT 1 FROM ssd_development.ssd_person p
-- WHERE p.pers_person_id = fi3.DIM_PERSON_ID
-- )

-- ORDER BY fi3.FACT_INVOLVEMENTS_ID DESC
-- FOR XML PATH('')
-- ), 1, 1, '') AS InvolvementTypeStory
-- FROM
-- Child_Social.FACT_INVOLVEMENTS fi

WHERE
EXISTS ( -- Remove this filter IF wishing to extract records beyond scope of SSD timeframe
SELECT 1 FROM ssd_development.ssd_person p
WHERE p.pers_person_id = fi.DIM_PERSON_ID
)
GROUP BY
fi.DIM_PERSON_ID
)
-- WHERE
-- EXISTS ( -- Remove this filter IF wishing to extract records beyond scope of SSD timeframe
-- SELECT 1 FROM ssd_development.ssd_person p
-- WHERE p.pers_person_id = fi.DIM_PERSON_ID
-- )
-- GROUP BY
-- fi.DIM_PERSON_ID
-- )


-- Update
UPDATE p
SET
p.pers_involvement_history_json = ih.involvement_history,
p.pers_involvement_type_story = CONCAT('[', its.InvolvementTypeStory, ']')
FROM ssd_development.ssd_person p
LEFT JOIN InvolvementHistoryCTE ih ON p.pers_person_id = ih.DIM_PERSON_ID
LEFT JOIN InvolvementTypeStoryCTE its ON p.pers_person_id = its.DIM_PERSON_ID;
-- -- Update
-- UPDATE p
-- SET
-- p.pers_involvement_history_json = ih.involvement_history,
-- p.pers_involvement_type_story = CONCAT('[', its.InvolvementTypeStory, ']')
-- FROM ssd_development.ssd_person p
-- LEFT JOIN InvolvementHistoryCTE ih ON p.pers_person_id = ih.DIM_PERSON_ID
-- LEFT JOIN InvolvementTypeStoryCTE its ON p.pers_person_id = its.DIM_PERSON_ID;

0 comments on commit e484395

Please sign in to comment.