generated from ncl-icb-analytics/ncl_project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAE_processing.sql
202 lines (169 loc) · 6.63 KB
/
AE_processing.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
-- balanced scorecard - IP
-- CS 5/9/23
use data_lab_Sbi
DROP TABLE if exists [Data_Lab_SBI].[dbo].[ML_BS_MPI]
SELECT
[Month],
[Month_no],
OPA.OACode as LSOA,
Legacy_CCG_Name as GP_Borough_Name,
[PCN_NAME],
[PRACTICE_CODE],
[PRACTICE_NAME],
99 as Quintile,
floor(datediff(year,fpro.dateofbirth,getdate())/15)*15 AgeBand,
gend.[GenderCode2] as Gender,
count( distinct fpra.sk_patientid) as PERSONS
into [Data_Lab_SBI].[dbo].[ML_BS_MPI]
FROM [Data_Lab_SBI].[dbo].[CS_months]
join [Fact].[Patient].[FactPractice] fpra
on 1=1
JOIN [Fact].[Patient].[FactProfile] fpro
on fpra.sk_patientid=fpro.sk_patientid
join dictionary.dbo.Organisation org1
on fpra.SK_OrganisationID=org1.sk_organisationid
join Dictionary.dbo.Organisation org2
on org1.SK_OrganisationID_ParentOrg=org2.SK_Organisation_ID
join [Dictionary].[dbo].[Gender] gend
on fpro.sk_genderid = gend.sk_genderid
join [Fact].[Patient].[FactResidence] fres
on fres.sk_patientid=fpro.sk_patientid
join [Dictionary].[dbo].[OutputArea] opa
on fres.SK_OutputAreaID = opa.SK_OutputAreaID
join [NCL].[_dictionary].[NCL_PCN_2021] pcn
on org1.organisation_code = pcn.PRACTICE_CODE
where
(fres.PeriodStart<[End_Date])
and (fres.PeriodEnd>=[Start_Date] or fres.PeriodEnd='9999-12-31' )
and (fres.DateDetectedEnd>=[Start_Date] or fres.DateDetectedEnd is null)
and (fpra.PeriodStart<[End_Date])
and (fpra.PeriodEnd>=[Start_Date] or fpra.PeriodEnd='9999-12-31' )
and (fpra.DateDetectedLeft>=[Start_Date] or fpra.DateDetectedLeft is null)
and (fpro.PeriodStart<[End_Date])
and (fpro.PeriodEnd>=[Start_Date] or fpro.PeriodEnd='9999-12-31' )
and (fpro.dateofdeath>=[Start_Date] or fpro.dateofdeath is null)
and fpra.SK_DataSourceID=7
and fpro.SK_DataSourceID=7
and fres.SK_DataSourceID=7
and OPA.CensusYear = '2011'
and Legacy_CCG_Name <> ''
group by [Month],
[Month_no],
OPA.OACode,
Legacy_CCG_Name,
[PCN_NAME],
[PRACTICE_CODE],
[PRACTICE_NAME],
floor(datediff(year,fpro.dateofbirth,getdate())/15)*15,
gend.[GenderCode2]
update A set Quintile = IMD_Quintile
from [Data_Lab_SBI].[dbo].[ML_BS_MPI] A
join [Data_Lab_SBI].[RD].[IMD2019] B
on A.LSOA = B.LSOA_code_2011
-- now activity
DROP TABLE if exists [Data_Lab_SBI].[dbo].[ML_BS_AE]
SELECT
[Month],
[Month_no],
OPA.OACode as LSOA,
Legacy_CCG_Name as GP_Borough_Name,
[PCN_NAME],
[PRACTICE_CODE],
[PRACTICE_NAME],
floor(datediff(year,fpro.dateofbirth,getdate())/15)*15 AgeBand,
gend.[GenderCode2] as Gender,
99 as Quintile,
count( *) as AE_ATTENDS
into [Data_Lab_SBI].[dbo].[ML_BS_AE]
FROM [Data_Lab_SBI].[dbo].[CS_months]
join SUS.IP.EncounterDenormalised A
on 1=1
join [Fact].[Patient].[FactPractice] fpra
on fpra.sk_patientid=A.sk_patientid
JOIN [Fact].[Patient].[FactProfile] fpro
on fpra.sk_patientid=fpro.sk_patientid
join dictionary.dbo.Organisation org1
on fpra.SK_OrganisationID=org1.sk_organisationid
join Dictionary.dbo.Organisation org2
on org1.SK_OrganisationID_ParentOrg=org2.SK_Organisation_ID
join [Dictionary].[dbo].[Gender] gend
on fpro.sk_genderid = gend.sk_genderid
join [Fact].[Patient].[FactResidence] fres
on fres.sk_patientid=fpro.sk_patientid
join [Dictionary].[dbo].[OutputArea] opa
on fres.SK_OutputAreaID = opa.SK_OutputAreaID
join [NCL].[_dictionary].[NCL_PCN_2021] pcn
on org1.organisation_code = pcn.PRACTICE_CODE
where
(fres.PeriodStart<[End_Date])
and (fres.PeriodEnd>=[Start_Date] or fres.PeriodEnd='9999-12-31' )
and (fres.DateDetectedEnd>=[Start_Date] or fres.DateDetectedEnd is null)
and (fpra.PeriodStart<[End_Date])
and (fpra.PeriodEnd>=[Start_Date] or fpra.PeriodEnd='9999-12-31' )
and (fpra.DateDetectedLeft>=[Start_Date] or fpra.DateDetectedLeft is null)
and (fpro.PeriodStart<[End_Date])
and (fpro.PeriodEnd>=[Start_Date] or fpro.PeriodEnd='9999-12-31' )
and (fpro.dateofdeath>=[Start_Date] or fpro.dateofdeath is null)
and fpra.SK_DataSourceID=7
and fpro.SK_DataSourceID=7
and fres.SK_DataSourceID=7
and OPA.CensusYear = '2011'
and Legacy_CCG_Name <> ''
and Episode_Start_Date >= Start_Date
and Episode_Start_Date < End_Date
and left(Admission_Method_Hospital_Provider_Spell,1) in ('1','2')
and Episode_Number=1
group by [Month],
[Month_no],
OPA.OACode,
Legacy_CCG_Name,
[PCN_NAME],
[PRACTICE_CODE],
[PRACTICE_NAME],
floor(datediff(year,fpro.dateofbirth,getdate())/15)*15,
gend.[GenderCode2]
update A set Quintile = IMD_Quintile
from [Data_Lab_SBI].[dbo].[ML_BS_AE] A
join [Data_Lab_SBI].[RD].[IMD2019] B
on A.LSOA = B.LSOA_code_2011
-- output by month
/*
select A.month, A.month_no, A.GP_Borough_Name, A.Quintile, A.ageband, A.gender, sum(PERSONS) as PERSONS, sum(AE_ATTENDS) as AE_ATTENDS
from [Data_Lab_SBI].[dbo].[ML_BS_MPI] A
left join [Data_Lab_SBI].[dbo].[ML_BS_AE] B
on A.month_no = B.month_no
and A.GP_Borough_Name=B.GP_Borough_Name
and A.Quintile = B.Quintile
and A.ageband = B.ageband
and A.gender = B.gender
and A.LSOA = B.LSOA
where A.quintile<>99
and A.gender <> 'U'
and A.ageband <105
group by A.month, A.month_no, A.GP_Borough_Name, A.Quintile, A.ageband, A.gender
*/
-- output most recent month
select A.GP_Borough_Name,
A.[PCN_NAME],
A.[PRACTICE_CODE],
A.[PRACTICE_NAME],A.Quintile, A.ageband, A.gender, sum(PERSONS) as PERSONS, sum(AE_ATTENDS) as AE_ATTENDS
from [Data_Lab_SBI].[dbo].[ML_BS_MPI] A
left join [Data_Lab_SBI].[dbo].[ML_BS_AE] B
on A.month_no = B.month_no
and A.GP_Borough_Name=B.GP_Borough_Name
and A.Quintile = B.Quintile
and A.ageband = B.ageband
and A.gender = B.gender
and A.[PCN_NAME] = B.[PCN_NAME]
and A.[PRACTICE_CODE]= B.[PRACTICE_CODE]
and A.[PRACTICE_NAME]= B.[PRACTICE_NAME]
and A.LSOA = B.LSOA
where A.quintile<>99
and A.gender <> 'U'
and A.ageband <105
and A.month_no = 19
group by
A.GP_Borough_Name,
A.[PCN_NAME],
A.[PRACTICE_CODE],
A.[PRACTICE_NAME], A.Quintile, A.ageband, A.gender