Skip to content

Commit 9c25f7b

Browse files
committed
cla_placements.end_reason + start_reason, referral_source coerced as interim fix for systemC users
1 parent 0116633 commit 9c25f7b

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

sql_json_query/populate_ssd_api_data_staging_2016.sql

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,15 @@ RawPayloads AS (
395395
-- str(id) for JSON
396396
CAST(cine.cine_referral_id AS varchar(36)) AS [social_care_episode_id], -- 16 [Mandatory]
397397
CONVERT(varchar(10), cine.cine_referral_date, 23) AS [referral_date], -- 17
398-
cine.cine_referral_source_code AS [referral_source], -- 18
398+
CASE
399+
-- this data point being coerced until superceded by change in source data field for systemC users
400+
WHEN cine.cine_referral_source_code IS NULL THEN NULL
401+
WHEN LTRIM(RTRIM(cine.cine_referral_source_code)) LIKE '10%' THEN '10'
402+
WHEN LTRIM(RTRIM(cine.cine_referral_source_code)) LIKE '[1-3][A-F]%' THEN LEFT(LTRIM(RTRIM(cine.cine_referral_source_code)), 2)
403+
WHEN LTRIM(RTRIM(cine.cine_referral_source_code)) LIKE '5[A-D]%' THEN LEFT(LTRIM(RTRIM(cine.cine_referral_source_code)), 2)
404+
WHEN LTRIM(RTRIM(cine.cine_referral_source_code)) LIKE '[46789]%' THEN LEFT(LTRIM(RTRIM(cine.cine_referral_source_code)), 1)
405+
ELSE NULL
406+
END AS [referral_source] -- 18
399407

400408
CONVERT(varchar(10), cine.cine_close_date, 23) AS [closure_date], -- 19
401409
cine.cine_close_reason AS [closure_reason], -- 20
@@ -544,8 +552,9 @@ RawPayloads AS (
544552
SELECT
545553
CAST(clap.clap_cla_placement_id AS varchar(36)) AS [child_looked_after_placement_id], -- 37 [Mandatory]
546554
CONVERT(varchar(10), clap.clap_cla_placement_start_date, 23) AS [start_date], -- 38
547-
MIN(clae.clae_cla_episode_start_reason) AS [start_reason], -- 39
548555

556+
-- this data point being coerced until superceded by change in source data field for systemC users
557+
MIN(LEFT(NULLIF(LTRIM(RTRIM(clae.clae_cla_episode_start_reason)), ''), 1)) AS [start_reason] -- 39
549558

550559
clap.clap_cla_placement_postcode AS [postcode], -- 40
551560
clap.clap_cla_placement_type AS [placement_type], -- 41
@@ -561,7 +570,19 @@ RawPayloads AS (
561570
23
562571
) AS [end_date], -- 42
563572

564-
MIN(clae.clae_cla_episode_ceased_reason) AS [end_reason], -- 43
573+
MIN( -- different approach needed here as needed raw data part has varied length
574+
NULLIF( -- this process to be superceded by replacement source field for systemC users
575+
REPLACE(
576+
REPLACE(
577+
REPLACE(
578+
REPLACE(LEFT(clae.clae_cla_episode_ceased_reason, 3), ' ', ''), -- remove spaces after max length truncation
579+
CHAR(9), ''), -- tabs
580+
CHAR(10), ''), -- LF
581+
CHAR(13), ''), -- CR
582+
'' -- empty string to NULL
583+
)
584+
) AS [end_reason], -- 43
585+
565586
clap.clap_cla_placement_change_reason AS [change_reason], -- 44
566587
CAST(0 AS bit) AS [purge]
567588
FROM ssd_cla_episodes clae
@@ -755,13 +776,13 @@ OUTER APPLY (
755776
ORDER BY s.id DESC
756777
) AS prev
757778

758-
-- /* Uncomment block to force hard-filter against known Stat-Returns cohort */
779+
-- /* Uncomment to force hard-filter against LA known Stat-Returns cohort table*/
759780
-- INNER JOIN
760781
-- [dbo].[StoredStatReturnsCohortIdTable] STATfilter -- FAILSAFE STAT RETURN COHORT
761782
-- ON STATfilter.[person_id] = h.person_id
762783

763-
WHERE prev.current_hash IS NULL -- first time we've seen this person
764-
OR prev.current_hash <> h.current_hash; -- payload has changed
784+
WHERE prev.current_hash IS NULL -- first time we've seen this person record
785+
OR prev.current_hash <> h.current_hash; -- or payload has changed
765786

766787

767788

0 commit comments

Comments
 (0)