     hpart.medical_record_number,
     hpart.hospital_record_number
 FROM
-	events
-	LEFT JOIN participations p
-		ON (p.event_id = events.id)
-	LEFT JOIN places pl
-		ON (pl.entity_id = p.secondary_entity_id)
+    events
+    LEFT JOIN participations p
+        ON (p.event_id = events.id)
+    LEFT JOIN places pl
+        ON (pl.entity_id = p.secondary_entity_id)
     LEFT JOIN hospitals_participations hpart
         ON (hpart.participation_id = p.id)
 WHERE
-	p.type = 'HospitalizationFacility'
-;
+    p.type = 'HospitalizationFacility'
+;
+
+ALTER TABLE dw_events_hospitals
+    ADD CONSTRAINT pk_dw_events_hospitals PRIMARY KEY (id);
 
 CREATE TABLE dw_lab_results AS
 SELECT

    stateec.code_description AS state,
    pataddr.postal_code,

	upsert_date(disev.disease_onset_date) AS date_disease_onset,
	upsert_date(disev.date_diagnosed) AS date_disease_diagnosed,
	upsert_date(events.results_reported_to_clinician_date) AS results_reported_to_clinician_date,
	upsert_date(events."first_reported_PH_date") AS date_reported_to_public_health,

	upsert_date(events.event_onset_date) AS date_entered_into_system,
	upsert_date(events.investigation_started_date) AS date_investigation_started,
	upsert_date(events."investigation_completed_LHD_date") AS date_investigation_completed,
	upsert_date(events.review_completed_by_state_date) AS review_completed_by_state_date,

	events.created_at AS date_created,
	events.updated_at AS date_updated,
	events.deleted_at AS date_deleted,

	events.sent_to_cdc,

	partcon_disp_ec.code_description AS disposition_if_once_a_contact,		-- the_code?
	partcon_cont_ec.code_description AS contact_type_if_once_a_contact,		-- the_code?

-- Stuff that didn't show up in Pete's model
	ifi.code_description AS imported_from_code, 		-- code_description? 
--  	events."investigation_LHD_status_id",  Can be ignored, as it's never used
	events.sent_to_ibis,
	events.ibis_updated_at,
	disevdied.code_description AS disease_event_died		-- code description?
FROM events
	LEFT JOIN participations pplpart
		ON (events.id = pplpart.event_id)
    LEFT JOIN participations_risk_factors prf
        ON (prf.participation_id = pplpart.id)
	LEFT JOIN entities pplent
		ON (pplpart.primary_entity_id = pplent.id)
	LEFT JOIN people ppl
		ON (ppl.entity_id = pplent.id)
	LEFT JOIN external_codes ifi
		ON (events.imported_from_id = ifi.id)
	LEFT JOIN external_codes scsi
		ON (events.state_case_status_id = scsi.id)
	LEFT JOIN external_codes oaci
		ON (events.outbreak_associated_id = oaci.id)
	LEFT JOIN external_codes lcsi
		ON (events.lhd_case_status_id = lcsi.id)
	LEFT JOIN users inv
		ON (events.investigator_id = inv.id)
	LEFT JOIN disease_events disev
		ON (events.id = disev.event_id)
	LEFT JOIN diseases ds
		ON (disev.disease_id = ds.id)
	LEFT JOIN external_codes disevhosp
		ON (disevhosp.id = disev.hospitalized_id)
	LEFT JOIN external_codes disevdied
		ON (disevdied.id = disev.died_id)
	LEFT JOIN participations pa
		ON (pa.event_id = events.id)
	LEFT JOIN places ijpl
		ON (ijpl.entity_id = pa.secondary_entity_id)
    LEFT JOIN external_codes fhec
        ON (prf.food_handler_id = fhec.id)
    LEFT JOIN external_codes hcwec

COMMIT;

BEGIN;
CREATE TABLE dw_morbidity_patients AS
SELECT
    people.id,
    people.entity_id,            -- Keeping this just in case
    birth_gender_ec.code_description AS birth_gender,            -- code_description?
    ethnicity_ec.code_description AS ethnicity,                -- code_description?
    primary_language_ec.code_description AS primary_language,        -- code_description?
    people.first_name,
    people.middle_name,
    people.last_name,
    upsert_date(people.birth_date) AS birth_date,
    upsert_date(people.date_of_death) AS date_of_death,
    people.first_name_soundex,
    people.last_name_soundex
