                     CASE WHEN contact_type != 'Infant' AND disposition = 'Unable to locate' THEN 1 ELSE 0 END AS unable_to_locate_hs,
                     CASE WHEN contact_type != 'Infant' AND disposition = 'Other' THEN 1 ELSE 0 END AS other_hs,
                     CASE WHEN contact_type != 'Infant' AND disposition IS NULL THEN 1 ELSE 0 END AS disposition_blank_hs,
-                    CASE WHEN contact_type != 'Infant' AND ((trisano.get_contact_hbsag_before(dce.id, current_date)).test_result IS NOT NULL AND (trisano.get_contact_antihb_before(dce.id, current_date)).test_result IS NOT NULL) THEN 1 ELSE 0 END AS total_hs_tested,
-                    CASE WHEN contact_type != 'Infant' AND (trisano.get_contact_hbsag_before(dce.id, current_date)).test_result ~ 'Positive' THEN 1 ELSE 0 END AS hbsag_pos_hs,
-                    CASE WHEN contact_type != 'Infant' AND (trisano.get_contact_antihb_before(dce.id, current_date)).test_result ~ 'Positive' THEN 1 ELSE 0 END AS antihb_pos_hs,
-                    CASE WHEN contact_type != 'Infant' AND hepb_dose1_date IS NOT NULL THEN 1 ELSE 0 END AS dose1_hs,
-                    CASE WHEN contact_type != 'Infant' AND hepb_dose1_date IS NOT NULL AND hepb_dose2_date IS NOT NULL THEN 1 ELSE 0 END AS dose2_hs,
-                    CASE WHEN contact_type != 'Infant' AND hepb_dose1_date IS NOT NULL AND hepb_dose2_date IS NOT NULL AND hepb_dose3_date IS NOT NULL THEN 1 ELSE 0 END AS dose3_hs,
+                    CASE WHEN
+                        contact_type != 'Infant' AND
+                        (
+                            (trisano.get_contact_hbsag_before(dce.id, NULL)).test_result IS NOT NULL AND
+                            (trisano.get_contact_antihb_before(dce.id, NULL)).test_result IS NOT NULL
+                        )
+                        THEN 1 ELSE 0 END AS total_hs_tested,
+                    CASE WHEN contact_type != 'Infant' AND (trisano.get_contact_hbsag_before(dce.id, NULL)).test_result ~ 'Positive / Reactive' THEN 1 ELSE 0 END AS hbsag_pos_hs,
+                    CASE WHEN contact_type != 'Infant' AND (trisano.get_contact_antihb_before(dce.id, NULL)).test_result ~ 'Positive / Reactive' THEN 1 ELSE 0 END AS antihb_pos_hs,
+                    CASE WHEN contact_type != 'Infant' AND treatments_agg.total_doses >= 1 THEN 1 ELSE 0 END AS dose1_hs,
+                    CASE WHEN contact_type != 'Infant' AND treatments_agg.total_doses >= 2 THEN 1 ELSE 0 END AS dose2_hs,
+                    CASE WHEN contact_type != 'Infant' AND treatments_agg.total_doses >= 3 THEN 1 ELSE 0 END AS dose3_hs,
+                    CASE WHEN contact_type != 'Infant' AND treatments_agg.total_doses >= 4 THEN 1 ELSE 0 END AS dose4_hs,
+                    CASE WHEN contact_type != 'Infant' AND treatments_agg.total_doses >= 5 THEN 1 ELSE 0 END AS dose5_hs,
+                    CASE WHEN contact_type != 'Infant' AND treatments_agg.total_doses >= 6 THEN 1 ELSE 0 END AS dose6_hs,
                     CASE WHEN contact_type = 'Infant' AND disposition = 'Moved' AND (hepb_dose1_date IS NOT NULL OR hepb_comvax1_date IS NOT NULL) AND hbig_vacc_date IS NOT NULL THEN 1 ELSE 0 END AS dose1_hbig_trans,
                     CASE WHEN contact_type = 'Infant' AND disposition = 'Moved' AND hepb_dose1_date IS NULL AND hepb_comvax1_date IS NULL AND hbig_vacc_date IS NOT NULL THEN 1 ELSE 0 END AS hbig_trans,
                     CASE WHEN contact_type = 'Infant' AND disposition = 'Moved' AND (hepb_dose1_date IS NOT NULL OR hepb_comvax1_date IS NOT NULL) AND hbig_vacc_date IS NULL THEN 1 ELSE 0 END AS dose1_trans,

                        ON (treatments_agg.contact_event_id = dce.id)
                WHERE
                    dce.disease_name = 'Hepatitis B Pregnancy Event' AND
                    dme.actual_delivery_date IS NOT NULL;
;

CREATE TABLE morb_sec_juris AS

                                    GROUP BY 1
                                ) treatments_agg
                                    ON (treatments_agg.contact_event_id = dce.id)
                            WHERE
                                disposition = 'Active follow up' OR disposition IS NULL
                        ) foo
                        WHERE fdd_act_code != -1
                    ) contact_stuff
