Skip to content

Commit

Permalink
prof_staff_id changes completed
Browse files Browse the repository at this point in the history
  • Loading branch information
robjharrison committed Jun 5, 2024
1 parent 51d65e1 commit 388bf68
Show file tree
Hide file tree
Showing 7 changed files with 329 additions and 390 deletions.
272 changes: 134 additions & 138 deletions cms_ssd_extract_sql/mosaic/SQL_Professionals_DW.sql
Original file line number Diff line number Diff line change
Expand Up @@ -16,166 +16,162 @@ DECLARE
@ssd_timeframe_years INT = 6,
@ssd_sub1_range_years INT = 1


IF OBJECT_ID('Tempdb..#t','u') IS NOT NULL
BEGIN
DROP TABLE #t
DROP TABLE #t
END
CREATE TABLE #t
(
prof_table_id INT /*Information not held as a separate field as Worker ID is unique*/,
prof_professional_id INT,
--FIRST_NAMES VARCHAR(50),
--LAST_NAMES VARCHAR(50),
prof_social_worker_registration_no VARCHAR(20),
prof_agency_worker_flag VARCHAR(10) /*Field removed from SSDS*/,
prof_professional_job_title VARCHAR(100),
prof_professional_caseload INT DEFAULT 0,
prof_professional_department VARCHAR(100),
prof_full_time_equivalency DEC (10,2) /*Information not held in Mosaic; held in HR System (I-Trent)*/
prof_table_id INT /*Information not held as a separate field as Worker ID is unique*/,
prof_staff_id INT,
--FIRST_NAMES VARCHAR(50),
--LAST_NAMES VARCHAR(50),
prof_social_worker_registration_no VARCHAR(20),
prof_agency_worker_flag VARCHAR(10) /*Field removed from SSDS*/,
prof_professional_job_title VARCHAR(100),
prof_professional_caseload INT DEFAULT 0,
prof_professional_department VARCHAR(100),
prof_full_time_equivalency DEC (10,2) /*Information not held in Mosaic; held in HR System (I-Trent)*/
)

INSERT #t
(
prof_table_id,
prof_professional_id,
--FIRST_NAMES,
--LAST_NAMES,
prof_social_worker_registration_no,
prof_agency_worker_flag,
prof_professional_job_title,
prof_professional_department,
prof_full_time_equivalency
prof_table_id,
prof_staff_id,
--FIRST_NAMES,
--LAST_NAMES,
prof_social_worker_registration_no,
prof_agency_worker_flag,
prof_professional_job_title,
prof_professional_department,
prof_full_time_equivalency
)

Select
d.prof_table_id,
d.prof_professional_id,
--d.FIRST_NAMES,
--d.LAST_NAMES,
d.prof_social_worker_registration_no,
d.prof_agency_worker_flag,
d.prof_professional_job_title,
d.prof_professional_department,
d.prof_full_time_equivalency

from
SELECT
d.prof_table_id,
d.prof_staff_id,
--d.FIRST_NAMES,
--d.LAST_NAMES,
d.prof_social_worker_registration_no,
d.prof_agency_worker_flag,
d.prof_professional_job_title,
d.prof_professional_department,
d.prof_full_time_equivalency

