forked from HMIS/LSASampleCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
10 LSACalculated Data Quality.sql
370 lines (334 loc) · 14.8 KB
/
10 LSACalculated Data Quality.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
LSA FY2024 Sample Code
Name: 10 LSACalculated Data Quality.sql
FY2024 Changes
None
(Detailed revision history maintained at https://github.com/HMIS/LSASampleCode)/
10.2 Get Counts of Enrollments Active after Operating End Date by ProjectID
*/
delete from lsa_Calculated where ReportRow in (901,902)
insert into lsa_Calculated
(Value, Cohort, Universe, HHType
, Population, SystemPath, ReportRow, ProjectID, ReportID, Step)
select count (distinct n.EnrollmentID), 1, 10, 0, 0, -1
, case when hx.ExitDate is null then 901
else 902 end
, p.ProjectID, cd.ReportID, '10.2'
from tlsa_Enrollment n
left outer join hmis_Exit hx on hx.EnrollmentID = n.EnrollmentID
and hx.DateDeleted is null
inner join hmis_Project p on p.ProjectID = n.ProjectID
inner join tlsa_CohortDates cd on cd.Cohort = 1
and p.OperatingEndDate between cd.CohortStart and cd.CohortEnd
where (hx.ExitDate is null or hx.ExitDate > p.OperatingEndDate)
group by case when hx.ExitDate is null then 901
else 902 end
, p.ProjectID, cd.ReportID
/*
10.3 Get Counts of Night-by-Night Enrollments with Exit Date Discrepancies
*/
delete from lsa_Calculated where ReportRow in (903,904)
insert into lsa_Calculated
(Value, Cohort, Universe, HHType
, Population, SystemPath, ReportRow, ProjectID, ReportID, Step)
select count (distinct hn.EnrollmentID), 1, 10, 0, 0, -1
, case when hx.ExitDate is null or hx.ExitDate > cd.CohortEnd then 903
else 904 end
, p.ProjectID, cd.ReportID, '10.3'
from tlsa_Enrollment n
inner join tlsa_CohortDates cd on cd.Cohort = 1
inner join tlsa_HHID hhid on hhid.HouseholdID = n.HouseholdID
inner join hmis_Enrollment hn on hn.EnrollmentID = n.EnrollmentID
left outer join hmis_Exit hx on hx.EnrollmentID = hn.EnrollmentID
and hx.DateDeleted is NULL
inner join hmis_Project p on p.ProjectID = hn.ProjectID and p.ProjectType = 1 and p.ContinuumProject = 1
left outer join (select svc.EnrollmentID, max(svc.DateProvided) as LastBednight
from hmis_Services svc
inner join hmis_Enrollment nbn on nbn.EnrollmentID = svc.EnrollmentID
inner join hmis_Project p on p.ProjectID = nbn.ProjectID
and p.ProjectType = 1
and (p.OperatingEndDate is null or p.OperatingEndDate >= DateProvided)
inner join tlsa_CohortDates cd on cd.Cohort = 1
and svc.DateProvided between cd.CohortStart and cd.CohortEnd
where svc.RecordType = 200 and svc.DateDeleted is null
group by svc.EnrollmentID
) bn on bn.EnrollmentID = hhid.EnrollmentID
where ((hx.ExitDate is null or hx.ExitDate > cd.CohortEnd) and bn.LastBednight <= dateadd(dd, -90, cd.CohortEnd))
or (hx.ExitDate between cd.CohortStart and cd.CohortEnd and hx.ExitDate <> dateadd(dd, 1, bn.LastBednight))
group by case when hx.ExitDate is null or hx.ExitDate > cd.CohortEnd then 903
else 904 end
, p.ProjectID, cd.ReportID
/*
10.4 Get Counts of Households with no Enrollment CoC Record
*/
delete from lsa_Calculated where ReportRow = 905
insert into lsa_Calculated
(Value, Cohort, Universe, HHType
, Population, SystemPath, ReportRow, ProjectID, ReportID, Step)
select count (distinct hn.HouseholdID), 1, 10, 0, 0, -1, 905, p.ProjectID, rpt.ReportID, '10.4'
from lsa_Report rpt
inner join hmis_Enrollment hn on hn.EntryDate <= rpt.ReportEnd
inner join lsa_Project p on p.ProjectID = hn.ProjectID and p.ProjectType not in (9,10)
left outer join hmis_Exit hx on hx.EnrollmentID = hn.EnrollmentID
and hx.DateDeleted is null
left outer join hmis_Enrollment hoh on hoh.HouseholdID = hn.HouseholdID
and hoh.RelationshipToHoH = 1
and hoh.DateDeleted is null
where hn.DateDeleted is null
and hoh.EnrollmentCoC is null
and (hx.ExitDate is null or
(hx.ExitDate >= rpt.ReportStart and hx.ExitDate > hn.EntryDate))
group by p.ProjectID, rpt.ReportID
/*
10.5 DQ – Enrollments in non-participating projects
*/
delete from lsa_Calculated where ReportRow = 906
insert into lsa_Calculated
(Value, Cohort, Universe, HHType
, Population, SystemPath, ReportRow, ProjectID, ReportID, Step)
select count (distinct n.EnrollmentID), 1, 10, 0, 0, -1, 906, n.ProjectID, rpt.ReportID, '10.5'
from lsa_Report rpt
inner join hmis_Enrollment n on n.EntryDate <= rpt.ReportEnd
inner join hmis_Enrollment hoh on hoh.HouseholdID = n.HouseholdID
inner join lsa_Project p on p.ProjectID = n.ProjectID and p.ProjectType not in (9,10)
left outer join hmis_Exit x on x.EnrollmentID = n.EnrollmentID and x.DateDeleted is null
left outer join lsa_HMISParticipation part on part.ProjectID = n.ProjectID
and part.HMISParticipationType = 1
and part.HMISParticipationStatusStartDate <= n.EntryDate
and (part.HMISParticipationStatusEndDate is null
or part.HMISParticipationStatusEndDate >= x.ExitDate
or (x.ExitDate is null and part.HMISParticipationStatusEndDate > rpt.ReportEnd))
where hoh.RelationshipToHoH = 1 and hoh.EnrollmentCoC = rpt.ReportCoC and part.ProjectID is null
and (x.ExitDate is null
or (x.ExitDate >= ReportStart and x.ExitDate > n.EntryDate))
and n.DateDeleted is null and hoh.DateDeleted is null
group by n.ProjectID, rpt.ReportID
/*
10.6 DQ – Enrollments without exactly one HoH
*/
delete from lsa_Calculated where ReportRow = 907
insert into lsa_Calculated
(Value, Cohort, Universe, HHType
, Population, SystemPath, ReportRow, ProjectID, ReportID, Step)
select count (distinct hn.EnrollmentID), 1, 10, 0, 0, -1, 907, hn.ProjectID, rpt.ReportID, '10.6'
from lsa_Report rpt
inner join hmis_Enrollment hn on hn.EntryDate <= rpt.ReportEnd
inner join lsa_Project p on p.ProjectID = hn.ProjectID
inner join hmis_Enrollment coc on coc.HouseholdID = hn.HouseholdID and coc.EnrollmentCoC = rpt.ReportCoC
left outer join hmis_Exit hx on hx.EnrollmentID = hn.EnrollmentID
and hx.DateDeleted is null
left outer join (select hoh.HouseholdID, count(hoh.PersonalID) as hoh
from hmis_Enrollment hoh
where hoh.RelationshipToHoH = 1 and hoh.DateDeleted is null
group by hoh.HouseholdID) counthoh on counthoh.HouseholdID = hn.HouseholdID
where (counthoh.HouseholdID is null or counthoh.hoh > 1)
and p.ProjectType not in (9,10)
and hn.DateDeleted is null
and (hx.ExitDate is null or
(hx.ExitDate >= rpt.ReportStart and hx.ExitDate > hn.EntryDate))
group by hn.ProjectID, rpt.ReportID
/*
10.7 DQ – Relationship to HoH
*/
delete from lsa_Calculated where ReportRow = 908
insert into lsa_Calculated
(Value, Cohort, Universe, HHType
, Population, SystemPath, ReportRow, ProjectID, ReportID, Step)
select count (distinct hn.EnrollmentID), 1, 10, 0, 0, -1, 908, hn.ProjectID, rpt.ReportID, '10.7'
from lsa_Report rpt
inner join hmis_Enrollment hn on hn.EntryDate <= rpt.ReportEnd
inner join lsa_Project p on p.ProjectID = hn.ProjectID
inner join hmis_Enrollment coc on coc.HouseholdID = hn.HouseholdID and coc.EnrollmentCoC = rpt.ReportCoC
left outer join hmis_Exit hx on hx.EnrollmentID = hn.EnrollmentID
and hx.DateDeleted is null
where (hn.RelationshipToHoH is null or hn.RelationshipToHoH not between 1 and 5)
and p.ProjectType not in (9,10)
and hn.DateDeleted is null
and (hx.ExitDate is null or
(hx.ExitDate >= rpt.ReportStart and hx.ExitDate > hn.EntryDate))
group by hn.ProjectID, rpt.ReportID
/*
10.8 DQ – Household Entry
*/
delete from lsa_Calculated where ReportRow = 909
insert into lsa_Calculated
(Value, Cohort, Universe, HHType
, Population, SystemPath, ReportRow, ProjectID, ReportID, Step)
select count (distinct hh.HouseholdID), 1, 10, 0, 0, -1, 909, hh.ProjectID, rpt.ReportID, '10.8'
from lsa_Report rpt
inner join tlsa_HHID hh on hh.EntryDate <= rpt.ReportEnd
where hh.Active = 1
group by hh.ProjectID, rpt.ReportID
/*
10.9 DQ – Client Entry
*/
delete from lsa_Calculated where ReportRow = 910
insert into lsa_Calculated
(Value, Cohort, Universe, HHType
, Population, SystemPath, ReportRow, ProjectID, ReportID, Step)
select count (distinct n.EnrollmentID), 1, 10, 0, 0, -1, 910, n.ProjectID, rpt.ReportID, '10.9'
from lsa_Report rpt
inner join tlsa_Enrollment n on n.EntryDate <= rpt.ReportEnd
where n.Active = 1
group by n.ProjectID, rpt.ReportID
/*
10.10 DQ – Adult/HoH Entry
*/
delete from lsa_Calculated where ReportRow = 911
insert into lsa_Calculated
(Value, Cohort, Universe, HHType
, Population, SystemPath, ReportRow, ProjectID, ReportID, Step)
select count (distinct n.EnrollmentID), 1, 10, 0, 0, -1, 911, n.ProjectID, rpt.ReportID, '10.10'
from lsa_Report rpt
inner join tlsa_Enrollment n on n.EntryDate <= rpt.ReportEnd
where n.Active = 1
and (n.RelationshipToHoH = 1 or n.ActiveAge between 18 and 65)
group by n.ProjectID, rpt.ReportID
/*
10.11 DQ – Client Exit
*/
delete from lsa_Calculated where ReportRow = 912
insert into lsa_Calculated
(Value, Cohort, Universe, HHType
, Population, SystemPath, ReportRow, ProjectID, ReportID, Step)
select count (distinct n.EnrollmentID), 1, 10, 0, 0, -1, 912, n.ProjectID, rpt.ReportID, '10.11'
from lsa_Report rpt
inner join tlsa_Enrollment n on n.ExitDate <= rpt.ReportEnd
where n.Active = 1
group by n.ProjectID, rpt.ReportID
/*
10.12 DQ – Disabling Condition
*/
delete from lsa_Calculated where ReportRow = 913
insert into lsa_Calculated
(Value, Cohort, Universe, HHType
, Population, SystemPath, ReportRow, ProjectID, ReportID, Step)
select count (distinct n.EnrollmentID), 1, 10, 0, 0, -1, 913, n.ProjectID, rpt.ReportID, '10.12'
from lsa_Report rpt
inner join tlsa_Enrollment n on n.EntryDate <= rpt.ReportEnd
where n.Active = 1 and n.DisabilityStatus = 99
group by n.ProjectID, rpt.ReportID
/*
10.13 DQ – Living Situation
*/
delete from lsa_Calculated where ReportRow = 914
insert into lsa_Calculated
(Value, Cohort, Universe, HHType
, Population, SystemPath, ReportRow, ProjectID, ReportID, Step)
select count (distinct n.EnrollmentID), 1, 10, 0, 0, -1, 914, n.ProjectID, rpt.ReportID, '10.13'
from lsa_Report rpt
inner join tlsa_Enrollment n on n.EntryDate <= rpt.ReportEnd
inner join hmis_Enrollment hn on hn.EnrollmentID = n.EnrollmentID
where n.Active = 1
and (n.RelationshipToHoH = 1 or n.ActiveAge between 18 and 65)
and (hn.LivingSituation in (8,9,99) or hn.LivingSituation is NULL)
group by n.ProjectID, rpt.ReportID
/*
10.14 DQ – Length of Stay
*/
delete from lsa_Calculated where ReportRow = 915
insert into lsa_Calculated
(Value, Cohort, Universe, HHType
, Population, SystemPath, ReportRow, ProjectID, ReportID, Step)
select count (distinct n.EnrollmentID), 1, 10, 0, 0, -1, 915, n.ProjectID, rpt.ReportID, '10.14'
from lsa_Report rpt
inner join tlsa_Enrollment n on n.EntryDate <= rpt.ReportEnd
inner join hmis_Enrollment hn on hn.EnrollmentID = n.EnrollmentID
where n.Active = 1
and (n.RelationshipToHoH = 1 or n.ActiveAge between 18 and 65)
and (hn.LengthOfStay in (8,9,99) or hn.LengthOfStay is NULL)
group by n.ProjectID, rpt.ReportID
/*
10.15 DQ – Date ES/SH/Street Homelessness Started
*/
delete from lsa_Calculated where ReportRow = 916
insert into lsa_Calculated
(Value, Cohort, Universe, HHType
, Population, SystemPath, ReportRow, ProjectID, ReportID, Step)
select count (distinct n.EnrollmentID), 1, 10, 0, 0, -1, 916, n.ProjectID, rpt.ReportID, '10.15'
from lsa_Report rpt
inner join tlsa_Enrollment n on n.EntryDate <= rpt.ReportEnd
inner join hmis_Enrollment hn on hn.EnrollmentID = n.EnrollmentID
where n.Active = 1
and (n.RelationshipToHoH = 1 or n.ActiveAge between 18 and 65)
and (hn.DateToStreetESSH > hn.EntryDate
or (hn.DateToStreetESSH is null
and (n.LSAProjectType in (0,1,8)
or hn.LivingSituation in (101,116,118)
or hn.PreviousStreetESSH = 1
)
)
)
group by n.ProjectID, rpt.ReportID
/*
10.16 DQ – Times ES/SH/Street Homeless Last 3 Years
*/
delete from lsa_Calculated where ReportRow = 917
insert into lsa_Calculated
(Value, Cohort, Universe, HHType
, Population, SystemPath, ReportRow, ProjectID, ReportID, Step)
select count (distinct n.EnrollmentID), 1, 10, 0, 0, -1, 917, n.ProjectID, rpt.ReportID, '10.16'
from lsa_Report rpt
inner join tlsa_Enrollment n on n.EntryDate <= rpt.ReportEnd
inner join hmis_Enrollment hn on hn.EnrollmentID = n.EnrollmentID
where n.Active = 1
and (n.RelationshipToHoH = 1 or n.ActiveAge between 18 and 65)
and (hn.TimesHomelessPastThreeYears is NULL
or hn.TimesHomelessPastThreeYears not in (1,2,3,4))
and (n.LSAProjectType in (0,1,8)
or hn.LivingSituation in (101,116,118)
or hn.PreviousStreetESSH = 1)
group by n.ProjectID, rpt.ReportID
/*
10.17 DQ – Months ES/SH/Street Homeless Last 3 Years
*/
delete from lsa_Calculated where ReportRow = 918
insert into lsa_Calculated
(Value, Cohort, Universe, HHType
, Population, SystemPath, ReportRow, ProjectID, ReportID, Step)
select count (distinct n.EnrollmentID), 1, 10, 0, 0, -1, 918, n.ProjectID, rpt.ReportID, '10.17'
from lsa_Report rpt
inner join tlsa_Enrollment n on n.EntryDate <= rpt.ReportEnd
inner join hmis_Enrollment hn on hn.EnrollmentID = n.EnrollmentID
where n.Active = 1
and (n.RelationshipToHoH = 1 or n.ActiveAge between 18 and 65)
and (hn.MonthsHomelessPastThreeYears is NULL
or hn.MonthsHomelessPastThreeYears not between 101 and 113)
and (n.LSAProjectType in (0,1,8)
or hn.LivingSituation in (101,116,118)
or hn.PreviousStreetESSH = 1)
group by n.ProjectID, rpt.ReportID
/*
10.18 DQ – Destination
*/
delete from lsa_Calculated where ReportRow = 919
insert into lsa_Calculated
(Value, Cohort, Universe, HHType
, Population, SystemPath, ReportRow, ProjectID, ReportID, Step)
select count (distinct n.EnrollmentID), 1, 10, 0, 0, -1, 919, n.ProjectID, rpt.ReportID, '10.18'
from lsa_Report rpt
inner join tlsa_Enrollment n on n.ExitDate <= rpt.ReportEnd
inner join hmis_Exit x on x.EnrollmentID = n.EnrollmentID and x.DateDeleted is null
where n.Active = 1
and (x.Destination is NULL or x.Destination in (8,9,17,30,99)
or (x.Destination = 435 and x.DestinationSubsidyType is NULL))
group by n.ProjectID, rpt.ReportID
/*
10.19 DQ – Date of Birth
*/
delete from lsa_Calculated where ReportRow = 920
insert into lsa_Calculated
(Value, Cohort, Universe, HHType
, Population, SystemPath, ReportRow, ProjectID, ReportID, Step)
select count (distinct n.PersonalID), 1, 10, 0, 0, -1, 920, n.ProjectID, rpt.ReportID, '10.19'
from lsa_Report rpt
inner join tlsa_Enrollment n on n.EntryDate <= rpt.ReportEnd
where n.Active = 1 and n.ActiveAge in (98,99)
group by n.ProjectID, rpt.ReportID
/*
10.20 LSACalculated
NOTE: Export of lsa_Calculated data to LSACalculated.csv has to exclude the Step column.
alter lsa_Calculated drop column Step
select Value, Cohort, Universe, HHType, Population, SystemPath, ProjectID, ReportRow, ReportID
from lsa_Calculated
*/