Skip to content

Commit 495035a

Browse files
No logic changes - minor speed improvements
Closes #1209
1 parent 06c0381 commit 495035a

File tree

2 files changed

+42
-43
lines changed

2 files changed

+42
-43
lines changed

08 LSACalculated Averages for LSAHousehold and LSAExit.sql

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,7 @@ Populates and references:
323323
inner join tlsa_AveragePops p1 on p1.PopID = rp.Pop1
324324
inner join tlsa_AveragePops p2 on p2.PopID = rp.Pop2
325325
and p1.Cohort = p2.Cohort
326-
and ((p1.HHType = p2.HHType and p1.HoHID = p2.HoHID)
327-
or (p1.PopID = 0 and p2.PopID = 0))
326+
and p1.HHType = p2.HHType and p1.HoHID = p2.HoHID
328327

329328
/*
330329
8.4-8.7 Average Days from LSAHousehold
@@ -356,7 +355,7 @@ Populates and references:
356355
hh.ReportID, '8.4-8.7'
357356
from tlsa_Household hh
358357
inner join tlsa_AveragePops pop on (pop.PopID = 0 or (pop.HHType = hh.HHType and pop.HoHID = hh.HoHID)) and pop.Cohort = 1
359-
inner join ref_RowPopulations rp on rp.PopID = pop.PopID
358+
inner join ref_RowPopulations rp on rp.PopID = pop.PopID and rp.RowMin between 1 and 16
360359
inner join ref_PopHHTypes ph on ph.PopID = rp.PopID and (ph.HHType = hh.HHType or ph.HHType = 0)
361360
inner join ref_RowValues rv on rv.RowID between rp.RowMin and rp.RowMax
362361
and ((rp.ByPath is null and rv.SystemPath = -1)

09 LSACalculated AHAR Counts.sql

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -31,62 +31,62 @@ Populates and references:
3131
values (0, '9.1.0')
3232

3333
insert into tlsa_CountPops (PopID, HouseholdID, Step)
34-
select 10, HouseholdID, '9.1.1'
34+
select distinct 10, HouseholdID, '9.1.1'
3535
from tlsa_HHID
3636
where AHAR = 1 and HHAdultAge = 18
3737
and ActiveHHType = 1
3838

3939
insert into tlsa_CountPops (PopID, HouseholdID, Step)
40-
select 11, HouseholdID, '9.1.2'
40+
select distinct 11, HouseholdID, '9.1.2'
4141
from tlsa_HHID
4242
where AHAR = 1 and HHAdultAge = 24
4343
and ActiveHHType = 1
4444

4545
insert into tlsa_CountPops (PopID, HouseholdID, Step)
46-
select 12, HouseholdID, '9.1.3'
46+
select distinct 12, HouseholdID, '9.1.3'
4747
from tlsa_HHID
4848
where AHAR = 1 and HHParent = 1 and HHAdultAge in (18,24)
4949
and ActiveHHType = 2
5050

5151
insert into tlsa_CountPops (PopID, HouseholdID, Step)
52-
select 13, HouseholdID, '9.1.4'
52+
select distinct 13, HouseholdID, '9.1.4'
5353
from tlsa_HHID
5454
where AHAR = 1 and HHVet = 1
5555

5656
insert into tlsa_CountPops (PopID, HouseholdID, Step)
57-
select 14, HouseholdID, '9.1.5'
57+
select distinct 14, HouseholdID, '9.1.5'
5858
from tlsa_HHID
5959
where AHAR = 1 and HHVet = 0 and HHAdultAge in (25,55)
6060
and ActiveHHType = 1
6161

6262
insert into tlsa_CountPops (PopID, HouseholdID, Step)
63-
select 15, HouseholdID, '9.1.6'
63+
select distinct 15, HouseholdID, '9.1.6'
6464
from tlsa_HHID
6565
where AHAR = 1 and HHChronic = 1
6666

6767
insert into tlsa_CountPops (PopID, HouseholdID, Step)
68-
select 18, HouseholdID, '9.1.7'
68+
select distinct 18, HouseholdID, '9.1.7'
6969
from tlsa_HHID
7070
where AHAR = 1 and HHDisability = 1
7171

7272
insert into tlsa_CountPops (PopID, HouseholdID, Step)
73-
select 19, HouseholdID, '9.1.8'
73+
select distinct 19, HouseholdID, '9.1.8'
7474
from tlsa_HHID
7575
where AHAR = 1 and HHFleeingDV = 1
7676

7777
insert into tlsa_CountPops (PopID, HouseholdID, Step)
78-
select 45, HouseholdID, '9.1.9'
78+
select distinct 45, HouseholdID, '9.1.9'
7979
from tlsa_HHID
8080
where AHAR = 1 and HHAdultAge = 55
8181
and ActiveHHType = 1
8282

8383
insert into tlsa_CountPops (PopID, HouseholdID, Step)
84-
select 46, HouseholdID, '9.1.10'
84+
select distinct 46, HouseholdID, '9.1.10'
8585
from tlsa_HHID
8686
where AHAR = 1 and HHParent = 1 and ActiveHHType = 3
8787

8888
insert into tlsa_CountPops (PopID, HouseholdID, Step)
89-
select 48, HouseholdID, '9.1.11'
89+
select distinct 48, HouseholdID, '9.1.11'
9090
from tlsa_HHID
9191
where AHAR = 1 and HHFleeingDV = 2
9292

@@ -97,12 +97,12 @@ Populates and references:
9797
where n.AHAR = 1 and lp.VetStatus = 1
9898

9999
insert into tlsa_CountPops (PopID, PersonalID, HouseholdID, Step)
100-
select 51, hhid.HoHID, hhid.HouseholdID, '9.1.13'
100+
select distinct 51, hhid.HoHID, hhid.HouseholdID, '9.1.13'
101101
from tlsa_HHID hhid
102102
where hhid.AHAR = 1 and hhid.HHAdultAge in (18,24) and hhid.HHParent = 1 and hhid.ActiveHHType = 2
103103

104104
insert into tlsa_CountPops (PopID, PersonalID, HouseholdID, Step)
105-
select 52, hhid.HoHID, hhid.HouseholdID, '9.1.14'
105+
select distinct 52, hhid.HoHID, hhid.HouseholdID, '9.1.14'
106106
from tlsa_HHID hhid
107107
where hhid.AHAR = 1 and hhid.HHParent = 1 and hhid.ActiveHHType = 3
108108

@@ -149,43 +149,43 @@ Populates and references:
149149
where n.AHAR = 1 and lp.RaceEthnicity not in (98,99)
150150

151151
insert into tlsa_CountPops (PopID, PersonalID, Step)
152-
select 71, n.PersonalID, '9.1.19'
152+
select distinct 71, n.PersonalID, '9.1.19'
153153
from tlsa_Enrollment n
154154
inner join tlsa_Person lp on lp.PersonalID = n.PersonalID
155155
where n.AHAR = 1 and cast(lp.RaceEthnicity as nvarchar) like '%1%'
156156

157157
insert into tlsa_CountPops (PopID, PersonalID, Step)
158-
select 72, n.PersonalID, '9.1.20'
158+
select distinct 72, n.PersonalID, '9.1.20'
159159
from tlsa_Enrollment n
160160
inner join tlsa_Person lp on lp.PersonalID = n.PersonalID
161161
where n.AHAR = 1 and cast(lp.RaceEthnicity as nvarchar) like '%2%'
162162

163163
insert into tlsa_CountPops (PopID, PersonalID, Step)
164-
select 73, n.PersonalID, '9.1.21'
164+
select distinct 73, n.PersonalID, '9.1.21'
165165
from tlsa_Enrollment n
166166
inner join tlsa_Person lp on lp.PersonalID = n.PersonalID
167167
where n.AHAR = 1 and cast(lp.RaceEthnicity as nvarchar) like '%3%'
168168

169169
insert into tlsa_CountPops (PopID, PersonalID, Step)
170-
select 74, n.PersonalID, '9.1.22'
170+
select distinct 74, n.PersonalID, '9.1.22'
171171
from tlsa_Enrollment n
172172
inner join tlsa_Person lp on lp.PersonalID = n.PersonalID
173173
where n.AHAR = 1 and cast(lp.RaceEthnicity as nvarchar) like '%6%'
174174

175175
insert into tlsa_CountPops (PopID, PersonalID, Step)
176-
select 75, n.PersonalID, '9.1.23'
176+
select distinct 75, n.PersonalID, '9.1.23'
177177
from tlsa_Enrollment n
178178
inner join tlsa_Person lp on lp.PersonalID = n.PersonalID
179179
where n.AHAR = 1 and cast(lp.RaceEthnicity as nvarchar) like '%7%'
180180

181181
insert into tlsa_CountPops (PopID, PersonalID, Step)
182-
select 76, n.PersonalID, '9.1.24'
182+
select distinct 76, n.PersonalID, '9.1.24'
183183
from tlsa_Enrollment n
184184
inner join tlsa_Person lp on lp.PersonalID = n.PersonalID
185185
where n.AHAR = 1 and cast(lp.RaceEthnicity as nvarchar) like '%4%'
186186

187187
insert into tlsa_CountPops (PopID, PersonalID, Step)
188-
select 77, n.PersonalID, '9.1.25'
188+
select distinct 77, n.PersonalID, '9.1.25'
189189
from tlsa_Enrollment n
190190
inner join tlsa_Person lp on lp.PersonalID = n.PersonalID
191191
where n.AHAR = 1 and cast(lp.RaceEthnicity as nvarchar) like '%5%'
@@ -206,7 +206,7 @@ Populates and references:
206206
where n.AHAR = 1 and lp.Gender not in (98, 99)
207207

208208
insert into tlsa_CountPops (PopID, PersonalID, Step)
209-
select case max(n.ActiveAge)
209+
select distinct case max(n.ActiveAge)
210210
when 0 then 86
211211
when 2 then 87
212212
when 5 then 88
@@ -255,19 +255,19 @@ Populates and references:
255255
*/
256256

