diff --git a/tools-ssd_chat_plus/component2_caseload_aggregation.sql b/tools-ssd_chat_plus/component2_caseload_aggregation.sql index d933e73..59dbc3e 100644 --- a/tools-ssd_chat_plus/component2_caseload_aggregation.sql +++ b/tools-ssd_chat_plus/component2_caseload_aggregation.sql @@ -17,31 +17,31 @@ Use HDM_Local; WITH -/* involvements filter open cases with 'ALLOCATED CASE WORKER' role */ +/* involvements filter active cases with 'ALLOCATED CASE WORKER' role */ InvolvementCounts AS ( SELECT i.invo_professional_team, -- grouping by team - COUNT(DISTINCT cla.clae_person_id) AS InvolvementCaseload -- count person_id for each team with open CLA episode + COUNT(DISTINCT cla.clae_person_id) AS InvolvementCaseload -- count person_id for each team with active CLA episode FROM ssd_involvements AS i INNER JOIN - ssd_cla_episodes AS cla ON i.invo_referral_id = cla.clae_referral_id -- join to link involvements with CLA episodes + ssd_cla_episodes AS cla ON i.invo_referral_id = cla.clae_referral_id -- link involvements with CLA episodes WHERE UPPER(i.invo_professional_role_id) = 'ALLOCATED CASE WORKER' -- filter by role 'ALLOCATED CASE WORKER' - AND i.invo_involvement_end_date IS NULL -- filter for open cases in involvements + AND i.invo_involvement_end_date IS NULL -- filter for active cases in involvements GROUP BY i.invo_professional_team ), -/* get count of open CLA episodes (where clae_cla_episode_ceased is NULL) */ +/* get count of active CLA episodes (where clae_cla_episode_ceased is NULL) */ OpenCLAEpisodes AS ( SELECT i.invo_professional_team, -- grouping by team ID via involvements - COUNT(DISTINCT cla.clae_person_id) AS OpenCLAEpisodeCount -- count person_id with open CLA episodes by team + COUNT(DISTINCT cla.clae_person_id) AS OpenCLAEpisodeCount -- count person_id with active CLA episodes by team FROM ssd_cla_episodes AS cla LEFT JOIN - ssd_involvements AS i ON cla.clae_referral_id = i.invo_referral_id -- join to link with involvements for team details + ssd_involvements AS i ON cla.clae_referral_id = i.invo_referral_id -- link with involvements for team details WHERE UPPER(i.invo_professional_role_id) = 'ALLOCATED CASE WORKER' -- filter by role 'ALLOCATED CASE WORKER' AND cla.clae_cla_episode_ceased IS NULL -- ensure the CLA episode is still open @@ -49,20 +49,20 @@ OpenCLAEpisodes AS ( i.invo_professional_team ), -/* get count of open CLA episodes that also have an open CP plan */ +/* get count of active CLA episodes that also have an active CP plan */ OpenCPAndLAC AS ( SELECT i.invo_professional_team, -- grouping by team ID via involvements - COUNT(cp.cppl_cp_plan_id) AS CPAndLACCount -- count of open CLA episodes with an open CP plan by team + COUNT(cp.cppl_cp_plan_id) AS CPAndLACCount -- active CLA episodes with an active CP plan by team FROM ssd_cp_plans AS cp LEFT JOIN ssd_cla_episodes AS cla ON cp.cppl_person_id = cla.clae_person_id LEFT JOIN - ssd_involvements AS i ON cp.cppl_referral_id = i.invo_referral_id -- join to link with involvements for team details + ssd_involvements AS i ON cp.cppl_referral_id = i.invo_referral_id -- link with involvements for team details WHERE - cla.clae_cla_episode_ceased IS NULL -- filter for open CLA episodes - AND cp.cppl_cp_plan_end_date IS NULL -- filter for open CP plans + cla.clae_cla_episode_ceased IS NULL -- filter for active CLA episodes + AND cp.cppl_cp_plan_end_date IS NULL -- filter for active CP plans GROUP BY i.invo_professional_team ), @@ -70,14 +70,14 @@ OpenCPAndLAC AS ( /* get count of active CP plans with no related CLA episode */ ActiveCPWithoutCLA AS ( SELECT - i.invo_professional_team, -- grouping by team ID via involvements - COUNT(cp.cppl_cp_plan_id) AS CPnoLAC -- count of active CP plans without a related CLA episode + i.invo_professional_team, -- grouping by team via involvements + COUNT(cp.cppl_cp_plan_id) AS CPnoLAC -- active CP plans without a related CLA episode FROM ssd_cp_plans AS cp LEFT JOIN ssd_cla_episodes AS cla ON cp.cppl_referral_id = cla.clae_referral_id LEFT JOIN - ssd_involvements AS i ON cp.cppl_referral_id = i.invo_referral_id -- join to link with involvements for team details + ssd_involvements AS i ON cp.cppl_referral_id = i.invo_referral_id -- link with involvements for team dets WHERE cp.cppl_cp_plan_end_date IS NULL -- filter for active CP plans AND cla.clae_cla_episode_id IS NULL -- ensure no related CLA episode @@ -85,11 +85,11 @@ ActiveCPWithoutCLA AS ( i.invo_professional_team ), -/* count care leavers without an associated open involvement or CLA episode */ +/* count care leavers without related active involvement or CLA episode */ CareLeaversWithoutInvolvementOrCLA AS ( SELECT cl.clea_care_leaver_allocated_team, -- grouping by care leaver allocated team - COUNT(cl.clea_person_id) AS CareLeaversNoInvolvementOrCLA -- count care leavers without open involvement or CLA episode by team + COUNT(cl.clea_person_id) AS CareLeaversNoInvolvementOrCLA -- care leavers without active involvement or CLA episode by team FROM ssd_care_leavers AS cl LEFT JOIN @@ -97,22 +97,22 @@ CareLeaversWithoutInvolvementOrCLA AS ( LEFT JOIN ssd_cla_episodes AS cla ON cl.clea_person_id = cla.clae_person_id AND cla.clae_cla_episode_ceased IS NULL WHERE - i.invo_involvement_end_date IS NULL -- no open involvement - AND cla.clae_cla_episode_ceased IS NULL -- no open CLA episode + i.invo_involvement_end_date IS NULL -- no active involvement + AND cla.clae_cla_episode_ceased IS NULL -- no active CLA episode GROUP BY cl.clea_care_leaver_allocated_team ) -/* main query selecting and combining all counts */ +/* main select, combine all counts */ SELECT - ISNULL(dd.dept_team_name, 'Unassigned') AS TeamName, -- team name from department details - ISNULL(dd.dept_team_parent_name, 'No Parent') AS TeamParentName, -- parent team name from department details + ISNULL(dd.dept_team_name, 'Unassigned') AS TeamName, -- team name from department dets (if None/NULL placeholder used) + ISNULL(dd.dept_team_parent_name, 'No Parent') AS TeamParentName, -- parent team name from department dets (if None/NULL placeholder used) COALESCE(ic.InvolvementCaseload, 0) AS InvolvementCaseload, -- total involvements/caseload for each team - COALESCE(ocla.OpenCLAEpisodeCount, 0) AS OpenCLAEpisodeCount, -- open CLA episodes by team - COALESCE(cp_lac.CPAndLACCount, 0) AS LACwithCP, -- open CLA episodes with an open CP plan by team - COALESCE(acp.CPnoLAC, 0) AS LACnoCP, -- active CP plans with no related CLA episode + COALESCE(ocla.OpenCLAEpisodeCount, 0) AS OpenCLAEpisodeCount, -- active CLA episode by team + COALESCE(cp_lac.CPAndLACCount, 0) AS LACwithCP, -- active CLA episode with an active CP plan by team + COALESCE(acp.CPnoLAC, 0) AS LACnoCP, -- active CLA episode without active plan COALESCE(cl_no_involvement_or_cla.CareLeaversNoInvolvementOrCLA, 0) - AS CareLeaversNoInvolvementOrCLA -- count care leavers without open involvement or CLA episode by team + AS CareLeaversNoInvolvementOrCLA -- care leavers without active involvement or CLA episode by team FROM ssd_department AS dd LEFT JOIN @@ -125,12 +125,13 @@ LEFT JOIN ActiveCPWithoutCLA AS acp ON dd.dept_team_id = acp.invo_professional_team LEFT JOIN CareLeaversWithoutInvolvementOrCLA AS cl_no_involvement_or_cla ON dd.dept_team_id = cl_no_involvement_or_cla.clea_care_leaver_allocated_team + WHERE - COALESCE(ic.InvolvementCaseload, 0) > 0 OR -- include teams with at least one involvement case - COALESCE(ocla.OpenCLAEpisodeCount, 0) > 0 OR -- include teams with at least one open CLA episode - COALESCE(cp_lac.CPAndLACCount, 0) > 0 OR -- include teams with at least one open CLA episode with a CP plan - COALESCE(acp.CPnoLAC, 0) > 0 OR -- include teams with at least one active CP plan with no related CLA episode - COALESCE(cl_no_involvement_or_cla.CareLeaversNoInvolvementOrCLA, 0) > 0 -- include teams with care leavers having no open involvement or CLA episode + COALESCE(ic.InvolvementCaseload, 0) > 0 OR -- include teams with at least one involvement case + COALESCE(ocla.OpenCLAEpisodeCount, 0) > 0 OR -- include teams with at least one active CLA episode + COALESCE(cp_lac.CPAndLACCount, 0) > 0 OR -- include teams with at least one active CLA episode with a CP plan + COALESCE(acp.CPnoLAC, 0) > 0 OR -- include teams with at least one active CP plan with no related CLA episode + COALESCE(cl_no_involvement_or_cla.CareLeaversNoInvolvementOrCLA, 0) > 0 -- include teams with care leavers having no active involvement or CLA episode ORDER BY InvolvementCaseload DESC; @@ -141,7 +142,7 @@ ORDER BY --- start of previous version BAK -- WITH --- -- involvements filter open cases with 'ALLOCATED CASE WORKER' role +-- -- involvements filter active cases with 'ALLOCATED CASE WORKER' role -- FilteredInvolvements AS ( -- SELECT -- i.invo_involvements_id, @@ -163,25 +164,25 @@ ORDER BY -- ssd_department AS d -- ), --- -- get count of open CP plans +-- -- get count of active CP plans -- OpenCPPlans AS ( -- /* --- count number of open Child Protection (CP) plans associated with each team +-- count number of active Child Protection (CP) plans associated with each team -- filters for active CP plans linked to ongoing involvements with an 'ALLOCATED CASE WORKER' --- count is valid, open CP plans grouped by involvement team +-- count is valid, active CP plans grouped by involvement team -- */ -- SELECT -- i.invo_professional_team, -- grp by involvement team --- COUNT(*) AS OpenCPPlanCount -- count open CP plans for each team +-- COUNT(*) AS OpenCPPlanCount -- count active CP plans for each team -- FROM -- FilteredInvolvements AS i -- LEFT JOIN -- ssd_cp_plans AS cp ON i.invo_referral_id = cp.cppl_referral_id --- AND cp.cppl_cp_plan_end_date IS NULL -- only consider CP plans that are still open (no end date) +-- AND cp.cppl_cp_plan_end_date IS NULL -- only consider CP plans that are still active (no end date) -- WHERE -- cp.cppl_cp_plan_id IS NOT NULL -- exclude involvements without associated CP plan -- GROUP BY --- i.invo_professional_team -- aggregate count of open CP plans by team +-- i.invo_professional_team -- aggregate count of active CP plans by team -- ), @@ -219,31 +220,31 @@ ORDER BY -- */ -- SELECT -- cine.cine_referral_team, -- grp by referral team responsible for CiN episode --- COUNT(DISTINCT cine.cine_person_id) AS OpenCINEpisodeCount -- count distinct persons with open CIN episode +-- COUNT(DISTINCT cine.cine_person_id) AS OpenCINEpisodeCount -- count distinct persons with active CIN episode -- FROM -- ssd_cin_episodes AS cine -- WHERE -- cine.cine_close_date IS NULL -- only include episodes with no close date (still active) -- GROUP BY --- cine.cine_referral_team -- aggregate count of open CiN episodes by team +-- cine.cine_referral_team -- aggregate count of active CiN episodes by team -- ), --- -- get count of care leavers by team, only incl.those with open CIN episode +-- -- get count of care leavers by team, only incl.those with active CIN episode -- CareLeavers AS ( -- /* --- count number of care leavers by team, includes only those with open CIN episode +-- count number of care leavers by team, includes only those with active CIN episode -- filters out care leavers without an active CIN case -- count is grouped by team assigned to care leaver -- */ -- SELECT -- cl.clea_care_leaver_allocated_team, -- grp by team assigned to care leaver --- COUNT(DISTINCT cl.clea_person_id) AS CareLeaverCount -- count distinct care leavers with open CIN episode +-- COUNT(DISTINCT cl.clea_person_id) AS CareLeaverCount -- count distinct care leavers with active CIN episode -- FROM -- ssd_care_leavers AS cl -- JOIN -- ssd_cin_episodes AS cine ON cl.clea_person_id = cine.cine_person_id -- WHERE --- cine.cine_close_date IS NULL -- only include care leavers with open CIN episode +-- cine.cine_close_date IS NULL -- only include care leavers with active CIN episode -- GROUP BY -- cl.clea_care_leaver_allocated_team -- aggregate count of care leavers by team -- ) @@ -256,9 +257,9 @@ ORDER BY -- COALESCE(cn.CiNWithoutPlanCount, 0) + -- COALESCE(oc.OpenCINEpisodeCount, 0) + -- COALESCE(cl.CareLeaverCount, 0) AS TestCount, -- testCount used as [TESTING] comparison against CaseloadCount --- COALESCE(cp.OpenCPPlanCount, 0) AS OpenCPPlanCount, -- Cnt open Child Protection plans, defaults to 0 if no open plans are found +-- COALESCE(cp.OpenCPPlanCount, 0) AS OpenCPPlanCount, -- Cnt active Child Protection plans, defaults to 0 if no active plans are found -- COALESCE(cn.CiNWithoutPlanCount, 0) AS CiNWithoutPlan, -- Cnt Child in Need episodes without a plan, defaults to 0 if none are found --- COALESCE(oc.OpenCINEpisodeCount, 0) AS OpenCINEpisodeCount, -- Cnt unique open CIN episodes by team +-- COALESCE(oc.OpenCINEpisodeCount, 0) AS OpenCINEpisodeCount, -- Cnt unique active CIN episodes by team -- COALESCE(cl.CareLeaverCount, 0) AS CareLeaverCount -- Cnt care leavers by team -- FROM -- FilteredInvolvements AS fi @@ -269,7 +270,7 @@ ORDER BY -- LEFT JOIN -- CiNWithoutPlans AS cn ON fi.invo_professional_team = cn.invo_professional_team -- LEFT JOIN --- OpenCINEpisodes AS oc ON dd.dept_team_id = oc.cine_referral_team -- Join to get open CIN episodes count by team +-- OpenCINEpisodes AS oc ON dd.dept_team_id = oc.cine_referral_team -- Join to get active CIN episodes count by team -- LEFT JOIN -- CareLeavers AS cl ON dd.dept_team_id = cl.clea_care_leaver_allocated_team -- Join to get care leaver count by team @@ -282,9 +283,9 @@ ORDER BY -- cl.CareLeaverCount -- HAVING -- -- only list teams with relevant caseloads --- COALESCE(cp.OpenCPPlanCount, 0) > 0 OR -- Include teams with at least one open CP plan +-- COALESCE(cp.OpenCPPlanCount, 0) > 0 OR -- Include teams with at least one active CP plan -- COALESCE(cn.CiNWithoutPlanCount, 0) > 0 OR -- Or include teams with at least one CiN episode without a plan --- COALESCE(oc.OpenCINEpisodeCount, 0) > 0 OR -- Or include teams with at least one open CIN episode +-- COALESCE(oc.OpenCINEpisodeCount, 0) > 0 OR -- Or include teams with at least one active CIN episode -- COALESCE(cl.CareLeaverCount, 0) > 0 -- Or include teams with at least one care leaver -- ORDER BY -- CaseloadCount DESC; @@ -305,7 +306,7 @@ LEFT JOIN ssd_development.ssd_department AS d ON i.invo_professional_team = d.dept_team_id WHERE UPPER(i.invo_professional_role_id) = 'ALLOCATED CASE WORKER' -- filter on Case Workers only - AND i.invo_involvement_end_date IS NULL -- open cases only + AND i.invo_involvement_end_date IS NULL -- active cases only GROUP BY d.dept_team_name, d.dept_team_parent_name