FROM
(
Select
NULL prof_table_id /*Information not held as a separate field as Worker ID is unique*/,
a.ID prof_professional_id,
a.FIRST_NAMES,
a.LAST_NAMES,
COALESCE(b.HCPCIdentifier, 'XX' + CAST(a.id as VARCHAR)) prof_social_worker_registration_no,
NULL prof_agency_worker_flag, /*Field removed from SSDS*/
wrt.DESCRIPTION prof_professional_job_title,
org.NAME prof_professional_department,
NULL prof_full_time_equivalency, /*Information not held in Mosaic; held in HR System (I-Trent)*/
DENSE_RANK() OVER(PARTITION BY a.ID ORDER BY COALESCE(wr.END_DATE, '99991231') DESC, wr.ID DESC) Rnk

from Mosaic.M.WORKERS a
inner join Mosaic.M.WORKER_ROLES wr on a.ID = wr.WORKER_ID
inner join Mosaic.M.WORKER_ROLE_TYPES wrt on wr.ROLE = wrt.ROLE
inner join Mosaic.M.ORGANISATIONS org on wr.ORG_ID = org.ID
left join Returns.CSCWC.HCPC b on a.ID = b.WID

where wrt.DESCRIPTION not like '%auth%' /*Authorisor roles which are additional to main role*/
and wr.START_DATE <= @STARTTIME
and COALESCE(wr.END_DATE,'99991231') > @STARTTIME

and wr.ORG_ID in
/*Get current Teams within 'Children's Social Care' in Mosaic Organisation Hierarchy*/
(
Select
d1.OHLevel1ID

from
(
Select
o1.ID OHLevel1ID,
o1.NAME OHLevel1Name,
o2.ID OHLevel2ID,
o2.NAME OHLevel2Name,
o3.ID OHLevel3ID,
o3.NAME OHLevel3Name,
o4.ID OHLevel4ID,
o4.NAME OHLevel4Name,
o5.ID OHLevel5ID,
o5.NAME OHLevel5Name,
o6.id OHLevel6ID,
o6.Name OHLevel6Name,
o7.id OHLevel7ID,
o7.Name OHLevel7Name

from Mosaic.M.ORGANISATIONS o1
left join Mosaic.M.ORGANISATION_STRUCTURE os1 on o1.id = os1.CHILD_ORG_ID
left join Mosaic.M.ORGANISATIONS o2 on os1.PARENT_ORG_ID = o2.id
left join Mosaic.M.ORGANISATION_STRUCTURE os2 on o2.id = os2.CHILD_ORG_ID
left join Mosaic.M.ORGANISATIONS o3 on os2.PARENT_ORG_ID = o3.id
left join Mosaic.M.ORGANISATION_STRUCTURE os3 on o3.id = os3.CHILD_ORG_ID
left join Mosaic.M.ORGANISATIONS o4 on os3.PARENT_ORG_ID = o4.id
left join Mosaic.M.ORGANISATION_STRUCTURE os4 on o4.id = os4.CHILD_ORG_ID
left join Mosaic.M.ORGANISATIONS o5 on os4.PARENT_ORG_ID = o5.id
left join Mosaic.M.ORGANISATION_STRUCTURE os5 on o5.id = os5.CHILD_ORG_ID
left join Mosaic.M.ORGANISATIONS o6 on os5.PARENT_ORG_ID = o6.id
left join Mosaic.M.ORGANISATION_STRUCTURE os6 on o6.id = os6.CHILD_ORG_ID
left join Mosaic.M.ORGANISATIONS o7 on os6.PARENT_ORG_ID = o7.id

where o1.ID = 1221140 /*Children's Social Care*/
or o2.ID = 1221140
or o3.ID = 1221140
or o4.ID = 1221140
or o5.ID = 1221140
or o6.ID = 1221140
or o7.ID = 1221140
)
d1
)
)
d

where d.Rnk = 1
SELECT
NULL prof_table_id /*Information not held as a separate field as Worker ID is unique*/,
a.ID prof_staff_id,
a.FIRST_NAMES,
a.LAST_NAMES,
COALESCE(b.HCPCIdentifier, 'XX' + CAST(a.id as VARCHAR)) prof_social_worker_registration_no,
NULL prof_agency_worker_flag, /*Field removed from SSDS*/,
wrt.DESCRIPTION prof_professional_job_title,
org.NAME prof_professional_department,
NULL prof_full_time_equivalency, /*Information not held in Mosaic; held in HR System (I-Trent)*/,
DENSE_RANK() OVER(PARTITION BY a.ID ORDER BY COALESCE(wr.END_DATE, '99991231') DESC, wr.ID DESC) Rnk

FROM Mosaic.M.WORKERS a
INNER JOIN Mosaic.M.WORKER_ROLES wr ON a.ID = wr.WORKER_ID
INNER JOIN Mosaic.M.WORKER_ROLE_TYPES wrt ON wr.ROLE = wrt.ROLE
INNER JOIN Mosaic.M.ORGANISATIONS org ON wr.ORG_ID = org.ID
LEFT JOIN Returns.CSCWC.HCPC b ON a.ID = b.WID

WHERE wrt.DESCRIPTION NOT LIKE '%auth%' /*Authorisor roles which are additional to main role*/
AND wr.START_DATE <= @STARTTIME
AND COALESCE(wr.END_DATE,'99991231') > @STARTTIME

