-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy path06 LSAHousehold.sql
1270 lines (1198 loc) · 51.7 KB
/
06 LSAHousehold.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
LSA FY2024 Sample Code
Name: 06 LSAHousehold.sql
FY2024 Changes
Run code only if the LSAScope is not 'HIC'
(Detailed revision history maintained at https://github.com/HMIS/LSASampleCode)
6.1 Get Unique Households and Population Identifiers for tlsa_Household
*/
if (select LSAScope from lsa_Report) <> 3
begin
truncate table tlsa_Household
insert into tlsa_Household (HoHID, HHType
, HHChronic, HHVet, HHDisability, HHFleeingDV
, HoHRaceEthnicity
, HHParent, ReportID, Step)
select distinct hhid.HoHID, hhid.ActiveHHType
, case when min(case hhid.HHChronic when 0 then 99 else hhid.HHChronic end) = 99 then 0
else min(case hhid.HHChronic when 0 then 99 else hhid.HHChronic end) end
, max(hhid.HHVet)
, max(hhid.HHDisability)
, case when min(case hhid.HHFleeingDV when 0 then 99 else hhid.HHFleeingDV end) = 99 then 0
else min(case hhid.HHFleeingDV when 0 then 99 else hhid.HHFleeingDV end) end
, lp.RaceEthnicity
, max(hhid.HHParent)
, lp.ReportID
, '6.1'
from tlsa_HHID hhid
inner join lsa_Report rpt on rpt.ReportEnd >= hhid.EntryDate
inner join tlsa_Person lp on lp.PersonalID = hhid.HoHID
where hhid.Active = 1
group by hhid.HoHID, hhid.ActiveHHType, lp.RaceEthnicity
, lp.ReportID
/*
6.2 Set Population Identifiers for LSAHousehold
*/
update hh
set HHChild = (select case when count(distinct n.PersonalID) >= 3 then 3
else count(distinct n.PersonalID) end
from tlsa_HHID hhid
inner join tlsa_Enrollment n on n.HouseholdID = hhid.HouseholdID and n.Active = 1
where n.ActiveAge < 18
and hhid.ActiveHHType = hh.HHType and hhid.HoHID = hh.HoHID)
, HHAdult = (select case when count(distinct n.PersonalID) >= 3 then 3
else count(distinct n.PersonalID) end
from tlsa_HHID hhid
inner join tlsa_Enrollment n on n.HouseholdID = hhid.HouseholdID and n.Active = 1
where n.ActiveAge between 18 and 65
and hhid.ActiveHHType = hh.HHType and hhid.HoHID = hh.HoHID
and n.PersonalID not in
(select n17.PersonalID
from tlsa_HHID hh17
inner join tlsa_Enrollment n17 on n17.HouseholdID = hh17.HouseholdID and n17.Active = 1
where hh17.HoHID = hhid.HoHID and hh17.ActiveHHType = hhid.ActiveHHType
and n17.ActiveAge < 18))
, HHNoDOB = (select case when count(distinct n.PersonalID) >= 3 then 3
else count(distinct n.PersonalID) end
from tlsa_HHID hhid
inner join tlsa_Enrollment n on n.HouseholdID = hhid.HouseholdID and n.Active = 1
where n.ActiveAge in (98,99)
and hhid.ActiveHHType = hh.HHType and hhid.HoHID = hh.HoHID)
, hh.Step = '6.2.1'
from tlsa_Household hh
update hh
set hh.HHAdultAge = null, hh.Step = '6.2.2'
from tlsa_Household hh
update hh
set hh.HHAdultAge = hhid.HHAdultAge, hh.Step = '6.2.3'
from tlsa_Household hh
inner join tlsa_HHID hhid
on hhid.HoHID = hh.HoHID and hhid.ActiveHHType = hh.HHType
and hhid.Active = 1
where hhid.HHAdultAge = 18
update hh
set hh.HHAdultAge = hhid.HHAdultAge, hh.Step = '6.2.4'
from tlsa_Household hh
inner join tlsa_HHID hhid
on hhid.HoHID = hh.HoHID and hhid.ActiveHHType = hh.HHType
and hhid.Active = 1
where hhid.HHAdultAge = 24 and hh.HHAdultAge is null
update hh
set hh.HHAdultAge = hhid.HHAdultAge, hh.Step = '6.2.5'
from tlsa_Household hh
inner join tlsa_HHID hhid
on hhid.HoHID = hh.HoHID and hhid.ActiveHHType = hh.HHType
and hhid.Active = 1
where hhid.HHAdultAge = 55 and hh.HHAdultAge is null
update hh
set hh.HHAdultAge = hhid.HHAdultAge, hh.Step = '6.2.6'
from tlsa_Household hh
inner join tlsa_HHID hhid
on hhid.HoHID = hh.HoHID and hhid.ActiveHHType = hh.HHType
and hhid.Active = 1
where hhid.HHAdultAge = 25 and hh.HHAdultAge is null
update hh
set hh.HHAdultAge = -1, hh.Step = '6.2.7'
from tlsa_Household hh
where hh.HHAdultAge is null
/*
6.3 Set tlsa_Household Project Group Status Indicators
*/
update hh
set ESTStatus = case when n.nStat is null then 0
else n.nStat + n.xStat end
, hh.Step = '6.3.1'
from tlsa_Household hh
left outer join
(select hhid.HoHID, hhid.ActiveHHType as HHType
, min(case when hhid.EntryDate < rpt.ReportStart then 10 else 20 end) as nStat
, min(case when hhid.ExitDate is null then 1 else 2 end) as xStat
from tlsa_HHID hhid
inner join lsa_Report rpt on hhid.EntryDate <= rpt.ReportEnd
where hhid.Active = 1 and hhid.LSAProjectType in (0,1,2,8)
group by hhid.HoHID, hhid.ActiveHHType
) n on n.HoHID = hh.HoHID and n.HHType = hh.HHType
update hh
set hh.RRHStatus = case when n.nStat is null then 0
else n.nStat + n.xStat end
, hh.Step = '6.3.2'
from tlsa_Household hh
left outer join
(select hhid.HoHID, hhid.ActiveHHType as HHType
, min(case when hhid.EntryDate < rpt.ReportStart then 10 else 20 end) as nStat
, min(case when hhid.ExitDate is null then 1 else 2 end) as xStat
from tlsa_HHID hhid
inner join lsa_Report rpt on hhid.EntryDate <= rpt.ReportEnd
where hhid.Active = 1 and hhid.LSAProjectType = 13
group by hhid.HoHID, hhid.ActiveHHType
) n on n.HoHID = hh.HoHID and n.HHType = hh.HHType
update hh
set hh.PSHStatus = case when n.nStat is null then 0
else n.nStat + n.xStat end
, hh.Step = '6.3.3'
from tlsa_Household hh
left outer join
(select hhid.HoHID, hhid.ActiveHHType as HHType
, min(case when hhid.EntryDate < rpt.ReportStart then 10 else 20 end) as nStat
, min(case when hhid.ExitDate is null then 1 else 2 end) as xStat
from tlsa_HHID hhid
inner join lsa_Report rpt on hhid.EntryDate <= rpt.ReportEnd
where hhid.Active = 1 and hhid.LSAProjectType = 3
group by hhid.HoHID, hhid.ActiveHHType
) n on n.HoHID = hh.HoHID and n.HHType = hh.HHType
update hh
set hh.RRHSOStatus = case when n.nStat is null then 0
else n.nStat + n.xStat end
, hh.Step = '6.3.4'
from tlsa_Household hh
left outer join
(select hhid.HoHID, hhid.ActiveHHType as HHType
, min(case when hhid.EntryDate < rpt.ReportStart then 10 else 20 end) as nStat
, min(case when hhid.ExitDate is null then 1 else 2 end) as xStat
from tlsa_HHID hhid
inner join lsa_Report rpt on hhid.EntryDate <= rpt.ReportEnd
where hhid.Active = 1 and hhid.LSAProjectType = 15
group by hhid.HoHID, hhid.ActiveHHType
) n on n.HoHID = hh.HoHID and n.HHType = hh.HHType
/*
6.4 Set tlsa_Household RRH and PSH Move-In Status Indicators
*/
update hh
set hh.RRHMoveIn = case when hh.RRHStatus = 0 then -1
when n.MoveInStat is null then 0
else n.MoveInStat end
, hh.Step = '6.4.1'
from tlsa_Household hh
left outer join
(select hhid.HoHID, hhid.ActiveHHType as HHType
, min(case when hhid.MoveInDate >= rpt.ReportStart then 1 else 2 end) as MoveInStat
from tlsa_HHID hhid
inner join lsa_Report rpt on hhid.EntryDate <= rpt.ReportEnd
where hhid.Active = 1 and hhid.MoveInDate is not null and hhid.LSAProjectType = 13
group by hhid.HoHID, hhid.ActiveHHType
) n on n.HoHID = hh.HoHID and n.HHType = hh.HHType
update hh
set hh.PSHMoveIn = case when hh.PSHStatus = 0 then -1
when n.MoveInStat is null then 0
else n.MoveInStat end
, hh.Step = '6.4.2'
from tlsa_Household hh
left outer join
(select hhid.HoHID, hhid.ActiveHHType as HHType
, min(case when hhid.MoveInDate >= rpt.ReportStart then 1 else 2 end) as MoveInStat
from tlsa_HHID hhid
inner join lsa_Report rpt on hhid.EntryDate <= rpt.ReportEnd
where hhid.Active = 1 and hhid.MoveInDate is not null and hhid.LSAProjectType = 3
group by hhid.HoHID, hhid.ActiveHHType
) n on n.HoHID = hh.HoHID and n.HHType = hh.HHType
update hh
set hh.RRHSOMoveIn = case when hh.RRHSOStatus = 0 then -1
when n.MoveInStat is null then 0
else n.MoveInStat end
, hh.Step = '6.4.3'
from tlsa_Household hh
left outer join
(select hhid.HoHID, hhid.ActiveHHType as HHType
, min(case when hhid.MoveInDate >= rpt.ReportStart then 1 else 2 end) as MoveInStat
from tlsa_HHID hhid
inner join lsa_Report rpt on hhid.EntryDate <= rpt.ReportEnd
where hhid.Active = 1 and hhid.MoveInDate is not null and hhid.LSAProjectType = 15
group by hhid.HoHID, hhid.ActiveHHType
) n on n.HoHID = hh.HoHID and n.HHType = hh.HHType
/*
6.5 Set tlsa_Household Geography for Each Project Group
-- Enrollment with latest active date in report period for project group
*/
update hh
set hh.ESTGeography = case when hh.ESTStatus = 0 then -1
else coalesce(
(select top 1 coc.GeographyType
from tlsa_HHID hhid
inner join lsa_ProjectCoC coc on coc.ProjectID = hhid.ProjectID
where hhid.Active = 1 and hhid.LSAProjectType in (0,1,2,8)
and hhid.HoHID = hh.HoHID and hhid.ActiveHHType = hh.HHType
order by coalesce(hhid.ExitDate, '9999-9-9') desc, hhid.EntryDate desc)
, 99) end
,hh.RRHGeography = case when hh.RRHStatus = 0 then -1
else coalesce(
(select top 1 coc.GeographyType
from tlsa_HHID hhid
inner join lsa_ProjectCoC coc on coc.ProjectID = hhid.ProjectID
where hhid.Active = 1 and hhid.LSAProjectType = 13
and hhid.HoHID = hh.HoHID and hhid.ActiveHHType = hh.HHType
order by coalesce(hhid.ExitDate, '9999-9-9') desc, hhid.EntryDate desc)
, 99) end
, hh.PSHGeography = case when hh.PSHStatus = 0 then -1
else coalesce(
(select top 1 coc.GeographyType
from tlsa_HHID hhid
inner join lsa_ProjectCoC coc on coc.ProjectID = hhid.ProjectID
where hhid.Active = 1 and hhid.LSAProjectType = 3
and hhid.HoHID = hh.HoHID and hhid.ActiveHHType = hh.HHType
order by coalesce(hhid.ExitDate, '9999-9-9') desc, hhid.EntryDate desc)
, 99) end
, hh.Step = '6.5.1'
from tlsa_Household hh
/*
6.6 Set tlsa_Household Living Situation for Each Project Group
--earliest active enrollment in project group
*/
update hh
set hh.ESTLivingSit =
case when hh.ESTStatus = 0 then -1
when hn.EntryDate <> n.EntryDate
or hn.LivingSituation is null
or (hn.LivingSituation = 435 and hn.RentalSubsidyType is null)
then 99
when hn.LivingSituation in (8,9) then 98
when hn.LivingSituation = 435 then hn.RentalSubsidyType
else hn.LivingSituation end
, hh.Step = '6.6.1'
from tlsa_Household hh
inner join hmis_Enrollment hn on hn.PersonalID = hh.HoHID
inner join tlsa_Enrollment n on n.EnrollmentID = hn.EnrollmentID
where hh.ESTStatus = 0
or hn.EnrollmentID in
(select top 1 hhid.EnrollmentID
from tlsa_HHID hhid
where hhid.LSAProjectType in (0,1,2,8)
and hhid.HoHID = hh.HoHID and hhid.ActiveHHType = hh.HHType
and hhid.Active = 1
order by hhid.EntryDate asc)
update hh
set hh.RRHLivingSit =
case when hh.RRHStatus = 0 then -1
when hn.EntryDate <> n.EntryDate
or hn.LivingSituation is null
or (hn.LivingSituation = 435 and hn.RentalSubsidyType is null)
then 99
when hn.LivingSituation in (8,9) then 98
when hn.LivingSituation = 435 then hn.RentalSubsidyType
else hn.LivingSituation end
, hh.Step = '6.6.2'
from tlsa_Household hh
inner join hmis_Enrollment hn on hn.PersonalID = hh.HoHID
inner join tlsa_Enrollment n on n.EnrollmentID = hn.EnrollmentID
where hh.RRHStatus = 0
or hn.EnrollmentID in
(select top 1 hhid.EnrollmentID
from tlsa_HHID hhid
where hhid.LSAProjectType = 13
and hhid.HoHID = hh.HoHID and hhid.ActiveHHType = hh.HHType
and hhid.Active = 1
order by hhid.EntryDate asc)
update hh
set hh.PSHLivingSit =
case when hh.PSHStatus = 0 then -1
when hn.EntryDate <> n.EntryDate
or hn.LivingSituation is null
or (hn.LivingSituation = 435 and hn.RentalSubsidyType is null)
then 99
when hn.LivingSituation in (8,9) then 98
when hn.LivingSituation = 435 then hn.RentalSubsidyType
else hn.LivingSituation end
, hh.Step = '6.6.3'
from tlsa_Household hh
inner join hmis_Enrollment hn on hn.PersonalID = hh.HoHID
inner join tlsa_Enrollment n on n.EnrollmentID = hn.EnrollmentID
where hh.PSHStatus = 0
or hn.EnrollmentID in
(select top 1 hhid.EnrollmentID
from tlsa_HHID hhid
where hhid.LSAProjectType = 3
and hhid.HoHID = hh.HoHID and hhid.ActiveHHType = hh.HHType
and hhid.Active = 1
order by hhid.EntryDate asc)
/*
6.7 Set tlsa_Household Destination for Each Project Group
--most recent exit from project group for households not active in project group at ReportEnd
*/
update hh
set ESTDestination =
case when hh.ESTStatus not in (12,22) then -1
else hhid.ExitDest end
, hh.Step = '6.7.1'
from tlsa_Household hh
inner join tlsa_HHID hhid on hhid.HoHID = hh.HoHID
and hhid.ActiveHHType = hh.HHType and hhid.Active = 1
where hh.ESTStatus not in (12,22)
or hhid.EnrollmentID in
(select top 1 hhid.EnrollmentID
from tlsa_HHID hhid
where hhid.LSAProjectType in (0,1,2,8)
and hhid.HoHID = hh.HoHID and hhid.ActiveHHType = hh.HHType
and hhid.Active = 1
order by hhid.ExitDate desc)
update hh
set RRHDestination =
case when hh.RRHStatus not in (12,22) then -1
else hhid.ExitDest end
, hh.Step = '6.7.2'
from tlsa_Household hh
inner join tlsa_HHID hhid on hhid.HoHID = hh.HoHID
and hhid.ActiveHHType = hh.HHType and hhid.Active = 1
where hh.RRHStatus not in (12,22)
or hhid.EnrollmentID in
(select top 1 hhid.EnrollmentID
from tlsa_HHID hhid
where hhid.LSAProjectType = 13
and hhid.HoHID = hh.HoHID and hhid.ActiveHHType = hh.HHType
and hhid.Active = 1
order by hhid.ExitDate desc)
update hh
set PSHDestination =
case when hh.PSHStatus not in (12,22) then -1
else hhid.ExitDest end
, hh.Step = '6.7.3'
from tlsa_Household hh
inner join tlsa_HHID hhid on hhid.HoHID = hh.HoHID
and hhid.ActiveHHType = hh.HHType and hhid.Active = 1
where hh.PSHStatus not in (12,22)
or hhid.EnrollmentID in
(select top 1 hhid.EnrollmentID
from tlsa_HHID hhid
where hhid.LSAProjectType = 3
and hhid.HoHID = hh.HoHID and hhid.ActiveHHType = hh.HHType
and hhid.Active = 1
order by hhid.ExitDate desc)
/*
6.8 EST/RRH/PSH Population Identifiers for LSAHousehold
*/
update hh
set ESTAC3Plus = coalesce (
(select max(hhid.AC3Plus)
from tlsa_HHID hhid
where hhid.Active = 1 and hhid.ActiveHHType = hh.HHType and hhid.HoHID = hh.HoHID
and hhid.LSAProjectType in (0,1,2,8)), 0)
, ESTVet = coalesce (
(select max(hhid.HHVet)
from tlsa_HHID hhid
where hhid.Active = 1 and hhid.ActiveHHType = hh.HHType and hhid.HoHID = hh.HoHID
and hhid.LSAProjectType in (0,1,2,8)), 0)
, ESTChronic = coalesce (
(select min(hhid.HHChronic)
from tlsa_HHID hhid
where hhid.Active = 1 and hhid.ActiveHHType = hh.HHType and hhid.HoHID = hh.HoHID
and hhid.HHChronic = 1 and hhid.LSAProjectType in (0,1,2,8)), 0)
, ESTDisability = coalesce (
(select max(hhid.HHDisability)
from tlsa_HHID hhid
where hhid.Active = 1 and hhid.ActiveHHType = hh.HHType and hhid.HoHID = hh.HoHID
and hhid.LSAProjectType in (0,1,2,8)), 0)
, ESTFleeingDV = coalesce (
(select min(hhid.HHFleeingDV)
from tlsa_HHID hhid
where hhid.Active = 1 and hhid.ActiveHHType = hh.HHType and hhid.HoHID = hh.HoHID
and hhid.LSAProjectType in (0,1,2,8) and hhid.HHFleeingDV > 0), 0)
, ESTParent = coalesce (
(select max(hhid.HHParent)
from tlsa_HHID hhid
where hhid.Active = 1 and hhid.ActiveHHType = hh.HHType and hhid.HoHID = hh.HoHID
and hhid.LSAProjectType in (0,1,2,8)), 0)
, ESTAdultAge = coalesce (hh18.HHAdultAge,
hh24.HHAdultAge,
hh55.HHAdultAge,
hh25.HHAdultAge,
-1)
, hh.Step = '6.8.1'
from tlsa_Household hh
left outer join tlsa_HHID hh18 on hh18.HHAdultAge = 18
and hh18.HoHID = hh.HoHID and hh18.ActiveHHType = hh.HHType
and hh18.Active = 1 and hh18.LSAProjectType in (0,1,2,8)
left outer join tlsa_HHID hh24 on hh24.HHAdultAge = 24
and hh24.HoHID = hh.HoHID and hh24.ActiveHHType = hh.HHType
and hh24.Active = 1 and hh24.LSAProjectType in (0,1,2,8)
left outer join tlsa_HHID hh55 on hh55.HHAdultAge = 55
and hh55.HoHID = hh.HoHID and hh55.ActiveHHType = hh.HHType
and hh55.Active = 1 and hh55.LSAProjectType in (0,1,2,8)
left outer join tlsa_HHID hh25 on hh25.HHAdultAge = 25
and hh25.Active = 1 and hh25.HoHID = hh.HoHID and hh25.ActiveHHType = hh.HHType
and hh25.LSAProjectType in (0,1,2,8)
update hh
set RRHAC3Plus = coalesce (
(select max(hhid.AC3Plus)
from tlsa_HHID hhid
where hhid.Active = 1 and hhid.ActiveHHType = hh.HHType and hhid.HoHID = hh.HoHID
and hhid.LSAProjectType = 13), 0)
, RRHVet = coalesce (
(select max(hhid.HHVet)
from tlsa_HHID hhid
where hhid.Active = 1 and hhid.ActiveHHType = hh.HHType and hhid.HoHID = hh.HoHID
and hhid.LSAProjectType = 13), 0)
, RRHChronic = coalesce (
(select min(hhid.HHChronic)
from tlsa_HHID hhid
where hhid.Active = 1 and hhid.ActiveHHType = hh.HHType and hhid.HoHID = hh.HoHID
and hhid.HHChronic = 1 and hhid.LSAProjectType = 13), 0)
, RRHDisability = coalesce (
(select max(hhid.HHDisability)
from tlsa_HHID hhid
where hhid.Active = 1 and hhid.ActiveHHType = hh.HHType and hhid.HoHID = hh.HoHID
and hhid.LSAProjectType = 13), 0)
, RRHFleeingDV = coalesce (
(select min(hhid.HHFleeingDV)
from tlsa_HHID hhid
where hhid.Active = 1 and hhid.ActiveHHType = hh.HHType and hhid.HoHID = hh.HoHID
and hhid.LSAProjectType = 13 and hhid.HHFleeingDV > 0), 0)
, RRHParent = coalesce (
(select max(hhid.HHParent)
from tlsa_HHID hhid
where hhid.Active = 1 and hhid.ActiveHHType = hh.HHType and hhid.HoHID = hh.HoHID
and hhid.LSAProjectType = 13), 0)
, RRHAdultAge = coalesce (hh18.HHAdultAge,
hh24.HHAdultAge,
hh55.HHAdultAge,
hh25.HHAdultAge,
-1)
, hh.Step = '6.8.2'
from tlsa_Household hh
left outer join tlsa_HHID hh18 on hh18.HHAdultAge = 18
and hh18.HoHID = hh.HoHID and hh18.ActiveHHType = hh.HHType
and hh18.Active = 1 and hh18.LSAProjectType = 13
left outer join tlsa_HHID hh24 on hh24.HHAdultAge = 24
and hh24.HoHID = hh.HoHID and hh24.ActiveHHType = hh.HHType
and hh24.Active = 1 and hh24.LSAProjectType = 13
left outer join tlsa_HHID hh55 on hh55.HHAdultAge = 55
and hh55.HoHID = hh.HoHID and hh55.ActiveHHType = hh.HHType
and hh55.Active = 1 and hh55.LSAProjectType = 13
left outer join tlsa_HHID hh25 on hh25.HHAdultAge = 25
and hh25.Active = 1 and hh25.HoHID = hh.HoHID and hh25.ActiveHHType = hh.HHType
and hh25.LSAProjectType = 13
update hh
set PSHAC3Plus = coalesce (
(select max(hhid.AC3Plus)
from tlsa_HHID hhid
where hhid.Active = 1 and hhid.ActiveHHType = hh.HHType and hhid.HoHID = hh.HoHID
and hhid.LSAProjectType = 3), 0)
, PSHVet = coalesce (
(select max(hhid.HHVet)
from tlsa_HHID hhid
where hhid.Active = 1 and hhid.ActiveHHType = hh.HHType and hhid.HoHID = hh.HoHID
and hhid.LSAProjectType = 3), 0)
, PSHChronic = coalesce (
(select min(hhid.HHChronic)
from tlsa_HHID hhid
where hhid.Active = 1 and hhid.ActiveHHType = hh.HHType and hhid.HoHID = hh.HoHID
and hhid.HHChronic = 1 and hhid.LSAProjectType = 3), 0)
, PSHDisability = coalesce (
(select max(hhid.HHDisability)
from tlsa_HHID hhid
where hhid.Active = 1 and hhid.ActiveHHType = hh.HHType and hhid.HoHID = hh.HoHID
and hhid.LSAProjectType = 3), 0)
, PSHFleeingDV = coalesce (
(select min(hhid.HHFleeingDV)
from tlsa_HHID hhid
where hhid.Active = 1 and hhid.ActiveHHType = hh.HHType and hhid.HoHID = hh.HoHID
and hhid.LSAProjectType = 3 and hhid.HHFleeingDV > 0), 0)
, PSHParent = coalesce (
(select max(hhid.HHParent)
from tlsa_HHID hhid
where hhid.Active = 1 and hhid.ActiveHHType = hh.HHType and hhid.HoHID = hh.HoHID
and hhid.LSAProjectType = 3), 0)
, PSHAdultAge = coalesce (hh18.HHAdultAge,
hh24.HHAdultAge,
hh55.HHAdultAge,
hh25.HHAdultAge,
-1)
, hh.Step = '6.8.3'
from tlsa_Household hh
left outer join tlsa_HHID hh18 on hh18.HHAdultAge = 18
and hh18.HoHID = hh.HoHID and hh18.ActiveHHType = hh.HHType
and hh18.Active = 1 and hh18.LSAProjectType = 3
left outer join tlsa_HHID hh24 on hh24.HHAdultAge = 24
and hh24.HoHID = hh.HoHID and hh24.ActiveHHType = hh.HHType
and hh24.Active = 1 and hh24.LSAProjectType = 3
left outer join tlsa_HHID hh55 on hh55.HHAdultAge = 55
and hh55.HoHID = hh.HoHID and hh55.ActiveHHType = hh.HHType
and hh55.Active = 1 and hh55.LSAProjectType = 3
left outer join tlsa_HHID hh25 on hh25.HHAdultAge = 25
and hh25.Active = 1 and hh25.HoHID = hh.HoHID and hh25.ActiveHHType = hh.HHType
and hh25.LSAProjectType = 3
/*
6.9 System Engagement Status and Return Time
*/
update hh
set hh.FirstEntry = (select min(hhid.EntryDate)
from tlsa_HHID hhid
where hhid.Active = 1 and hhid.HoHID = hh.HoHID and hhid.ActiveHHType = hh.HHType)
, hh.Step = '6.9.1'
from tlsa_Household hh
update hh
set hh.StatEnrollmentID =
(select top 1 prior.EnrollmentID
from tlsa_HHID prior
where prior.ExitDate >= dateadd (dd,-730,hh.FirstEntry)
and prior.ExitDate < hh.FirstEntry
and prior.HoHID = hh.HoHID and prior.ActiveHHType = hh.HHType
order by prior.ExitDate desc)
, hh.Step = '6.9.2'
from tlsa_Household hh
update hh
set hh.Stat = case
when PSHStatus in (11,12) or RRHStatus in (11,12) or ESTStatus in (11,12)
then 5
when hh.StatEnrollmentID is null then 1
when dateadd(dd, 15, prior.ExitDate) > hh.FirstEntry then 5
when prior.ExitDest between 400 and 499 then 2
when prior.ExitDest between 100 and 399 then 3
else 4 end
--Note: ReturnTime is set to the actual number of days here and grouped into LSA categories
-- in 6.19 like other counts of days
, hh.ReturnTime = case
when PSHStatus in (11,12) or RRHStatus in (11,12) or ESTStatus in (11,12)
or hh.StatEnrollmentID is null
-- The line below has been corrected from >= to just >
or dateadd(dd, 15, prior.ExitDate) > hh.FirstEntry then -1
else datediff(dd, prior.ExitDate, hh.FirstEntry) end
, hh.Step = '6.9.3'
from tlsa_Household hh
left outer join tlsa_HHID prior on prior.EnrollmentID = hh.StatEnrollmentID
/*
6.10 Get Days In RRH Pre-Move-In
*/
update hh
set RRHPreMoveInDays = (select count(distinct cal.theDate)
from tlsa_HHID hhid
inner join lsa_Report rpt on rpt.ReportEnd >= hhid.EntryDate
inner join ref_Calendar cal on cal.theDate >= hhid.EntryDate
and cal.theDate <= coalesce(
dateadd(dd, -1, hhid.MoveInDate)
-- line below corrected to use the ExitDate and not ExitDate - 1
, hhid.ExitDate
, rpt.ReportEnd)
where hhid.LSAProjectType = 13
and hhid.ActiveHHType = hh.HHType and hhid.HoHID = hh.HoHID
and hhid.Active = 1)
, hh.Step = '6.10'
from tlsa_Household hh
/*
6.11 Get Dates Housed in PSH or RRH
*/
truncate table sys_Time
insert into sys_Time (HoHID, HHType, sysDate, sysStatus, Step)
select distinct hhid.HoHID, hhid.ActiveHHType, cal.theDate
, min(case hhid.LSAProjectType
when 3 then 1
else 2 end)
, '6.11'
from tlsa_HHID hhid
inner join tlsa_Household hh on hh.HoHID = hhid.HoHID and hh.HHType = hhid.ActiveHHType
inner join lsa_Report rpt on rpt.ReportEnd >= hhid.EntryDate
inner join ref_Calendar cal on cal.theDate >= hhid.MoveInDate
and (cal.theDate < hhid.ExitDate
or (hhid.ExitDate is null and cal.theDate <= rpt.ReportEnd))
and cal.theDate >= rpt.LookbackDate
where hhid.LSAProjectType in (3,13) and hhid.Active = 1
group by hhid.HoHID, hhid.ActiveHHType, cal.theDate
/*
6.12 Get Last Inactive Date
*/
--LastInactive = (FirstEntry - 1 day) for any household where Stat <> 5
-- and for any household where Stat = 5 but there is no enrollment for the HoHID/HHType
-- active in the six days prior to First Entry.
update hh
set hh.LastInactive = case
when dateadd(dd, -1, hh.FirstEntry) < dateadd(dd, -1, rpt.LookbackDate) then dateadd(dd, -1, rpt.LookbackDate)
else dateadd(dd, -1, hh.FirstEntry) end
, hh.Step = '6.12.1'
from tlsa_Household hh
inner join lsa_Report rpt on rpt.ReportEnd >= hh.FirstEntry
where hh.Stat <> 5
or (select top 1 hhid.EnrollmentID
from tlsa_HHID hhid
inner join lsa_Report rpt on hhid.ExitDate < rpt.ReportStart
where hhid.HoHID = hh.HoHID and hhid.ActiveHHType = hh.HHType
and dateadd(dd, 6, hhid.ExitDate) >= hh.FirstEntry) is null
insert into sys_TimePadded (HoHID, HHType, Cohort, StartDate, EndDate, Step)
select distinct hh.HoHID, hh.HHType, 1
, hhid.EntryDate
, case when hhid.ExitDate is null or dateadd(dd, 6, hhid.ExitDate) >= rpt.ReportEnd then rpt.ReportEnd
else dateadd(dd, 6, hhid.ExitDate) end
, '6.12.2.a'
from tlsa_Household hh
inner join lsa_Report rpt on rpt.ReportStart >= hh.FirstEntry
inner join tlsa_HHID hhid on hhid.HoHID = hh.HoHID and hhid.ActiveHHType = hh.HHType
and (hhid.Active = 1 or hhid.ExitDate < rpt.ReportStart)
where hh.LastInactive is null
and (hhid.LSAProjectType <> 1)
union
select distinct hh.HoHID, hh.HHType, 1
, bn.DateProvided
, case when dateadd(dd, 6, bn.DateProvided) <= rpt.ReportEnd then dateadd(dd, 6, bn.DateProvided) else rpt.ReportEnd end
, '6.12.2.b'
from tlsa_Household hh
inner join lsa_Report rpt on rpt.ReportStart >= hh.FirstEntry
inner join tlsa_HHID hhid on hhid.HoHID = hh.HoHID and hhid.ActiveHHType = hh.HHType
and (hhid.Active = 1 or hhid.ExitDate < rpt.ReportStart)
inner join hmis_Services bn on bn.EnrollmentID = hhid.EnrollmentID
and bn.DateProvided between rpt.LookbackDate and rpt.ReportEnd
and bn.DateProvided >= hhid.EntryDate
and (bn.DateProvided < hhid.ExitDate or hhid.ExitDate is null)
and bn.RecordType = 200 and bn.DateDeleted is null
and hhid.LSAProjectType = 1
where hh.LastInactive is null
update hh
set hh.LastInactive = coalesce(lastDay.inactive, dateadd(dd, -1, rpt.LookbackDate))
, hh.Step = '6.12.3'
from tlsa_Household hh
inner join lsa_Report rpt on rpt.ReportEnd >= hh.FirstEntry
left outer join
(select hh.HoHID, hh.HHType, max(cal.theDate) as inactive
from tlsa_Household hh
inner join lsa_Report rpt on rpt.ReportID = hh.ReportID
inner join ref_Calendar cal on cal.theDate <= rpt.ReportEnd
and cal.theDate >= rpt.LookbackDate
left outer join
sys_TimePadded stp on stp.HoHID = hh.HoHID and stp.HHType = hh.HHType
and cal.theDate between stp.StartDate and stp.EndDate
where stp.HoHID is null
and cal.theDate < hh.FirstEntry
group by hh.HoHID, hh.HHType
) lastDay on lastDay.HoHID = hh.HoHID and lastDay.HHType = hh.HHType
where hh.LastInactive is null
/*
6.13 Get Dates of Other System Use
*/
--Transitional Housing (sysStatus = 3) and SafeHaven/Entry-Exit ES (sysStatus = 4)
insert into sys_Time (HoHID, HHType, sysDate, sysStatus, Step)
select distinct hh.HoHID, hh.HHType, cal.theDate
, min(case when hhid.LSAProjectType = 2 then 3 else 4 end)
, '6.13.1'
from tlsa_Household hh
inner join tlsa_HHID hhid on hhid.HoHID = hh.HoHID and hhid.ActiveHHType = hh.HHType
inner join lsa_Report rpt on rpt.ReportEnd >= hhid.EntryDate
inner join ref_Calendar cal on
cal.theDate >= hhid.EntryDate
and cal.theDate > hh.LastInactive
and cal.theDate <= coalesce(dateadd(dd, -1, hhid.ExitDate), rpt.ReportEnd)
and cal.theDate >= rpt.LookbackDate
left outer join sys_Time housed on housed.HoHID = hh.HoHID and housed.HHType = hh.HHType
and housed.sysDate = cal.theDate
where housed.sysDate is null
and hhid.LSAProjectType in (0,2,8)
group by hh.HoHID, hh.HHType, cal.theDate
--Emergency Shelter (Night-by-Night) (sysStatus = 4)
insert into sys_Time (HoHID, HHType, sysDate, sysStatus, Step)
select distinct hh.HoHID, hh.HHType, cal.theDate, 4
, '6.13.2'
from tlsa_Household hh
inner join tlsa_HHID hhid on hhid.HoHID = hh.HoHID and hhid.ActiveHHType = hh.HHType
inner join lsa_Report rpt on rpt.ReportEnd >= hhid.EntryDate
inner join hmis_Services bn on bn.EnrollmentID = hhid.EnrollmentID
and bn.RecordType = 200 and bn.DateDeleted is null
inner join ref_Calendar cal on
cal.theDate = bn.DateProvided
and cal.theDate > hh.LastInactive
and cal.theDate between hhid.EntryDate and coalesce(dateadd(dd, -1, hhid.ExitDate), rpt.ReportEnd)
and cal.theDate >= rpt.LookbackDate
left outer join sys_Time other on other.HoHID = hh.HoHID and other.HHType = hh.HHType
and other.sysDate = cal.theDate
where other.sysDate is null and hhid.LSAProjectType = 1
--Homeless (Time prior to Move-In) in PSH and RRH (sysStatus = 5 and 6)
insert into sys_Time (HoHID, HHType, sysDate, sysStatus, Step)
select distinct hh.HoHID, hh.HHType, cal.theDate
, min (case when hhid.LSAProjectType = 3 then 5 else 6 end)
, '6.13.3'
from tlsa_Household hh
inner join tlsa_HHID hhid on hhid.HoHID = hh.HoHID and hhid.ActiveHHType = hh.HHType
inner join lsa_Report rpt on rpt.ReportEnd >= hhid.EntryDate
inner join ref_Calendar cal on
cal.theDate >= hhid.EntryDate
and cal.theDate <= coalesce(dateadd(dd, -1, hhid.MoveInDate), dateadd(dd, -1, hhid.ExitDate), rpt.ReportEnd)
and cal.theDate >= rpt.LookbackDate
left outer join sys_Time other on other.HoHID = hh.HoHID and other.HHType = hh.HHType
and other.sysDate = cal.theDate
where cal.theDate > hh.LastInactive
and other.sysDate is null and hhid.LSAProjectType in (3,13)
group by hh.HoHID, hh.HHType, cal.theDate
/*
6.14 Get Other Dates Homeless from 3.917 Living Situation
*/
--If there are enrollments in sys_Enrollment where EntryDate > LastInactive,
-- dates between the earliest DateToStreetESSH and LastInactive --
-- i.e., dates without a potential status conflict based on other system use --
-- populate Other3917Days as the difference in days between DateToStreetESSH
-- and LastInactive + 1.
--NOTE: This statement will leave Other3917Days NULL for households without
--at least one DateToStreetESSH prior to LastInactive. Final value for Other3917Days
--is the sum of days prior to LastInactive (if any) PLUS the count of dates
--after LastInactive that are added to sys_Time in the next statement.
update hh
set hh.Other3917Days = (select datediff (dd,
(select top 1 hn.DateToStreetESSH
from tlsa_HHID hhid
inner join hmis_Enrollment hn on hn.EnrollmentID = hhid.EnrollmentID
where hhid.HoHID = hh.HoHID and hhid.ActiveHHType = hh.HHType
and hhid.EntryDate > hh.LastInactive
and hn.DateToStreetESSH <= hh.LastInactive
and (hhid.LSAProjectType in (0,1,8)
or hn.LivingSituation between 100 and 199
or (hn.LengthOfStay in (10,11) and hn.PreviousStreetESSH = 1)
or (hn.LivingSituation between 200 and 299
and hn.LengthOfStay in (2,3) and hn.PreviousStreetESSH = 1))
order by hn.DateToStreetESSH asc)
, hh.LastInactive)) + 1
, hh.Step = '6.14.1'
from tlsa_Household hh
insert into sys_Time (HoHID, HHType, sysDate, sysStatus, Step)
select distinct other3917.HoHID, other3917.HHType, cal.theDate, 7
, '6.14.2'
from ref_Calendar cal
inner join (select hh.HoHID, hh.HHType
, case when hn.DateToStreetESSH >= hh.LastInactive then hn.DateToStreetESSH else hh.LastInactive end as StartDate
, hhid.EntryDate as EndDate
from tlsa_Household hh
inner join tlsa_HHID hhid on hhid.HoHID = hh.HoHID and hhid.EntryHHType = hh.HHType
inner join hmis_Enrollment hn on hn.EnrollmentID = hhid.EnrollmentID
where hhid.EntryDate > hh.LastInactive
and (hhid.LSAProjectType in (0,1,8)
or hn.LivingSituation between 100 and 199
or (hn.LengthOfStay in (10,11) and hn.PreviousStreetESSH = 1)
or (hn.LivingSituation between 200 and 299
and hn.LengthOfStay in (2,3) and hn.PreviousStreetESSH = 1))
) other3917 on other3917.StartDate <= cal.theDate and other3917.EndDate > cal.theDate
left outer join sys_Time priorStat on priorStat.HoHID = other3917.HoHID and priorStat.HHType = other3917.HHType
and priorStat.sysDate = cal.theDate
where priorStat.sysDate is null
/*
6.15 Set System Use Days for LSAHousehold
*/
update hh
set ESDays = (select count(distinct st.sysDate)
from sys_Time st
where st.sysStatus = 4
and st.HoHID = hh.HoHID and st.HHType = hh.HHType)
, THDays = (select count(distinct st.sysDate)
from sys_Time st
where st.sysStatus = 3
and st.HoHID = hh.HoHID and st.HHType = hh.HHType)
, ESTDays = (select count(distinct st.sysDate)
from sys_Time st
where st.sysStatus in (3,4)
and st.HoHID = hh.HoHID and st.HHType = hh.HHType)
, RRHPSHPreMoveInDays = (select count(distinct st.sysDate)
from sys_Time st
where st.sysStatus in (5,6)
and st.HoHID = hh.HoHID and st.HHType = hh.HHType)
, RRHHousedDays = (select count(distinct st.sysDate)
from sys_Time st
where st.sysStatus = 2
and st.HoHID = hh.HoHID and st.HHType = hh.HHType)
, SystemDaysNotPSHHoused = (select count(distinct st.sysDate)
from sys_Time st
where st.sysStatus in (2,3,4,5,6)
and st.HoHID = hh.HoHID and st.HHType = hh.HHType)
, SystemHomelessDays = (select count(distinct st.sysDate)
from sys_Time st
where st.sysStatus in (3,4,5,6)
and st.HoHID = hh.HoHID and st.HHType = hh.HHType)
, Other3917Days = case
when Other3917Days is null then 0
else Other3917Days end
+ (select count(distinct st.sysDate)
from sys_Time st
where st.sysStatus = 7
and st.HoHID = hh.HoHID and st.HHType = hh.HHType)
, TotalHomelessDays = case
when Other3917Days is null then 0
else Other3917Days end + (select count(distinct st.sysDate)
from sys_Time st
where st.sysStatus in (3,4,5,6,7)
and st.HoHID = hh.HoHID and st.HHType = hh.HHType)
, PSHHousedDays = (select count(distinct st.sysDate)
from sys_Time st
where st.sysStatus = 1
and st.HoHID = hh.HoHID and st.HHType = hh.HHType)
, Step = '6.15'
from tlsa_Household hh
/*
6.16 Update EST/RRH/PSHStatus
*/
update hh
set hh.ESTStatus = 2
, hh.Step = '6.16.1'
from tlsa_Household hh
inner join sys_Time st on st.HoHID = hh.HoHID and st.HHType = hh.HHType
where hh.ESTStatus = 0
and st.sysStatus in (3,4)
update hh
set hh.RRHStatus = 2
, hh.Step = '6.16.2'
from tlsa_Household hh
inner join sys_Time st on st.HoHID = hh.HoHID and st.HHType = hh.HHType
where hh.RRHStatus = 0
and st.sysStatus = 6
update hh
set hh.PSHStatus = 2
, hh.Step = '6.16.3'
from tlsa_Household hh
inner join sys_Time st on st.HoHID = hh.HoHID and st.HHType = hh.HHType
where hh.PSHStatus = 0
and st.sysStatus = 5
/*
6.17 Set EST/RRH/PSHAHAR
*/
update hh
set ESTAHAR = 0, RRHAHAR = 0, PSHAHAR = 0
, hh.Step = '6.17.1'
from tlsa_Household hh
update hh
set hh.ESTAHAR = 1
, hh.Step = '6.17.2'
from tlsa_Household hh
inner join tlsa_HHID hhid on hhid.HoHID = hh.HoHID and hhid.ActiveHHType = hh.HHType
inner join tlsa_Enrollment n on n.HouseholdID = hhid.HouseholdID and n.PersonalID = hhid.HoHID
where n.AHAR = 1 and hhid.LSAProjectType in (0,1,2,8)
update hh
set hh.RRHAHAR = 1
, hh.Step = '6.17.3'
from tlsa_Household hh
inner join tlsa_HHID hhid on hhid.HoHID = hh.HoHID and hhid.ActiveHHType = hh.HHType
inner join tlsa_Enrollment n on n.HouseholdID = hhid.HouseholdID and n.PersonalID = hhid.HoHID
where n.AHAR = 1 and hhid.LSAProjectType = 13
update hh
set hh.PSHAHAR = 1
, hh.Step = '6.17.4'
from tlsa_Household hh
inner join tlsa_HHID hhid on hhid.HoHID = hh.HoHID and hhid.ActiveHHType = hh.HHType
inner join tlsa_Enrollment n on n.HouseholdID = hhid.HouseholdID and n.PersonalID = hhid.HoHID
where n.AHAR = 1 and hhid.LSAProjectType = 3
/*
6.18 Set SystemPath for LSAHousehold
*/
update hh
set hh.SystemPath =
case when hh.ESTStatus not in (21,22) and hh.RRHStatus not in (21,22) and hh.PSHMoveIn = 2
then -1
when hh.ESDays >= 1 and hh.THDays = 0 and hh.RRHStatus = 0 and hh.PSHStatus = 0
then 1
when hh.ESDays = 0 and hh.THDays >= 1 and hh.RRHStatus = 0 and hh.PSHStatus = 0
then 2
when hh.ESDays >= 1 and hh.THDays >= 1 and hh.RRHStatus = 0 and hh.PSHStatus = 0
then 3
when hh.ESTStatus = 0 and hh.RRHStatus >= 11 and hh.PSHStatus = 0
then 4
when hh.ESDays >= 1 and hh.THDays = 0 and hh.RRHStatus >= 2 and hh.PSHStatus = 0
then 5
when hh.ESDays = 0 and hh.THDays >= 1 and hh.RRHStatus >= 2 and hh.PSHStatus = 0
then 6
when hh.ESDays >= 1 and hh.THDays >= 1 and hh.RRHStatus >= 2 and hh.PSHStatus = 0
then 7
when hh.ESTStatus = 0 and hh.RRHStatus = 0 and hh.PSHStatus >= 11 and hh.PSHMoveIn <> 2
then 8
when hh.ESDays >= 1 and hh.THDays = 0 and hh.RRHStatus = 0 and hh.PSHStatus >= 11 and hh.PSHMoveIn <> 2
then 9
when hh.ESTStatus in (21,22) and hh.ESDays >= 1 and hh.THDays = 0 and hh.RRHStatus = 0 and hh.PSHStatus >= 11 and hh.PSHMoveIn = 2
then 9
when hh.ESDays >= 1 and hh.THDays = 0 and hh.RRHStatus >= 2 and hh.PSHStatus >= 11 and hh.PSHMoveIn <> 2
then 10
when hh.ESTStatus in (21,22) and hh.ESDays >= 1 and hh.THDays = 0 and hh.RRHStatus in (21,22) and hh.PSHStatus >= 11 and hh.PSHMoveIn = 2
then 10
when hh.ESTStatus = 0 and hh.RRHStatus >= 2 and hh.PSHStatus >= 11 and hh.PSHMoveIn <> 2
then 11
when hh.ESTStatus = 0 and hh.RRHStatus in (21,22) and hh.PSHStatus >= 11 and hh.PSHMoveIn = 2
then 11
else 12 end
, hh.Step = '6.18'
from tlsa_Household hh
/*
6.19 LSAHousehold
*/
truncate table lsa_Household
insert into lsa_Household(RowTotal
, Stat, ReturnTime
, HHType, HHChronic, HHVet, HHDisability, HHFleeingDV, HoHRaceEthnicity
, HHAdult, HHChild, HHNoDOB
, HHAdultAge, HHParent
, ESTStatus, ESTGeography, ESTLivingSit, ESTDestination, ESTChronic, ESTVet, ESTDisability, ESTFleeingDV, ESTAC3Plus, ESTAdultAge, ESTParent
, RRHStatus, RRHMoveIn, RRHGeography, RRHLivingSit, RRHDestination, RRHPreMoveInDays, RRHChronic, RRHVet, RRHDisability, RRHFleeingDV, RRHAC3Plus, RRHAdultAge, RRHParent
, PSHStatus, PSHMoveIn, PSHGeography, PSHLivingSit, PSHDestination, PSHHousedDays, PSHChronic, PSHVet, PSHDisability, PSHFleeingDV, PSHAC3Plus, PSHAdultAge, PSHParent
, ESDays, THDays, ESTDays, RRHPSHPreMoveInDays, RRHHousedDays, SystemDaysNotPSHHoused, SystemHomelessDays, Other3917Days, TotalHomelessDays
, SystemPath, ESTAHAR, RRHAHAR, PSHAHAR, RRHSOStatus, RRHSOMoveIn, ReportID
)
select count (distinct HoHID + cast(HHType as nvarchar)), Stat
, case when Stat in (1,5) then -1
when ReturnTime between 15 and 30 then 30
when ReturnTime between 31 and 60 then 60
when ReturnTime between 61 and 90 then 90
when ReturnTime between 91 and 180 then 180
when ReturnTime between 181 and 365 then 365
when ReturnTime between 366 and 547 then 547
when ReturnTime >= 548 then 730
else ReturnTime end
, HHType, HHChronic, HHVet, HHDisability, HHFleeingDV, HoHRaceEthnicity
, HHAdult, HHChild, HHNoDOB
, HHAdultAge, HHParent
, ESTStatus, ESTGeography, ESTLivingSit, ESTDestination, ESTChronic, ESTVet, ESTDisability, ESTFleeingDV, ESTAC3Plus, ESTAdultAge, ESTParent
, RRHStatus, RRHMoveIn, RRHGeography, RRHLivingSit, RRHDestination
, case when RRHPreMoveInDays between 1 and 7 then 7
when RRHPreMoveInDays between 8 and 30 then 30
when RRHPreMoveInDays between 31 and 60 then 60