FROM
    people
    LEFT JOIN external_codes birth_gender_ec
        ON (birth_gender_ec.id = people.age_type_id)
    LEFT JOIN external_codes ethnicity_ec
        ON (ethnicity_ec.id = people.age_type_id)
    LEFT JOIN external_codes primary_language_ec
        ON (primary_language_ec.id = people.age_type_id)
    INNER JOIN (
        SELECT
            part.primary_entity_id
        FROM
            participations part
            INNER JOIN events
                ON (events.id = part.event_id)
        WHERE
            part.type = 'InterestedParty' AND
            events.type = 'MorbidityEvent'
        GROUP BY part.primary_entity_id
    ) f
        ON (f.primary_entity_id = people.entity_id)
;

CREATE TABLE dw_contact_patients AS
SELECT
    people.id,
    people.entity_id,            -- Keeping this just in case
    birth_gender_ec.code_description AS birth_gender,            -- code_description?
    ethnicity_ec.code_description AS ethnicity,                -- code_description?
    primary_language_ec.code_description AS primary_language,        -- code_description?
    people.first_name,
    people.middle_name,
    people.last_name,
    upsert_date(people.birth_date) AS birth_date,
    upsert_date(people.date_of_death) AS date_of_death,
    people.first_name_soundex,
    people.last_name_soundex
FROM
    people
    LEFT JOIN external_codes birth_gender_ec
        ON (birth_gender_ec.id = people.age_type_id)
    LEFT JOIN external_codes ethnicity_ec
        ON (ethnicity_ec.id = people.age_type_id)
    LEFT JOIN external_codes primary_language_ec
        ON (primary_language_ec.id = people.age_type_id)
    INNER JOIN (
        SELECT
            part.primary_entity_id
        FROM
            participations part
            INNER JOIN events
                ON (events.id = part.event_id)
        WHERE
            part.type = 'InterestedParty' AND
            events.type = 'ContactEvent'
        GROUP BY part.primary_entity_id
    ) f
        ON (f.primary_entity_id = people.entity_id)
;

CREATE TABLE dw_encounter_patients AS
SELECT
    people.id,
    people.entity_id,            -- Keeping this just in case
    birth_gender_ec.code_description AS birth_gender,            -- code_description?
    ethnicity_ec.code_description AS ethnicity,                -- code_description?
    primary_language_ec.code_description AS primary_language,        -- code_description?
    people.first_name,
    people.middle_name,
    people.last_name,
    upsert_date(people.birth_date) AS birth_date,
    upsert_date(people.date_of_death) AS date_of_death,
    people.first_name_soundex,
    people.last_name_soundex
FROM
    people
    LEFT JOIN external_codes birth_gender_ec
        ON (birth_gender_ec.id = people.age_type_id)
    LEFT JOIN external_codes ethnicity_ec
        ON (ethnicity_ec.id = people.age_type_id)
    LEFT JOIN external_codes primary_language_ec
        ON (primary_language_ec.id = people.age_type_id)
    INNER JOIN (
        SELECT
            part.primary_entity_id
        FROM
            participations part
            INNER JOIN events
                ON (events.id = part.event_id)
        WHERE
            part.type = 'InterestedParty' AND
            events.type = 'EncounterEvent'
        GROUP BY part.primary_entity_id
    ) f
        ON (f.primary_entity_id = people.entity_id)
;

CREATE TABLE dw_morbidity_events AS
SELECT
    events.id,
    events.parent_id,               -- Reporting tool might provide a field "was_a_contact" == parent_id IS NOT NULL
    ppl.id AS dw_patients_id,

    ds.id AS disease_id,
    ijpl.name AS investigating_jurisdiction,
    ijpl.id AS investigating_jurisdiction_id,
    jorpl.name AS jurisdiction_of_residence,
    jorpl.id AS jurisdiction_of_residence_id,
    scsi.code_description AS state_case_status_code,     -- code_description?
    lcsi.code_description AS lhd_case_status_code,        -- code_description?
    events."MMWR_week",
    events."MMWR_year",

    events.event_name,
    events.record_number,

    events.age_at_onset AS actual_age_at_onset,
    agetypeec.code_description AS actual_age_type,
    ppl.approximate_age_no_birthday AS estimated_age_at_onset,
    est_ec.code_description AS estimated_age_type,
    events.parent_guardian,

    fhec.code_description AS food_handler,
    hcwec.code_description AS healthcare_worker,