AND wr.ORG_ID IN
/*Get current Teams within 'Children's Social Care' in Mosaic Organisation Hierarchy*/
(
SELECT
d1.OHLevel1ID

FROM
(
SELECT
o1.ID OHLevel1ID,
o1.NAME OHLevel1Name,
o2.ID OHLevel2ID,
o2.NAME OHLevel2Name,
o3.ID OHLevel3ID,
o3.NAME OHLevel3Name,
o4.ID OHLevel4ID,
o4.NAME OHLevel4Name,
o5.ID OHLevel5ID,
o5.NAME OHLevel5Name,
o6.ID OHLevel6ID,
o6.Name OHLevel6Name,
o7.ID OHLevel7ID,
o7.Name OHLevel7Name

FROM Mosaic.M.ORGANISATIONS o1
LEFT JOIN Mosaic.M.ORGANISATION_STRUCTURE os1 ON o1.ID = os1.CHILD_ORG_ID
LEFT JOIN Mosaic.M.ORGANISATIONS o2 ON os1.PARENT_ORG_ID = o2.ID
LEFT JOIN Mosaic.M.ORGANISATION_STRUCTURE os2 ON o2.ID = os2.CHILD_ORG_ID
LEFT JOIN Mosaic.M.ORGANISATIONS o3 ON os2.PARENT_ORG_ID = o3.ID
LEFT JOIN Mosaic.M.ORGANISATION_STRUCTURE os3 ON o3.ID = os3.CHILD_ORG_ID
LEFT JOIN Mosaic.M.ORGANISATIONS o4 ON os3.PARENT_ORG_ID = o4.ID
LEFT JOIN Mosaic.M.ORGANISATION_STRUCTURE os4 ON o4.ID = os4.CHILD_ORG_ID
LEFT JOIN Mosaic.M.ORGANISATIONS o5 ON os4.PARENT_ORG_ID = o5.ID
LEFT JOIN Mosaic.M.ORGANISATION_STRUCTURE os5 ON o5.ID = os5.CHILD_ORG_ID
LEFT JOIN Mosaic.M.ORGANISATIONS o6 ON os5.PARENT_ORG_ID = o6.ID
LEFT JOIN Mosaic.M.ORGANISATION_STRUCTURE os6 ON o6.ID = os6.CHILD_ORG_ID
LEFT JOIN Mosaic.M.ORGANISATIONS o7 ON os6.PARENT_ORG_ID = o7.ID

WHERE o1.ID = 1221140 /*Children's Social Care*/
OR o2.ID = 1221140
OR o3.ID = 1221140
OR o4.ID = 1221140
OR o5.ID = 1221140
OR o6.ID = 1221140
OR o7.ID = 1221140
) d1
)
) d

WHERE d.Rnk = 1


/*Number of Open Cases*/
UPDATE #t
SET prof_professional_caseload = d.prof_professional_caseload

from #t t
inner join
FROM #t t
INNER JOIN
(
Select
pw.WORKER_ID prof_professional_id,
COUNT(*) prof_professional_caseload
SELECT
pw.WORKER_ID prof_staff_id,
COUNT(*) prof_professional_caseload

from Mosaic.M.PEOPLE_WORKERS pw
FROM Mosaic.M.PEOPLE_WORKERS pw

where pw.START_DATE <= @STARTTIME
and COALESCE(pw.END_DATE, '99991231') > @STARTTIME
and pw.TYPE in ('ALLWKR' /*Allocated Worker*/,
'AWAKENTEAN' /*Awaken Team*/,
'FAMFINDSOC' /*Adoption*/,
'FAMPLACEMENTWORK' /*Fostering*/,
'PASSW')
WHERE pw.START_DATE <= @STARTTIME
AND COALESCE(pw.END_DATE, '99991231') > @STARTTIME
AND pw.TYPE IN ('ALLWKR' /*Allocated Worker*/,
'AWAKENTEAN' /*Awaken Team*/,
'FAMFINDSOC' /*Adoption*/,
'FAMPLACEMENTWORK' /*Fostering*/,
'PASSW')

group by pw.WORKER_ID
)
d on t.prof_professional_id = d.prof_professional_id
GROUP BY pw.WORKER_ID
) d ON t.prof_staff_id = d.prof_staff_id


/*Output Data*/
Select
t.prof_table_id,
t.prof_professional_id,
--t.FIRST_NAMES,
--t.LAST_NAMES,
t.prof_social_worker_registration_no,
t.prof_agency_worker_flag,
t.prof_professional_job_title,
t.prof_professional_caseload,
t.prof_professional_department,
t.prof_full_time_equivalency

from #t t

order by t.prof_professional_id
SELECT
t.prof_table_id,
t.prof_staff_id,
--t.FIRST_NAMES,
--t.LAST_NAMES,
t.prof_social_worker_registration_no,
t.prof_agency_worker_flag,
t.prof_professional_job_title,
t.prof_professional_caseload,
t.prof_professional_department,
t.prof_full_time_equivalency

FROM #t t

ORDER BY t.prof_staff_id
Loading

0 comments on commit 388bf68

Please sign in to comment.