Skip to content

Commit d45a82b

Browse files
Minor tweaks to temp tables
Unique names for each temp table so it is possible to use INSERT INTO when script is in a stored procedure.
1 parent 5f55a14 commit d45a82b

File tree

1 file changed

+32
-43
lines changed

1 file changed

+32
-43
lines changed

LSASampleCode.sql

Lines changed: 32 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,6 @@ Revision history is maintained in github: https://github.com/HMIS/LSASampleCode/
2828
2929
4.1 Create Intermediate Tables
3030
**********************************************************************/
31-
SET ANSI_NULLS ON
32-
GO
33-
SET QUOTED_IDENTIFIER ON
34-
GO
35-
3631
--11/29/2018 hmis_tables in the sample db all have primary keys identified /
3732
-- clustered indexes on their respective primary keys.
3833
-- The indexes created by the next 5 statements on hmis_tables in the
@@ -2254,7 +2249,7 @@ select hhid.HouseholdID
22542249
when sum(hhid.AgeStatus%10) > 0 then 3
22552250
when sum((hhid.AgeStatus/10)%100) > 0 then 1
22562251
else 99 end as HHType
2257-
into #hh
2252+
into #hhstat
22582253
from
22592254
--get AgeStatus for household members on previous enrollment
22602255
(select distinct hn.HouseholdID
@@ -2297,8 +2292,8 @@ from
22972292
) hhid
22982293
group by hhid.HouseholdID
22992294

2300-
CREATE NONCLUSTERED INDEX ix_hh_household_id ON #hh (HouseholdID);
2301-
CREATE NONCLUSTERED INDEX ix_hh_hhtype ON #hh (HHType);
2295+
CREATE NONCLUSTERED INDEX ix_hhstat_household_id ON #hhstat (HouseholdID);
2296+
CREATE NONCLUSTERED INDEX ix_hhstat_hhtype ON #hhstat (HHType);
23022297

23032298
update lhh
23042299
set lhh.StatEnrollmentID =
@@ -2312,14 +2307,13 @@ set lhh.StatEnrollmentID =
23122307
or (hx.ExitDate = prior.EntryDate and prior.MoveInDate is null and p.ProjectType in (3,13)))
23132308
and hx.ExitDate between dateadd(dd,-730,lhh.FirstEntry) and lhh.FirstEntry
23142309
inner join --Get enrollments for the same HoH and HHType prior to FirstEntry
2315-
#hh on #hh.HouseholdID = prior.HouseholdID
2310+
#hhstat on #hhstat.HouseholdID = prior.HouseholdID
23162311
where prior.PersonalID = lhh.HoHID and prior.RelationshipToHoH = 1
2317-
and #hh.HHType = lhh.HHType
2312+
and #hhstat.HHType = lhh.HHType
23182313
order by hx.ExitDate desc)
23192314
from tmp_Household lhh
23202315

2321-
drop table #hh
2322-
GO
2316+
drop table #hhstat
23232317

23242318
/*************************************************************************
23252319
4.29.c Set System Engagement Status for tmp_Household
@@ -2402,7 +2396,7 @@ when sum(hhid.AgeStatus%10) > 0
24022396
when sum((hhid.AgeStatus/10)%100) > 0
24032397
then 1
24042398
else 99 end as HHType
2405-
into #hh
2399+
into #hhsys
24062400
from (select distinct hn.HouseholdID
24072401
, case when c.DOBDataQuality in (8,9)
24082402
or c.DOB is null
@@ -2427,8 +2421,8 @@ from (select distinct hn.HouseholdID
24272421
) hhid
24282422
group by hhid.HouseholdID
24292423

2430-
CREATE NONCLUSTERED INDEX ix_household_id_2 ON #hh (HouseholdID);
2431-
CREATE NONCLUSTERED INDEX ix_hhtype_2 ON #hh (HHType);
2424+
CREATE NONCLUSTERED INDEX ix_hhsys_HouseholdID ON #hhsys (HouseholdID);
2425+
CREATE NONCLUSTERED INDEX ix_hhsys_HHType ON #hhsys (HHType);
24322426

24332427
delete from sys_Enrollment
24342428

@@ -2440,7 +2434,7 @@ insert into sys_Enrollment (HoHID, HHType, EnrollmentID, ProjectType
24402434
select distinct hn.PersonalID
24412435
-- CHANGE 10/23/2018 for active enrollments, use HHType as already calculated;
24422436
-- otherwise, use HHType based on HH member age(s) at project entry.
2443-
, case when an.EnrollmentID is not null then an.HHType else #hh.HHType end
2437+
, case when an.EnrollmentID is not null then an.HHType else #hhsys.HHType end
24442438
, hn.EnrollmentID, p.ProjectType
24452439
, case when p.TrackingMethod = 3 then null else hn.EntryDate end
24462440
, case when p.ProjectType in (3,13) then hn.MoveInDate else null end
@@ -2454,16 +2448,16 @@ left outer join active_Enrollment an on an.EnrollmentID = hn.EnrollmentID
24542448
left outer join hmis_Exit hx on hx.EnrollmentID = hn.EnrollmentID
24552449
and hx.ExitDate <= rpt.ReportEnd
24562450
inner join hmis_Project p on p.ProjectID = hn.ProjectID
2457-
inner join #hh on #hh.HouseholdID = hn.HouseholdID
2451+
inner join #hhsys on #hhsys.HouseholdID = hn.HouseholdID
24582452
where
24592453
an.EnrollmentID is not null --All active enrollments are relevant.
24602454
or (hx.ExitDate >= '10/1/2012'-- Inactive enrollments potentially relevant...
2461-
and #hh.HHType = lhh.HHType -- if they occurred under the same HHType
2455+
and #hhsys.HHType = lhh.HHType -- if they occurred under the same HHType
24622456
and lhh.Stat = 5 --... and HH was 'continously engaged' at ReportStart...
24632457
and lhh.PSHMoveIn <> 2) --...and HH was not housed in PSH at ReportStart.
24642458

2465-
drop table #hh
2466-
GO
2459+
drop table #hhsys
2460+
24672461
/*****************************************************************
24682462
4.33 Get Last Inactive Date
24692463
*****************************************************************/
@@ -2509,7 +2503,6 @@ group by lhh.HoHID, lhh.HHType
25092503
) lastDay on lastDay.HoHID = lhh.HoHID and lastDay.HHType = lhh.HHType
25102504