257257
update n
258-
set PITOctober = (select case when cd.Cohort is null then 0 else 1 end
258+
set PITOctober = (select distinct case when cd.Cohort is null then 0 else 1 end
259259
from tlsa_CohortDates cd
260260
where (cd.CohortStart < n.ExitDate or n.ExitDate is NULL)
261261
and n.EntryDate <= cd.CohortStart and cd.Cohort = 10)
262-
, PITJanuary = (select case when cd.Cohort is null then 0 else 1 end
262+
, PITJanuary = (select distinct case when cd.Cohort is null then 0 else 1 end
263263
from tlsa_CohortDates cd
264264
where (cd.CohortStart < n.ExitDate or n.ExitDate is NULL)
265265
and n.EntryDate <= cd.CohortStart and cd.Cohort = 11)
266-
, PITApril = (select case when cd.Cohort is null then 0 else 1 end
266+
, PITApril = (select distinct case when cd.Cohort is null then 0 else 1 end
267267
from tlsa_CohortDates cd
268268
where (cd.CohortStart < n.ExitDate or n.ExitDate is NULL)
269269
and n.EntryDate <= cd.CohortStart and cd.Cohort = 12)
270-
, PITJuly = (select case when cd.Cohort is null then 0 else 1 end
270+
, PITJuly = (select distinct case when cd.Cohort is null then 0 else 1 end
271271
from tlsa_CohortDates cd
272272
where (cd.CohortStart < n.ExitDate or n.ExitDate is NULL)
273273
and n.EntryDate <= cd.CohortStart and cd.Cohort = 13)
@@ -283,7 +283,7 @@ Populates and references:
283283
, n.PITJuly = PIT.PITJuly
284284
, n.Step = '9.2.2'
285285
from tlsa_Enrollment n
286-
inner join (select nbn.EnrollmentID
286+
inner join (select distinct nbn.EnrollmentID
287287
, PITOctober = max(case when cd1.Cohort is null then 0 else 1 end)
288288
, PITJanuary = max(case when cd2.Cohort is null then 0 else 1 end)
289289
, PITApril = max(case when cd3.Cohort is null then 0 else 1 end)
@@ -303,19 +303,19 @@ Populates and references:
303303
group by nbn.EnrollmentID) PIT on PIT.EnrollmentID = n.EnrollmentID
304304

305305
update n
306-
set PITOctober = (select case when cd.Cohort is null then 0 else 1 end
306+
set PITOctober = (select distinct case when cd.Cohort is null then 0 else 1 end
307307
from tlsa_CohortDates cd
308308
where (cd.CohortStart < n.ExitDate or n.ExitDate is NULL)
309309
and n.MoveInDate <= cd.CohortStart and cd.Cohort = 10)
310-
, PITJanuary = (select case when cd.Cohort is null then 0 else 1 end
310+
, PITJanuary = (select distinct case when cd.Cohort is null then 0 else 1 end
311311
from tlsa_CohortDates cd
312312
where (cd.CohortStart < n.ExitDate or n.ExitDate is NULL)
313313
and n.MoveInDate <= cd.CohortStart and cd.Cohort = 11)
314-
, PITApril = (select case when cd.Cohort is null then 0 else 1 end
314+
, PITApril = (select distinct case when cd.Cohort is null then 0 else 1 end
315315
from tlsa_CohortDates cd
316316
where (cd.CohortStart < n.ExitDate or n.ExitDate is NULL)
317317
and n.MoveInDate <= cd.CohortStart and cd.Cohort = 12)
318-
, PITJuly = (select case when cd.Cohort is null then 0 else 1 end
318+
, PITJuly = (select distinct case when cd.Cohort is null then 0 else 1 end
319319
from tlsa_CohortDates cd
320320
where (cd.CohortStart < n.ExitDate or n.ExitDate is NULL)
321321
and n.MoveInDate <= cd.CohortStart and cd.Cohort = 13)
@@ -337,7 +337,7 @@ Populates and references:
337337
else count(distinct hhid.HoHID + cast(hhid.ActiveHHType as varchar)) end
338338
, rv.Cohort, rv.Universe, ph.HHType, rp.PopID, rv.SystemPath
339339
, case when rv.Universe = 10 then hhid.ProjectID else null end
340-
, rv.RowID, (select ReportID from lsa_Report), '9.3.1'
340+
, rv.RowID, (select distinct ReportID from lsa_Report), '9.3.1'
341341
from ref_RowValues rv
342342
inner join ref_RowPopulations rp on rv.RowID between rp.RowMin and rp.RowMax
343343
inner join ref_PopHHTypes ph on ph.PopID = rp.PopID
@@ -375,7 +375,7 @@ Populates and references:
375375
select distinct count(distinct n.PersonalID)
376376
, rv.Cohort, rv.Universe, ph.HHType, rp.PopID, rv.SystemPath
377377
, case when rv.Universe = 10 then hhid.ProjectID else null end
378-
, rv.RowID, (select ReportID from lsa_Report), '9.4.1'
378+
, rv.RowID, (select distinct ReportID from lsa_Report), '9.4.1'
379379
from ref_RowValues rv
380380
inner join ref_RowPopulations rp on rv.RowID between rp.RowMin and rp.RowMax
381381
inner join ref_PopHHTypes ph on ph.PopID = rp.PopID
@@ -423,10 +423,10 @@ Populates and references:
423423

424424

425425
insert into lsa_Calculated (Value, Cohort, Universe, HHType, Population, SystemPath, ProjectID, ReportRow, ReportID, Step)
426-
select count(distinct n.PersonalID + cast(bn.DateProvided as varchar)), 1, 10, ph.HHType, pop.PopID, -1
426+
select distinct count(distinct n.PersonalID + cast(bn.DateProvided as varchar)), 1, 10, ph.HHType, pop.PopID, -1
427427
, hhid.ProjectID
428428
, case when pop.PopID in (0,10,11) then 56 else 57 end
429-
, (select ReportID from lsa_Report), '9.5.1'
429+
, (select distinct ReportID from lsa_Report), '9.5.1'
430430
from hmis_Services bn
431431
inner join tlsa_Enrollment n on n.EnrollmentID = bn.EnrollmentID
432432
and (n.ExitDate is null or n.ExitDate > bn.DateProvided)
@@ -444,7 +444,7 @@ Populates and references:
444444
insert into lsa_Calculated
445445
(Value, Cohort, Universe, HHType
446446
, Population, SystemPath, ReportRow, ProjectID, ReportID, Step)
447-
select count (distinct n.PersonalID + cast(cal.theDate as nvarchar))
447+
select distinct count (distinct n.PersonalID + cast(cal.theDate as nvarchar))
448448
, 1, 10, ph.HHType
449449
, pop.PopID, -1
450450
, case when pop.PopID in (0,10,11) then 56 else 57 end
@@ -470,9 +470,9 @@ Populates and references:
470470

471471
-- All ES (Universe 11)
472472
insert into lsa_Calculated (Value, Cohort, Universe, HHType, Population, SystemPath, ProjectID, ReportRow, ReportID, Step)
473-
select count(distinct es.bn), 1, 11, es.HHType, es.PopID, -1, NULL
473+
select distinct count(distinct es.bn), 1, 11, es.HHType, es.PopID, -1, NULL
474474
, case when es.PopID in (0,10,11) then 56 else 57 end
475-
, (select ReportID from lsa_Report), '9.5.3'
475+
, (select distinct ReportID from lsa_Report), '9.5.3'
476476
from
477477
(select distinct n.PersonalID + cast(bn.DateProvided as varchar) as bn, ph.HHType as HHType, pop.PopID
478478
from hmis_Services bn
@@ -506,7 +506,7 @@ Populates and references:
506506
insert into lsa_Calculated
507507
(Value, Cohort, Universe, HHType
508508
, Population, SystemPath, ReportRow, ReportID, Step)
509-
select count (distinct n.PersonalID + cast(cal.theDate as nvarchar))
509+
select distinct count (distinct n.PersonalID + cast(cal.theDate as nvarchar))
510510
, 1, case n.LSAProjectType
511511
when 8 then 12
512512
when 2 then 13
@@ -538,9 +538,9 @@ Populates and references:
538538

539539
-- Unduplicated ES/SH/TH (Universe 16)
540540
insert into lsa_Calculated (Value, Cohort, Universe, HHType, Population, SystemPath, ProjectID, ReportRow, ReportID, Step)
541-
select count(distinct est.bn), 1, 16, est.HHType, est.PopID, -1, NULL
541+
select distinct count(distinct est.bn), 1, 16, est.HHType, est.PopID, -1, NULL
542542
, case when est.PopID in (0,10,11) then 56 else 57 end
543-
, (select ReportID from lsa_Report), '9.5.5'
543+
, (select distinct ReportID from lsa_Report), '9.5.5'
544544
from
545545
(select distinct n.PersonalID + cast(bn.DateProvided as varchar) as bn, ph.HHType as HHType, pop.PopID
546546
from hmis_Services bn

0 commit comments

Comments
 (0)