25112505
drop table #padded
2512-
GO
25132506

25142507
/*****************************************************************
25152508
4.34 Get Dates of Other System Use
@@ -2782,7 +2775,7 @@ select hhid.HouseholdID, case
27822775
when sum(hhid.AgeStatus%10) > 0 then 3
27832776
when sum((hhid.AgeStatus/10)%100) > 0 then 1
27842777
else 99 end as HHType
2785-
into #hh
2778+
into #hhx
27862779
from (--hhid identifies age status (adult/child/unknown) for
27872780
--members of households with exits in subquery hoh
27882781
select distinct hn.HouseholdID
@@ -2847,7 +2840,7 @@ select hhid.HouseholdID, case
28472840
) hhid
28482841
group by hhid.HouseholdID
28492842

2850-
CREATE NONCLUSTERED INDEX ix_household_id ON #hh (HouseholdID);
2843+
CREATE NONCLUSTERED INDEX ix_hhx_HouseholdID ON #hhx (HouseholdID);
28512844

28522845
select hn.PersonalID as HoHID, hh.HHType, hn.EntryDate, hx.ExitDate
28532846
into #b
@@ -2910,7 +2903,7 @@ delete from ex_Enrollment
29102903

29112904
insert into ex_Enrollment (Cohort, HoHID, HHType, EnrollmentID, ProjectType
29122905
, EntryDate, MoveInDate, ExitDate, ExitTo)
2913-
select distinct cd.Cohort, hn.PersonalID, #hh.HHType, hn.EnrollmentID, p.ProjectType
2906+
select distinct cd.Cohort, hn.PersonalID, #hhx.HHType, hn.EnrollmentID, p.ProjectType
29142907
, hn.EntryDate, hn.MoveInDate, hx.ExitDate
29152908
, case when hx.Destination = 3 then 1 --PSH
29162909
when hx.Destination = 31 then 2 --PH - rent/temp subsidy
@@ -2943,13 +2936,13 @@ inner join tmp_CohortDates cd on cd.CohortStart <= hx.ExitDate
29432936
inner join lsa_Report rpt on rpt.ReportEnd >= cd.CohortEnd
29442937
inner join
29452938
--hh identifies household exits by HHType from relevant projects
2946-
#hh on #hh.HouseholdID = hn.HouseholdID
2939+
#hhx on #hhx.HouseholdID = hn.HouseholdID
29472940
left outer join
29482941
--Subquery b identifies household enrollments by HHType in ES/SH/TH/RRH/PSH
29492942
-- projects active in the cohort period; if these include any activity in the
29502943
-- within 15 days of an exit identified in the hh subquery, the hh exit is
29512944
-- excluded in the WHERE clause.
2952-
#b on #b.HoHID = hn.PersonalID and #b.HHType = #hh.HHType
2945+
#b on #b.HoHID = hn.PersonalID and #b.HHType = #hhx.HHType
29532946
and #b.EntryDate < dateadd(dd, 15, hx.ExitDate)
29542947
and (#b.ExitDate is NULL or #b.ExitDate > hx.ExitDate)
29552948
--If there is at least one exit followed by 15 days of inactivity during a cohort period,
@@ -2963,10 +2956,8 @@ where hn.RelationshipToHoH = 1 and #b.HoHID is null and cd.Cohort <= 0
29632956
where mostrecent.EnrollmentID = hn.EnrollmentID
29642957
order by mostrecent.InformationDate desc)
29652958

2966-
drop table #hh
2967-
GO
2959+
drop table #hhx
29682960
drop table #b
2969-
GO
29702961

29712962
/*****************************************************************
29722963
4.41 Get EnrollmentIDs for Exit Cohort Households
@@ -3016,7 +3007,7 @@ when sum(hhid.AgeStatus/100) > 0 then 99
30163007
when sum(hhid.AgeStatus%10) > 0 then 3
30173008
when sum((hhid.AgeStatus/10)%100) > 0 then 1
30183009
else 99 end as HHType
3019-
into #hh
3010+
into #hhreturn
30203011
from (select distinct hn.HouseholdID
30213012
, case when c.DOBDataQuality in (8,9)
30223013
or c.DOB is null
@@ -3060,21 +3051,20 @@ from (select distinct hn.HouseholdID
30603051
) hhid
30613052
group by hhid.HouseholdID
30623053

3063-
CREATE NONCLUSTERED INDEX ix_household_id ON #hh (HouseholdID);
3064-
CREATE NONCLUSTERED INDEX ix_hhtype ON #hh (HHType);
3054+
CREATE NONCLUSTERED INDEX ix_hhreturn_HouseholdID ON #hhreturn (HouseholdID);
3055+
CREATE NONCLUSTERED INDEX ix_hhreturn_HHType ON #hhreturn (HHType);
30653056

30663057
update ex
30673058
set ex.ReturnDate = (select min(hn.EntryDate)
30683059
from hmis_Enrollment hn
3069-
inner join #hh on #hh.HouseholdID = hn.HouseholdID
3060+
inner join #hhreturn on #hhreturn.HouseholdID = hn.HouseholdID
30703061
where hn.RelationshipToHoH = 1
3071-
and hn.PersonalID = ex.HoHID and #hh.HHType = ex.HHType
3062+
and hn.PersonalID = ex.HoHID and #hhreturn.HHType = ex.HHType
30723063
and hn.EntryDate
30733064
between dateadd(dd, 15, ex.ExitDate) and dateadd(dd, 730, ex.ExitDate))
30743065
from tmp_Exit ex
30753066

3076-
drop table #hh
3077-
GO
3067+
drop table #hhreturn
30783068

30793069
update ex
30803070
set ex.ReturnTime =
@@ -3235,7 +3225,7 @@ select hhid.HouseholdID, case
32353225
when sum(hhid.AgeStatus%10) > 0 then 3
32363226
when sum((hhid.AgeStatus/10)%100) > 0 then 1
32373227
else 99 end as HHType
3238-
into #hh
3228+
into #hhxstat
32393229
from
32403230
--HouseholdIDs with age status for household members
32413231
(select distinct hn.HouseholdID
@@ -3277,8 +3267,8 @@ from
32773267
) hhid
32783268
group by hhid.HouseholdID
32793269

3280-
CREATE NONCLUSTERED INDEX ix_household_id ON #hh (HouseholdID);
3281-
CREATE NONCLUSTERED INDEX ix_hhtype ON #hh (HHType);
3270+
CREATE NONCLUSTERED INDEX ix_hhxstat_HouseholdID ON #hhxstat (HouseholdID);
3271+
CREATE NONCLUSTERED INDEX ix_hhxstat_HHType ON #hhxstat (HHType);
32823272

32833273
update ex
32843274
set ex.StatEnrollmentID = (select top 1 previous.EnrollmentID
@@ -3289,14 +3279,13 @@ set ex.StatEnrollmentID = (select top 1 previous.EnrollmentID
32893279
and hx.ExitDate < ex.ExitDate
32903280
inner join
32913281
--HouseholdIDs with LSA household types
3292-
#hh on #hh.HouseholdID = previous.HouseholdID
3282+
#hhxstat on #hhxstat.HouseholdID = previous.HouseholdID
32933283
where previous.PersonalID = ex.HoHID and previous.RelationshipToHoH = 1
3294-
and #hh.HHType = ex.HHType
3284+
and #hhxstat.HHType = ex.HHType
32953285
order by hx.ExitDate desc)
32963286
from tmp_Exit ex
32973287

3298-
drop table #hh
3299-
GO
3288+
drop table #hhxstat
33003289

33013290
update ex
33023291
set ex.Stat = case when ex.StatEnrollmentID is null then 1

0 commit comments

Comments
 (0)